11 my (@ary, %ary, %hash);
42 ok defined($ary{'foo'});
43 ok !defined($ary{'bar'});
45 ok !defined($ary{'foo'});
56 like $@, qr/^Modification of a read/;
59 like $@, qr/^Modification of a read/;
62 # [perl #17753] segfault when undef'ing unquoted string constant
64 like $@, qr/^Can't modify constant item/;
68 # undefing a hash may free objects with destructors that then try to
69 # modify the hash. Ensure that the hash remains consistent
77 $hash{"k$_"} = bless ["k$_"], 'X';
78 $mirror{"k$_"} = "k$_";
88 note("----- DELETE($key) ------");
91 is join('-', sort keys %hash), join('-', sort keys %mirror),
93 is join('-', sort map $_->[0], values %hash),
94 join('-', sort values %mirror), "$key: values";
96 # don't know exactly what we'll get from the iterator, but
97 # it must be a sensible value
98 my ($k, $v) = each %hash;
99 ok defined $k ? exists($mirror{$k}) : (keys(%mirror) == 0),
102 is delete $hash{$key}, undef, "$key: delete";
103 ($k, $v) = each %hash;
104 ok defined $k ? exists($mirror{$k}) : (keys(%mirror) <= 1),
108 if ($c <= $iters * 2) {
109 $hash{"k$c"} = bless ["k$c"], 'X';
110 $mirror{"k$c"} = "k$c";
115 each %hash; # set eiter
118 is scalar keys %hash, 0, "hash empty at end";
119 is $events, ('DE' x ($iters*2)), "events";
120 my ($k, $v) = each %hash;
121 is $k, undef, 'each undef at end';
124 # part of #105906: inlined undef constant getting copied
125 BEGIN { $::{z} = \undef }
129 is $_[0], $_[1], 'undef constants preserve identity';
134 DESTROY { $messages .= 'destroyed ' }
138 my $t = bless [], 'Thingie';
140 }->(), $messages .= 'after ';
145 is $messages, 'destroyed after ', 'undef $scalar frees refs immediately';
148 # this will segfault if it fails
150 sub PVBM () { 'foo' }
151 { my $dummy = index 'foo', PVBM }
157 # Prior to GH#20077 (Add OPpTARGET_MY optimization to OP_UNDEF), any PV
158 # allocation was kept with "$x = undef" but freed with "undef $x". That
159 # behaviour was carried over and is expected to still be present.
160 # (I totally copied most of this block from other t/op/* files.)
163 skip_without_dynamic_extension("Devel::Peek", 2);
165 my $out = runperl(stderr => 1,
166 progs => [ split /\n/, <<'EOS' ]);
168 my $f = q(x) x 40; $f = undef;
169 Devel::Peek::Dump($f);
171 Devel::Peek::Dump($f);
174 my ($space, $first, $second) = split /SV =/, $out;
175 like($first, qr/\bPV = 0x[0-9a-f]+\b/, '$x = undef preserves PV allocation');
176 like($second, qr/\bPV = 0\b$/, 'undef $x frees PV allocation');
179 # Tests suggested for GH#20077 (Add OPpTARGET_MY optimization to OP_UNDEF)
180 # (No failures were observed during development, these are just checking
181 # that no failures are introduced down the line.)
184 my $y= 1; my @x= ($y= undef);
185 is( defined($x[0]), "", 'lval undef assignment in list context');
186 is( defined($y) , "", 'scalar undef assignment in list context');
188 $y= 1; my $z; sub f{$z = shift} f($y=undef);
189 is( defined($y) , "", 'undef assignment in sub args');
190 is( defined($z) , "", 'undef assignment reaches @_');
192 ($y,$z)=(1,2); sub f{} f(($y=undef),$z);
193 is( defined($y) , "", 'undef assignment reaches @_');
194 is( $z, 2, 'undef adjacent argument is unchanged');
198 my $h= { baz => 1 }; my @k= keys %{($h=undef)||{}};
199 is( defined($h) , "", 'scalar undef assignment in keys');
200 is( scalar @k, 0, 'undef assignment dor anonhash');
202 my $y= 1; my @x= \($y= undef);
203 is( defined($y) , "", 'scalar undef assignment before reference');
204 is( scalar @x, 1, 'assignment of one element to array');
205 is( defined($x[0]->$*), "", 'assignment of undef element to array');
208 # GH#20336 - "my $x = undef" pushed &PL_sv_undef onto the stack, but
209 # should be pushing $x (i.e. a mutable copy of &PL_sv_undef)
210 is( ++(my $x = undef), 1, '"my $x = undef" pushes $x onto the stack' );