This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Added porting tests for CUSTOMIZED files
[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
48f8bad9 47ok defined(@ary);
d47e1c27
NC
48{
49 no warnings 'deprecated';
50 ok defined(%ary);
51}
52ok %ary;
a687059c 53undef @ary;
48f8bad9 54ok !defined(@ary);
a687059c 55undef %ary;
d47e1c27
NC
56{
57 no warnings 'deprecated';
58 ok !defined(%ary);
59}
60ok !%ary;
a687059c 61@ary = (1);
48f8bad9 62ok defined @ary;
a687059c 63%ary = (1,1);
d47e1c27
NC
64{
65 no warnings 'deprecated';
66 ok defined %ary;
67}
68ok %ary;
a687059c 69
48f8bad9 70sub foo { pass; 1 }
a687059c 71
48f8bad9 72&foo || fail;
a687059c 73
48f8bad9 74ok defined &foo;
a687059c 75undef &foo;
48f8bad9 76ok !defined(&foo);
a3f914c5
GS
77
78eval { undef $1 };
48f8bad9 79like $@, qr/^Modification of a read/;
a3f914c5
GS
80
81eval { $1 = undef };
48f8bad9 82like $@, qr/^Modification of a read/;
a3f914c5 83
659eaf73
GS
84{
85 require Tie::Hash;
86 tie my %foo, 'Tie::StdHash';
140d27a4 87 no warnings 'deprecated';
48f8bad9 88 ok defined %foo;
659eaf73 89 %foo = ( a => 1 );
48f8bad9 90 ok defined %foo;
659eaf73
GS
91}
92
93{
94 require Tie::Array;
95 tie my @foo, 'Tie::StdArray';
140d27a4 96 no warnings 'deprecated';
48f8bad9 97 ok defined @foo;
659eaf73 98 @foo = ( a => 1 );
48f8bad9 99 ok defined @foo;
659eaf73 100}
3d387947
JH
101
102{
103 # [perl #17753] segfault when undef'ing unquoted string constant
104 eval 'undef tcp';
48f8bad9 105 like $@, qr/^Can't modify constant item/;
3d387947 106}
2f86008e
DM
107
108# bugid 3096
109# undefing a hash may free objects with destructors that then try to
3b37eb24
DM
110# modify the hash. Ensure that the hash remains consistent
111
112{
113 my (%hash, %mirror);
114
115 my $iters = 5;
116
117 for (1..$iters) {
118 $hash{"k$_"} = bless ["k$_"], 'X';
119 $mirror{"k$_"} = "k$_";
120 }
121
122
123 my $c = $iters;
124 my $events;
125
126 sub X::DESTROY {
127 my $key = $_[0][0];
128 $events .= 'D';
129 note("----- DELETE($key) ------");
130 delete $mirror{$key};
131
132 is join('-', sort keys %hash), join('-', sort keys %mirror),
133 "$key: keys";
134 is join('-', sort map $_->[0], values %hash),
135 join('-', sort values %mirror), "$key: values";
136
137 # don't know exactly what we'll get from the iterator, but
138 # it must be a sensible value
139 my ($k, $v) = each %hash;
140 ok defined $k ? exists($mirror{$k}) : (keys(%mirror) == 0),
141 "$key: each 1";
142
143 is delete $hash{$key}, undef, "$key: delete";
144 ($k, $v) = each %hash;
145 ok defined $k ? exists($mirror{$k}) : (keys(%mirror) <= 1),
146 "$key: each 2";
147
148 $c++;
149 if ($c <= $iters * 2) {
150 $hash{"k$c"} = bless ["k$c"], 'X';
151 $mirror{"k$c"} = "k$c";
152 }
153 $events .= 'E';
154 }
155
156 each %hash; # set eiter
157 undef %hash;
158
159 is scalar keys %hash, 0, "hash empty at end";
160 is $events, ('DE' x ($iters*2)), "events";
161 my ($k, $v) = each %hash;
162 is $k, undef, 'each undef at end';
2f86008e 163}
6e592b3a
BM
164
165# this will segfault if it fails
166
167sub PVBM () { 'foo' }
168{ my $dummy = index 'foo', PVBM }
169
170my $pvbm = PVBM;
171undef $pvbm;
48f8bad9 172ok !defined $pvbm;