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
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 85;
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 {
49     no warnings 'deprecated';
50     ok defined(%ary);
51 }
52 ok %ary;
53 undef @ary;
54 ok !defined(@ary);
55 undef %ary;
56 {
57     no warnings 'deprecated';
58     ok !defined(%ary);
59 }
60 ok !%ary;
61 @ary = (1);
62 ok defined @ary;
63 %ary = (1,1);
64 {
65     no warnings 'deprecated';
66     ok defined %ary;
67 }
68 ok %ary;
69
70 sub foo { pass; 1 }
71
72 &foo || fail;
73
74 ok defined &foo;
75 undef &foo;
76 ok !defined(&foo);
77
78 eval { undef $1 };
79 like $@, qr/^Modification of a read/;
80
81 eval { $1 = undef };
82 like $@, qr/^Modification of a read/;
83
84 {
85     require Tie::Hash;
86     tie my %foo, 'Tie::StdHash';
87     no warnings 'deprecated';
88     ok defined %foo;
89     %foo = ( a => 1 );
90     ok defined %foo;
91 }
92
93 {
94     require Tie::Array;
95     tie my @foo, 'Tie::StdArray';
96     no warnings 'deprecated';
97     ok defined @foo;
98     @foo = ( a => 1 );
99     ok defined @foo;
100 }
101
102 {
103     # [perl #17753] segfault when undef'ing unquoted string constant
104     eval 'undef tcp';
105     like $@, qr/^Can't modify constant item/;
106 }
107
108 # bugid 3096
109 # undefing a hash may free objects with destructors that then try to
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';
163 }
164
165 # this will segfault if it fails
166
167 sub PVBM () { 'foo' }
168 { my $dummy = index 'foo', PVBM }
169
170 my $pvbm = PVBM;
171 undef $pvbm;
172 ok !defined $pvbm;