defined @array and defined %hash need no warnings 'deprecated';
[p5sagit/p5-mst-13.2.git] / t / op / undef.t
CommitLineData
a687059c 1#!./perl
2
e03bd546 3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
48f8bad9 6 require './test.pl';
e03bd546 7}
8
48f8bad9 9use strict;
a687059c 10
48f8bad9 11use vars qw(@ary %ary %hash);
12
13plan 37;
14
15ok !defined($a);
a687059c 16
17$a = 1+1;
48f8bad9 18ok defined($a);
a687059c 19
20undef $a;
48f8bad9 21ok !defined($a);
a687059c 22
23$a = "hi";
48f8bad9 24ok defined($a);
a687059c 25
26$a = $b;
48f8bad9 27ok !defined($a);
a687059c 28
29@ary = ("1arg");
30$a = pop(@ary);
48f8bad9 31ok defined($a);
a687059c 32$a = pop(@ary);
48f8bad9 33ok !defined($a);
a687059c 34
35@ary = ("1arg");
36$a = shift(@ary);
48f8bad9 37ok defined($a);
a687059c 38$a = shift(@ary);
48f8bad9 39ok !defined($a);
a687059c 40
41$ary{'foo'} = 'hi';
48f8bad9 42ok defined($ary{'foo'});
43ok !defined($ary{'bar'});
a687059c 44undef $ary{'foo'};
48f8bad9 45ok !defined($ary{'foo'});
a687059c 46
48f8bad9 47ok defined(@ary);
48ok defined(%ary);
a687059c 49undef @ary;
48f8bad9 50ok !defined(@ary);
a687059c 51undef %ary;
48f8bad9 52ok !defined(%ary);
a687059c 53@ary = (1);
48f8bad9 54ok defined @ary;
a687059c 55%ary = (1,1);
48f8bad9 56ok defined %ary;
a687059c 57
48f8bad9 58sub foo { pass; 1 }
a687059c 59
48f8bad9 60&foo || fail;
a687059c 61
48f8bad9 62ok defined &foo;
a687059c 63undef &foo;
48f8bad9 64ok !defined(&foo);
a3f914c5 65
66eval { undef $1 };
48f8bad9 67like $@, qr/^Modification of a read/;
a3f914c5 68
69eval { $1 = undef };
48f8bad9 70like $@, qr/^Modification of a read/;
a3f914c5 71
659eaf73 72{
73 require Tie::Hash;
74 tie my %foo, 'Tie::StdHash';
140d27a4 75 no warnings 'deprecated';
48f8bad9 76 ok defined %foo;
659eaf73 77 %foo = ( a => 1 );
48f8bad9 78 ok defined %foo;
659eaf73 79}
80
81{
82 require Tie::Array;
83 tie my @foo, 'Tie::StdArray';
140d27a4 84 no warnings 'deprecated';
48f8bad9 85 ok defined @foo;
659eaf73 86 @foo = ( a => 1 );
48f8bad9 87 ok defined @foo;
659eaf73 88}
3d387947 89
90{
91 # [perl #17753] segfault when undef'ing unquoted string constant
92 eval 'undef tcp';
48f8bad9 93 like $@, qr/^Can't modify constant item/;
3d387947 94}
2f86008e 95
96# bugid 3096
97# undefing a hash may free objects with destructors that then try to
98# modify the hash. To them, the hash should appear empty.
99
2f86008e 100%hash = (
101 key1 => bless({}, 'X'),
102 key2 => bless({}, 'X'),
103);
104undef %hash;
105sub X::DESTROY {
48f8bad9 106 is scalar keys %hash, 0;
107 is scalar values %hash, 0;
108 my @l = each %hash;
109 is @l, 0;
110 is delete $hash{'key2'}, undef;
2f86008e 111}
6e592b3a 112
113# this will segfault if it fails
114
115sub PVBM () { 'foo' }
116{ my $dummy = index 'foo', PVBM }
117
118my $pvbm = PVBM;
119undef $pvbm;
48f8bad9 120ok !defined $pvbm;