This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove the port to MiNT. It's a dead platform that hasn't had any love since 5.005
[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 37;
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 ok defined(%ary);
49 undef @ary;
50 ok !defined(@ary);
51 undef %ary;
52 ok !defined(%ary);
53 @ary = (1);
54 ok defined @ary;
55 %ary = (1,1);
56 ok defined %ary;
57
58 sub foo { pass; 1 }
59
60 &foo || fail;
61
62 ok defined &foo;
63 undef &foo;
64 ok !defined(&foo);
65
66 eval { undef $1 };
67 like $@, qr/^Modification of a read/;
68
69 eval { $1 = undef };
70 like $@, qr/^Modification of a read/;
71
72 {
73     require Tie::Hash;
74     tie my %foo, 'Tie::StdHash';
75     ok defined %foo;
76     %foo = ( a => 1 );
77     ok defined %foo;
78 }
79
80 {
81     require Tie::Array;
82     tie my @foo, 'Tie::StdArray';
83     ok defined @foo;
84     @foo = ( a => 1 );
85     ok defined @foo;
86 }
87
88 {
89     # [perl #17753] segfault when undef'ing unquoted string constant
90     eval 'undef tcp';
91     like $@, qr/^Can't modify constant item/;
92 }
93
94 # bugid 3096
95 # undefing a hash may free objects with destructors that then try to
96 # modify the hash. To them, the hash should appear empty.
97
98 %hash = (
99     key1 => bless({}, 'X'),
100     key2 => bless({}, 'X'),
101 );
102 undef %hash;
103 sub X::DESTROY {
104     is scalar keys %hash, 0;
105     is scalar values %hash, 0;
106     my @l = each %hash;
107     is @l, 0;
108     is delete $hash{'key2'}, undef;
109 }
110
111 # this will segfault if it fails
112
113 sub PVBM () { 'foo' }
114 { my $dummy = index 'foo', PVBM }
115
116 my $pvbm = PVBM;
117 undef $pvbm;
118 ok !defined $pvbm;