This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: Change 29723 breaks t/op/inccode-tie.t on Win32
[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;
bc9b29db 11use feature "err";
c963b151
BD
12require './test.pl';
13
bc9b29db 14plan( tests => 35 );
c963b151
BD
15
16my($x);
17
18$x=1;
19is($x // 0, 1, ' // : left-hand operand defined');
20
21$x = undef;
22is($x // 1, 1, ' // : left-hand operand undef');
23
24$x='';
25is($x // 0, '', ' // : left-hand operand defined but empty');
26
121d9cec
NC
27like([] // 0, qr/^ARRAY/, ' // : left-hand operand a referece');
28
c963b151
BD
29$x=1;
30is(($x err 0), 1, ' err: left-hand operand defined');
31
32$x = undef;
33is(($x err 1), 1, ' err: left-hand operand undef');
34
35$x='';
36is(($x err 0), '', ' err: left-hand operand defined but empty');
37
121d9cec
NC
38like(([] err 0), qr/^ARRAY/, ' err: left-hand operand a referece');
39
c963b151
BD
40$x=undef;
41$x //= 1;
42is($x, 1, ' //=: left-hand operand undefined');
43
44$x //= 0;
121d9cec 45is($x, 1, '//=: left-hand operand defined');
c963b151
BD
46
47$x = '';
48$x //= 0;
121d9cec 49is($x, '', '//=: left-hand operand defined but empty');
6f33ba73
RGS
50
51@ARGV = (undef, 0, 3);
52is(shift // 7, 7, 'shift // ... works');
53is(shift() // 7, 0, 'shift() // ... works');
54is(shift @ARGV // 7, 3, 'shift @array // ... works');
55
56@ARGV = (3, 0, undef);
57is(pop // 7, 7, 'pop // ... works');
58is(pop() // 7, 0, 'pop() // ... works');
59is(pop @ARGV // 7, 3, 'pop @array // ... works');
60
61# Test that various syntaxes are allowed
62
63for (qw(getc pos readline readlink undef umask <> <FOO> <$foo> -f)) {
64 eval "sub { $_ // 0 }";
65 is($@, '', "$_ // ... compiles");
66}
7ce6e6b9
RGS
67
68# Test for some ambiguous syntaxes
69
70eval q# sub f ($) { } f $x / 2; #;
71is( $@, '' );
72eval q# sub f ($):lvalue { $y } f $x /= 2; #;
73is( $@, '' );
74eval q# sub f ($) { } f $x /2; #;
75like( $@, qr/^Search pattern not terminated/ );
76eval q# sub { print $fh / 2 } #;
77is( $@, '' );
78eval q# sub { print $fh /2 } #;
79like( $@, qr/^Search pattern not terminated/ );
75cc09e4
MHM
80
81# [perl #28123] Perl optimizes // away incorrectly
82
83is(0 // 2, 0, ' // : left-hand operand not optimized away');
84is('' // 2, '', ' // : left-hand operand not optimized away');
85is(undef // 2, 2, ' // : left-hand operand optimized away');