Commit | Line | Data |
---|---|---|
a687059c LW |
1 | #!./perl |
2 | ||
e03bd546 JH |
3 | BEGIN { |
4 | chdir 't' if -d 't'; | |
20822f61 | 5 | @INC = '../lib'; |
48f8bad9 | 6 | require './test.pl'; |
e03bd546 JH |
7 | } |
8 | ||
48f8bad9 | 9 | use strict; |
a687059c | 10 | |
48f8bad9 NC |
11 | use vars qw(@ary %ary %hash); |
12 | ||
d054cdb0 | 13 | plan 86; |
48f8bad9 NC |
14 | |
15 | ok !defined($a); | |
a687059c LW |
16 | |
17 | $a = 1+1; | |
48f8bad9 | 18 | ok defined($a); |
a687059c LW |
19 | |
20 | undef $a; | |
48f8bad9 | 21 | ok !defined($a); |
a687059c LW |
22 | |
23 | $a = "hi"; | |
48f8bad9 | 24 | ok defined($a); |
a687059c LW |
25 | |
26 | $a = $b; | |
48f8bad9 | 27 | ok !defined($a); |
a687059c LW |
28 | |
29 | @ary = ("1arg"); | |
30 | $a = pop(@ary); | |
48f8bad9 | 31 | ok defined($a); |
a687059c | 32 | $a = pop(@ary); |
48f8bad9 | 33 | ok !defined($a); |
a687059c LW |
34 | |
35 | @ary = ("1arg"); | |
36 | $a = shift(@ary); | |
48f8bad9 | 37 | ok defined($a); |
a687059c | 38 | $a = shift(@ary); |
48f8bad9 | 39 | ok !defined($a); |
a687059c LW |
40 | |
41 | $ary{'foo'} = 'hi'; | |
48f8bad9 NC |
42 | ok defined($ary{'foo'}); |
43 | ok !defined($ary{'bar'}); | |
a687059c | 44 | undef $ary{'foo'}; |
48f8bad9 | 45 | ok !defined($ary{'foo'}); |
a687059c | 46 | |
d47e1c27 NC |
47 | { |
48 | no warnings 'deprecated'; | |
33f89799 | 49 | ok defined(@ary); |
d47e1c27 NC |
50 | ok defined(%ary); |
51 | } | |
52 | ok %ary; | |
a687059c | 53 | undef @ary; |
33f89799 NC |
54 | { |
55 | no warnings 'deprecated'; | |
56 | ok !defined(@ary); | |
57 | } | |
a687059c | 58 | undef %ary; |
d47e1c27 NC |
59 | { |
60 | no warnings 'deprecated'; | |
61 | ok !defined(%ary); | |
62 | } | |
63 | ok !%ary; | |
a687059c | 64 | @ary = (1); |
33f89799 NC |
65 | { |
66 | no warnings 'deprecated'; | |
67 | ok defined @ary; | |
68 | } | |
a687059c | 69 | %ary = (1,1); |
d47e1c27 NC |
70 | { |
71 | no warnings 'deprecated'; | |
72 | ok defined %ary; | |
73 | } | |
74 | ok %ary; | |
a687059c | 75 | |
48f8bad9 | 76 | sub foo { pass; 1 } |
a687059c | 77 | |
48f8bad9 | 78 | &foo || fail; |
a687059c | 79 | |
48f8bad9 | 80 | ok defined &foo; |
a687059c | 81 | undef &foo; |
48f8bad9 | 82 | ok !defined(&foo); |
a3f914c5 GS |
83 | |
84 | eval { undef $1 }; | |
48f8bad9 | 85 | like $@, qr/^Modification of a read/; |
a3f914c5 GS |
86 | |
87 | eval { $1 = undef }; | |
48f8bad9 | 88 | like $@, qr/^Modification of a read/; |
a3f914c5 | 89 | |
659eaf73 GS |
90 | { |
91 | require Tie::Hash; | |
92 | tie my %foo, 'Tie::StdHash'; | |
140d27a4 | 93 | no warnings 'deprecated'; |
48f8bad9 | 94 | ok defined %foo; |
659eaf73 | 95 | %foo = ( a => 1 ); |
48f8bad9 | 96 | ok defined %foo; |
659eaf73 GS |
97 | } |
98 | ||
99 | { | |
100 | require Tie::Array; | |
101 | tie my @foo, 'Tie::StdArray'; | |
140d27a4 | 102 | no warnings 'deprecated'; |
48f8bad9 | 103 | ok defined @foo; |
659eaf73 | 104 | @foo = ( a => 1 ); |
48f8bad9 | 105 | ok defined @foo; |
659eaf73 | 106 | } |
3d387947 JH |
107 | |
108 | { | |
109 | # [perl #17753] segfault when undef'ing unquoted string constant | |
110 | eval 'undef tcp'; | |
48f8bad9 | 111 | like $@, qr/^Can't modify constant item/; |
3d387947 | 112 | } |
2f86008e DM |
113 | |
114 | # bugid 3096 | |
115 | # undefing a hash may free objects with destructors that then try to | |
3b37eb24 DM |
116 | # modify the hash. Ensure that the hash remains consistent |
117 | ||
118 | { | |
119 | my (%hash, %mirror); | |
120 | ||
121 | my $iters = 5; | |
122 | ||
123 | for (1..$iters) { | |
124 | $hash{"k$_"} = bless ["k$_"], 'X'; | |
125 | $mirror{"k$_"} = "k$_"; | |
126 | } | |
127 | ||
128 | ||
129 | my $c = $iters; | |
130 | my $events; | |
131 | ||
132 | sub X::DESTROY { | |
133 | my $key = $_[0][0]; | |
134 | $events .= 'D'; | |
135 | note("----- DELETE($key) ------"); | |
136 | delete $mirror{$key}; | |
137 | ||
138 | is join('-', sort keys %hash), join('-', sort keys %mirror), | |
139 | "$key: keys"; | |
140 | is join('-', sort map $_->[0], values %hash), | |
141 | join('-', sort values %mirror), "$key: values"; | |
142 | ||
143 | # don't know exactly what we'll get from the iterator, but | |
144 | # it must be a sensible value | |
145 | my ($k, $v) = each %hash; | |
146 | ok defined $k ? exists($mirror{$k}) : (keys(%mirror) == 0), | |
147 | "$key: each 1"; | |
148 | ||
149 | is delete $hash{$key}, undef, "$key: delete"; | |
150 | ($k, $v) = each %hash; | |
151 | ok defined $k ? exists($mirror{$k}) : (keys(%mirror) <= 1), | |
152 | "$key: each 2"; | |
153 | ||
154 | $c++; | |
155 | if ($c <= $iters * 2) { | |
156 | $hash{"k$c"} = bless ["k$c"], 'X'; | |
157 | $mirror{"k$c"} = "k$c"; | |
158 | } | |
159 | $events .= 'E'; | |
160 | } | |
161 | ||
162 | each %hash; # set eiter | |
163 | undef %hash; | |
164 | ||
165 | is scalar keys %hash, 0, "hash empty at end"; | |
166 | is $events, ('DE' x ($iters*2)), "events"; | |
167 | my ($k, $v) = each %hash; | |
168 | is $k, undef, 'each undef at end'; | |
2f86008e | 169 | } |
6e592b3a | 170 | |
d054cdb0 FC |
171 | # part of #105906: inlined undef constant getting copied |
172 | BEGIN { $::{z} = \undef } | |
173 | for (z,z) { | |
174 | push @_, \$_; | |
175 | } | |
176 | is $_[0], $_[1], 'undef constants preserve identity'; | |
177 | ||
6e592b3a BM |
178 | # this will segfault if it fails |
179 | ||
180 | sub PVBM () { 'foo' } | |
181 | { my $dummy = index 'foo', PVBM } | |
182 | ||
183 | my $pvbm = PVBM; | |
184 | undef $pvbm; | |
48f8bad9 | 185 | ok !defined $pvbm; |