Lots of consting
[p5sagit/p5-mst-13.2.git] / t / op / dor.t
CommitLineData
c963b151 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
7fc307b5 13plan( tests => 41 );
c963b151 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
121d9cec 26like([] // 0, qr/^ARRAY/, ' // : left-hand operand a referece');
27
c963b151 28$x=1;
29is(($x err 0), 1, ' err: left-hand operand defined');
30
31$x = undef;
32is(($x err 1), 1, ' err: left-hand operand undef');
33
34$x='';
35is(($x err 0), '', ' err: left-hand operand defined but empty');
36
121d9cec 37like(([] err 0), qr/^ARRAY/, ' err: left-hand operand a referece');
38
c963b151 39$x=undef;
40$x //= 1;
41is($x, 1, ' //=: left-hand operand undefined');
42
43$x //= 0;
121d9cec 44is($x, 1, '//=: left-hand operand defined');
c963b151 45
46$x = '';
47$x //= 0;
121d9cec 48is($x, '', '//=: left-hand operand defined but empty');
6f33ba73 49
50@ARGV = (undef, 0, 3);
51is(shift // 7, 7, 'shift // ... works');
52is(shift() // 7, 0, 'shift() // ... works');
53is(shift @ARGV // 7, 3, 'shift @array // ... works');
54
55@ARGV = (3, 0, undef);
56is(pop // 7, 7, 'pop // ... works');
57is(pop() // 7, 0, 'pop() // ... works');
58is(pop @ARGV // 7, 3, 'pop @array // ... works');
59
60# Test that various syntaxes are allowed
61
62for (qw(getc pos readline readlink undef umask <> <FOO> <$foo> -f)) {
63 eval "sub { $_ // 0 }";
64 is($@, '', "$_ // ... compiles");
65}
7ce6e6b9 66
67# Test for some ambiguous syntaxes
68
69eval q# sub f ($) { } f $x / 2; #;
70is( $@, '' );
71eval q# sub f ($):lvalue { $y } f $x /= 2; #;
72is( $@, '' );
73eval q# sub f ($) { } f $x /2; #;
74like( $@, qr/^Search pattern not terminated/ );
75eval q# sub { print $fh / 2 } #;
76is( $@, '' );
77eval q# sub { print $fh /2 } #;
78like( $@, qr/^Search pattern not terminated/ );
75cc09e4 79
80# [perl #28123] Perl optimizes // away incorrectly
81
82is(0 // 2, 0, ' // : left-hand operand not optimized away');
83is('' // 2, '', ' // : left-hand operand not optimized away');
84is(undef // 2, 2, ' // : left-hand operand optimized away');
7fc307b5 85
86# [perl #32347] err should be a weak keyword
87
88package weakerr;
89
90sub err { "<@_>" }
91::is( (shift() err 42), 42, 'err as an operator' );
92::is( (shift err 42), 42, 'err as an operator, with ambiguity' );
93::is( (err 2), "<2>", 'err as a function without parens' );
94::is( err(2, 3), "<2 3>", 'err as a function with parens' );
95::is( err(), "<>", 'err as a function without arguments' );
96::is( err, "<>", 'err as a function without parens' );