Commit | Line | Data |
c963b151 |
1 | #!./perl |
2 | |
3 | # Test // and friends. |
4 | |
5 | BEGIN { |
6 | chdir 't' if -d 't'; |
7 | @INC = '../lib'; |
8 | } |
9 | |
10 | package main; |
bc9b29db |
11 | use feature "err"; |
c963b151 |
12 | require './test.pl'; |
13 | |
bc9b29db |
14 | plan( tests => 35 ); |
c963b151 |
15 | |
16 | my($x); |
17 | |
18 | $x=1; |
19 | is($x // 0, 1, ' // : left-hand operand defined'); |
20 | |
21 | $x = undef; |
22 | is($x // 1, 1, ' // : left-hand operand undef'); |
23 | |
24 | $x=''; |
25 | is($x // 0, '', ' // : left-hand operand defined but empty'); |
26 | |
121d9cec |
27 | like([] // 0, qr/^ARRAY/, ' // : left-hand operand a referece'); |
28 | |
c963b151 |
29 | $x=1; |
30 | is(($x err 0), 1, ' err: left-hand operand defined'); |
31 | |
32 | $x = undef; |
33 | is(($x err 1), 1, ' err: left-hand operand undef'); |
34 | |
35 | $x=''; |
36 | is(($x err 0), '', ' err: left-hand operand defined but empty'); |
37 | |
121d9cec |
38 | like(([] err 0), qr/^ARRAY/, ' err: left-hand operand a referece'); |
39 | |
c963b151 |
40 | $x=undef; |
41 | $x //= 1; |
42 | is($x, 1, ' //=: left-hand operand undefined'); |
43 | |
44 | $x //= 0; |
121d9cec |
45 | is($x, 1, '//=: left-hand operand defined'); |
c963b151 |
46 | |
47 | $x = ''; |
48 | $x //= 0; |
121d9cec |
49 | is($x, '', '//=: left-hand operand defined but empty'); |
6f33ba73 |
50 | |
51 | @ARGV = (undef, 0, 3); |
52 | is(shift // 7, 7, 'shift // ... works'); |
53 | is(shift() // 7, 0, 'shift() // ... works'); |
54 | is(shift @ARGV // 7, 3, 'shift @array // ... works'); |
55 | |
56 | @ARGV = (3, 0, undef); |
57 | is(pop // 7, 7, 'pop // ... works'); |
58 | is(pop() // 7, 0, 'pop() // ... works'); |
59 | is(pop @ARGV // 7, 3, 'pop @array // ... works'); |
60 | |
61 | # Test that various syntaxes are allowed |
62 | |
63 | for (qw(getc pos readline readlink undef umask <> <FOO> <$foo> -f)) { |
64 | eval "sub { $_ // 0 }"; |
65 | is($@, '', "$_ // ... compiles"); |
66 | } |
7ce6e6b9 |
67 | |
68 | # Test for some ambiguous syntaxes |
69 | |
70 | eval q# sub f ($) { } f $x / 2; #; |
71 | is( $@, '' ); |
72 | eval q# sub f ($):lvalue { $y } f $x /= 2; #; |
73 | is( $@, '' ); |
74 | eval q# sub f ($) { } f $x /2; #; |
75 | like( $@, qr/^Search pattern not terminated/ ); |
76 | eval q# sub { print $fh / 2 } #; |
77 | is( $@, '' ); |
78 | eval q# sub { print $fh /2 } #; |
79 | like( $@, qr/^Search pattern not terminated/ ); |
75cc09e4 |
80 | |
81 | # [perl #28123] Perl optimizes // away incorrectly |
82 | |
83 | is(0 // 2, 0, ' // : left-hand operand not optimized away'); |
84 | is('' // 2, '', ' // : left-hand operand not optimized away'); |
85 | is(undef // 2, 2, ' // : left-hand operand optimized away'); |