This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Avoid uninit warning for qq|${\<<FOO}|
[perl5.git] / t / op / undef.t
CommitLineData
a687059c
LW
1#!./perl
2
e03bd546
JH
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
48f8bad9 6 require './test.pl';
e03bd546
JH
7}
8
48f8bad9 9use strict;
a687059c 10
48f8bad9
NC
11use vars qw(@ary %ary %hash);
12
3b37eb24 13plan 85;
48f8bad9
NC
14
15ok !defined($a);
a687059c
LW
16
17$a = 1+1;
48f8bad9 18ok defined($a);
a687059c
LW
19
20undef $a;
48f8bad9 21ok !defined($a);
a687059c
LW
22
23$a = "hi";
48f8bad9 24ok defined($a);
a687059c
LW
25
26$a = $b;
48f8bad9 27ok !defined($a);
a687059c
LW
28
29@ary = ("1arg");
30$a = pop(@ary);
48f8bad9 31ok defined($a);
a687059c 32$a = pop(@ary);
48f8bad9 33ok !defined($a);
a687059c
LW
34
35@ary = ("1arg");
36$a = shift(@ary);
48f8bad9 37ok defined($a);
a687059c 38$a = shift(@ary);
48f8bad9 39ok !defined($a);
a687059c
LW
40
41$ary{'foo'} = 'hi';
48f8bad9
NC
42ok defined($ary{'foo'});
43ok !defined($ary{'bar'});
a687059c 44undef $ary{'foo'};
48f8bad9 45ok !defined($ary{'foo'});
a687059c 46
d47e1c27
NC
47{
48 no warnings 'deprecated';
33f89799 49 ok defined(@ary);
d47e1c27
NC
50 ok defined(%ary);
51}
52ok %ary;
a687059c 53undef @ary;
33f89799
NC
54{
55 no warnings 'deprecated';
56 ok !defined(@ary);
57}
a687059c 58undef %ary;
d47e1c27
NC
59{
60 no warnings 'deprecated';
61 ok !defined(%ary);
62}
63ok !%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}
74ok %ary;
a687059c 75
48f8bad9 76sub foo { pass; 1 }
a687059c 77
48f8bad9 78&foo || fail;
a687059c 79
48f8bad9 80ok defined &foo;
a687059c 81undef &foo;
48f8bad9 82ok !defined(&foo);
a3f914c5
GS
83
84eval { undef $1 };
48f8bad9 85like $@, qr/^Modification of a read/;
a3f914c5
GS
86
87eval { $1 = undef };
48f8bad9 88like $@, 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
BM
170
171# this will segfault if it fails
172
173sub PVBM () { 'foo' }
174{ my $dummy = index 'foo', PVBM }
175
176my $pvbm = PVBM;
177undef $pvbm;
48f8bad9 178ok !defined $pvbm;