This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In taint.t, add violates_taint(), to replace a repeated is()/like() pair.
[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
d47e1c27 13plan 40;
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
110# modify the hash. To them, the hash should appear empty.
111
2f86008e
DM
112%hash = (
113 key1 => bless({}, 'X'),
114 key2 => bless({}, 'X'),
115);
116undef %hash;
117sub X::DESTROY {
48f8bad9
NC
118 is scalar keys %hash, 0;
119 is scalar values %hash, 0;
120 my @l = each %hash;
121 is @l, 0;
122 is delete $hash{'key2'}, undef;
2f86008e 123}
6e592b3a
BM
124
125# this will segfault if it fails
126
127sub PVBM () { 'foo' }
128{ my $dummy = index 'foo', PVBM }
129
130my $pvbm = PVBM;
131undef $pvbm;
48f8bad9 132ok !defined $pvbm;