This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
hashassign.t: Test undef explicitly
[perl5.git] / t / op / dor.t
CommitLineData
c963b151
BD
1#!./perl
2
3# Test // and friends.
4
5BEGIN {
6 chdir 't' if -d 't';
7 @INC = '../lib';
8}
9
10package main;
11require './test.pl';
12
f23102e2 13plan( tests => 31 );
c963b151
BD
14
15my($x);
16
17$x=1;
18is($x // 0, 1, ' // : left-hand operand defined');
19
20$x = undef;
21is($x // 1, 1, ' // : left-hand operand undef');
22
23$x='';
24is($x // 0, '', ' // : left-hand operand defined but empty');
25
1c2e8cca 26like([] // 0, qr/^ARRAY/, ' // : left-hand operand a reference');
121d9cec 27
c963b151
BD
28$x=undef;
29$x //= 1;
30is($x, 1, ' //=: left-hand operand undefined');
31
32$x //= 0;
121d9cec 33is($x, 1, '//=: left-hand operand defined');
c963b151
BD
34
35$x = '';
36$x //= 0;
121d9cec 37is($x, '', '//=: left-hand operand defined but empty');
6f33ba73
RGS
38
39@ARGV = (undef, 0, 3);
40is(shift // 7, 7, 'shift // ... works');
41is(shift() // 7, 0, 'shift() // ... works');
42is(shift @ARGV // 7, 3, 'shift @array // ... works');
43
44@ARGV = (3, 0, undef);
45is(pop // 7, 7, 'pop // ... works');
46is(pop() // 7, 0, 'pop() // ... works');
47is(pop @ARGV // 7, 3, 'pop @array // ... works');
48
49# Test that various syntaxes are allowed
50
51for (qw(getc pos readline readlink undef umask <> <FOO> <$foo> -f)) {
52 eval "sub { $_ // 0 }";
53 is($@, '', "$_ // ... compiles");
54}
7ce6e6b9
RGS
55
56# Test for some ambiguous syntaxes
57
58eval q# sub f ($) { } f $x / 2; #;
59is( $@, '' );
60eval q# sub f ($):lvalue { $y } f $x /= 2; #;
61is( $@, '' );
62eval q# sub f ($) { } f $x /2; #;
63like( $@, qr/^Search pattern not terminated/ );
64eval q# sub { print $fh / 2 } #;
65is( $@, '' );
66eval q# sub { print $fh /2 } #;
67like( $@, qr/^Search pattern not terminated/ );
75cc09e4
MHM
68
69# [perl #28123] Perl optimizes // away incorrectly
70
71is(0 // 2, 0, ' // : left-hand operand not optimized away');
72is('' // 2, '', ' // : left-hand operand not optimized away');
73is(undef // 2, 2, ' // : left-hand operand optimized away');