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