| 1 | #!./perl |
| 2 | |
| 3 | # Test // and friends. |
| 4 | |
| 5 | BEGIN { |
| 6 | chdir 't' if -d 't'; |
| 7 | @INC = '../lib'; |
| 8 | } |
| 9 | |
| 10 | package main; |
| 11 | require './test.pl'; |
| 12 | |
| 13 | plan( tests => 31 ); |
| 14 | |
| 15 | my($x); |
| 16 | |
| 17 | $x=1; |
| 18 | is($x // 0, 1, ' // : left-hand operand defined'); |
| 19 | |
| 20 | $x = undef; |
| 21 | is($x // 1, 1, ' // : left-hand operand undef'); |
| 22 | |
| 23 | $x=''; |
| 24 | is($x // 0, '', ' // : left-hand operand defined but empty'); |
| 25 | |
| 26 | like([] // 0, qr/^ARRAY/, ' // : left-hand operand a referece'); |
| 27 | |
| 28 | $x=undef; |
| 29 | $x //= 1; |
| 30 | is($x, 1, ' //=: left-hand operand undefined'); |
| 31 | |
| 32 | $x //= 0; |
| 33 | is($x, 1, '//=: left-hand operand defined'); |
| 34 | |
| 35 | $x = ''; |
| 36 | $x //= 0; |
| 37 | is($x, '', '//=: left-hand operand defined but empty'); |
| 38 | |
| 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'); |
| 43 | |
| 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'); |
| 48 | |
| 49 | # Test that various syntaxes are allowed |
| 50 | |
| 51 | for (qw(getc pos readline readlink undef umask <> <FOO> <$foo> -f)) { |
| 52 | eval "sub { $_ // 0 }"; |
| 53 | is($@, '', "$_ // ... compiles"); |
| 54 | } |
| 55 | |
| 56 | # Test for some ambiguous syntaxes |
| 57 | |
| 58 | eval q# sub f ($) { } f $x / 2; #; |
| 59 | is( $@, '' ); |
| 60 | eval q# sub f ($):lvalue { $y } f $x /= 2; #; |
| 61 | is( $@, '' ); |
| 62 | eval q# sub f ($) { } f $x /2; #; |
| 63 | like( $@, qr/^Search pattern not terminated/ ); |
| 64 | eval q# sub { print $fh / 2 } #; |
| 65 | is( $@, '' ); |
| 66 | eval q# sub { print $fh /2 } #; |
| 67 | like( $@, qr/^Search pattern not terminated/ ); |
| 68 | |
| 69 | # [perl #28123] Perl optimizes // away incorrectly |
| 70 | |
| 71 | is(0 // 2, 0, ' // : left-hand operand not optimized away'); |
| 72 | is('' // 2, '', ' // : left-hand operand not optimized away'); |
| 73 | is(undef // 2, 2, ' // : left-hand operand optimized away'); |