This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Convert undef.t to use test.pl and strict.
[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
13plan 37;
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
NC
47ok defined(@ary);
48ok defined(%ary);
a687059c 49undef @ary;
48f8bad9 50ok !defined(@ary);
a687059c 51undef %ary;
48f8bad9 52ok !defined(%ary);
a687059c 53@ary = (1);
48f8bad9 54ok defined @ary;
a687059c 55%ary = (1,1);
48f8bad9 56ok defined %ary;
a687059c 57
48f8bad9 58sub foo { pass; 1 }
a687059c 59
48f8bad9 60&foo || fail;
a687059c 61
48f8bad9 62ok defined &foo;
a687059c 63undef &foo;
48f8bad9 64ok !defined(&foo);
a3f914c5
GS
65
66eval { undef $1 };
48f8bad9 67like $@, qr/^Modification of a read/;
a3f914c5
GS
68
69eval { $1 = undef };
48f8bad9 70like $@, qr/^Modification of a read/;
a3f914c5 71
659eaf73
GS
72{
73 require Tie::Hash;
74 tie my %foo, 'Tie::StdHash';
48f8bad9 75 ok defined %foo;
659eaf73 76 %foo = ( a => 1 );
48f8bad9 77 ok defined %foo;
659eaf73
GS
78}
79
80{
81 require Tie::Array;
82 tie my @foo, 'Tie::StdArray';
48f8bad9 83 ok defined @foo;
659eaf73 84 @foo = ( a => 1 );
48f8bad9 85 ok defined @foo;
659eaf73 86}
3d387947
JH
87
88{
89 # [perl #17753] segfault when undef'ing unquoted string constant
90 eval 'undef tcp';
48f8bad9 91 like $@, qr/^Can't modify constant item/;
3d387947 92}
2f86008e
DM
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
2f86008e
DM
98%hash = (
99 key1 => bless({}, 'X'),
100 key2 => bless({}, 'X'),
101);
102undef %hash;
103sub X::DESTROY {
48f8bad9
NC
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;
2f86008e 109}
6e592b3a
BM
110
111# this will segfault if it fails
112
113sub PVBM () { 'foo' }
114{ my $dummy = index 'foo', PVBM }
115
116my $pvbm = PVBM;
117undef $pvbm;
48f8bad9 118ok !defined $pvbm;