Small optimisations, by Brandon Black
[p5sagit/p5-mst-13.2.git] / t / op / undef.t
CommitLineData
a687059c 1#!./perl
2
e03bd546 3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
e03bd546 6}
7
2f86008e 8print "1..36\n";
a687059c 9
10print defined($a) ? "not ok 1\n" : "ok 1\n";
11
12$a = 1+1;
13print defined($a) ? "ok 2\n" : "not ok 2\n";
14
15undef $a;
16print defined($a) ? "not ok 3\n" : "ok 3\n";
17
18$a = "hi";
19print defined($a) ? "ok 4\n" : "not ok 4\n";
20
21$a = $b;
22print defined($a) ? "not ok 5\n" : "ok 5\n";
23
24@ary = ("1arg");
25$a = pop(@ary);
26print defined($a) ? "ok 6\n" : "not ok 6\n";
27$a = pop(@ary);
28print defined($a) ? "not ok 7\n" : "ok 7\n";
29
30@ary = ("1arg");
31$a = shift(@ary);
32print defined($a) ? "ok 8\n" : "not ok 8\n";
33$a = shift(@ary);
34print defined($a) ? "not ok 9\n" : "ok 9\n";
35
36$ary{'foo'} = 'hi';
37print defined($ary{'foo'}) ? "ok 10\n" : "not ok 10\n";
38print defined($ary{'bar'}) ? "not ok 11\n" : "ok 11\n";
39undef $ary{'foo'};
40print defined($ary{'foo'}) ? "not ok 12\n" : "ok 12\n";
41
42print defined(@ary) ? "ok 13\n" : "not ok 13\n";
43print defined(%ary) ? "ok 14\n" : "not ok 14\n";
44undef @ary;
45print defined(@ary) ? "not ok 15\n" : "ok 15\n";
46undef %ary;
47print defined(%ary) ? "not ok 16\n" : "ok 16\n";
48@ary = (1);
fe14fcc3 49print defined @ary ? "ok 17\n" : "not ok 17\n";
a687059c 50%ary = (1,1);
51print defined %ary ? "ok 18\n" : "not ok 18\n";
52
53sub foo { print "ok 19\n"; }
54
55&foo || print "not ok 19\n";
56
57print defined &foo ? "ok 20\n" : "not ok 20\n";
58undef &foo;
59print defined(&foo) ? "not ok 21\n" : "ok 21\n";
a3f914c5 60
61eval { undef $1 };
62print $@ =~ /^Modification of a read/ ? "ok 22\n" : "not ok 22\n";
63
64eval { $1 = undef };
65print $@ =~ /^Modification of a read/ ? "ok 23\n" : "not ok 23\n";
66
659eaf73 67{
68 require Tie::Hash;
69 tie my %foo, 'Tie::StdHash';
70 print defined %foo ? "ok 24\n" : "not ok 24\n";
71 %foo = ( a => 1 );
72 print defined %foo ? "ok 25\n" : "not ok 25\n";
73}
74
75{
76 require Tie::Array;
77 tie my @foo, 'Tie::StdArray';
78 print defined @foo ? "ok 26\n" : "not ok 26\n";
79 @foo = ( a => 1 );
80 print defined @foo ? "ok 27\n" : "not ok 27\n";
81}
3d387947 82
83{
84 # [perl #17753] segfault when undef'ing unquoted string constant
85 eval 'undef tcp';
86 print $@ =~ /^Can't modify constant item/ ? "ok 28\n" : "not ok 28\n";
87}
2f86008e 88
89# bugid 3096
90# undefing a hash may free objects with destructors that then try to
91# modify the hash. To them, the hash should appear empty.
92
93$test = 29;
94%hash = (
95 key1 => bless({}, 'X'),
96 key2 => bless({}, 'X'),
97);
98undef %hash;
99sub X::DESTROY {
100 print "not " if keys %hash; print "ok $test\n"; $test++;
101 print "not " if values %hash; print "ok $test\n"; $test++;
102 print "not " if each %hash; print "ok $test\n"; $test++;
103 print "not " if defined delete $hash{'key2'}; print "ok $test\n"; $test++;
104}