18 is($x // 0, 1, ' // : left-hand operand defined');
21 is($x // 1, 1, ' // : left-hand operand undef');
24 is($x // 0, '', ' // : left-hand operand defined but empty');
26 like([] // 0, qr/^ARRAY/, ' // : left-hand operand a reference');
30 is($x, 1, ' //=: left-hand operand undefined');
33 is($x, 1, '//=: left-hand operand defined');
37 is($x, '', '//=: left-hand operand defined but empty');
39 @ARGV = (undef, 0, 3);
40 is(shift // 7, 7, 'shift // ... works');
41 is(shift() // 7, 0, 'shift() // ... works');
42 is(shift @ARGV // 7, 3, 'shift @array // ... works');
44 @ARGV = (3, 0, undef);
45 is(pop // 7, 7, 'pop // ... works');
46 is(pop() // 7, 0, 'pop() // ... works');
47 is(pop @ARGV // 7, 3, 'pop @array // ... works');
49 # Test that various syntaxes are allowed
51 for (qw(getc pos readline readlink undef umask <> <FOO> <$foo> -f)) {
52 eval "sub { $_ // 0 }";
53 is($@, '', "$_ // ... compiles");
56 # Test for some ambiguous syntaxes
58 eval q# sub f ($) { } f $x / 2; #;
59 is( $@, '', "'/' correctly parsed as arithmetic operator" );
60 eval q# sub f ($):lvalue { $y } f $x /= 2; #;
61 is( $@, '', "'/=' correctly parsed as assigment operator" );
62 eval q# sub f ($) { } f $x /2; #;
63 like( $@, qr/^Search pattern not terminated/,
64 "Caught unterminated search pattern error message: empty subroutine" );
65 eval q# sub { print $fh / 2 } #;
67 "'/' correctly parsed as arithmetic operator in sub with built-in function" );
68 eval q# sub { print $fh /2 } #;
69 like( $@, qr/^Search pattern not terminated/,
70 "Caught unterminated search pattern error message: sub with built-in function" );
72 # [perl #28123] Perl optimizes // away incorrectly
74 is(0 // 2, 0, ' // : left-hand operand not optimized away');
75 is('' // 2, '', ' // : left-hand operand not optimized away');
76 is(undef // 2, 2, ' // : left-hand operand optimized away');
78 # Test that OP_DORs other branch isn't run when arg is defined
79 # // returns the value if its defined, and we must test its
85 is($y, 0, 'y is still 0 after "$x // 1 and $y = 1"');
88 # $x is defined, so its value 0 is returned to the if block
89 # and the block is skipped
93 is($y, 0, 'if ($x // 1) exited out early since $x is defined and 0');
95 # This is actually (($x // $z) || 'cat'), so 0 from first dor
96 # evaluates false, we should see 'cat'.
99 $y = $x // $z || 'cat';
100 is($y, 'cat', 'chained or/dor behaves correctly');