This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Initial attempt at feature 'try'
[perl5.git] / t / op / flip.t
CommitLineData
8d063cd8
LW
1#!./perl
2
5376f8ad 3BEGIN {
b5efbd1f 4 chdir 't' if -d 't';
1ae3d757 5 require "./test.pl";
5376f8ad 6}
88587957 7
1e2dd519 8plan(14);
8d063cd8
LW
9
10@a = (1,2,3,4,5,6,7,8,9,10,11,12);
5376f8ad 11@b = ();
c6aa4a32 12while ($_ = shift(@a)) {
5376f8ad 13 if ($x = /4/../8/) { $z = $x; push @b, $x + 0; }
8d063cd8
LW
14 $y .= /1/../2/;
15}
5376f8ad 16is(join("*", @b), "1*2*3*4*5");
8d063cd8 17
5376f8ad 18is($z, '5E0');
8d063cd8 19
5376f8ad 20is($y, '12E0123E0');
8d063cd8
LW
21
22@a = ('a','b','c','d','e','f','g');
23
4e3399f9
YST
24{
25local $.;
26
2eb25c99 27open(of,'harness') or die "Can't open harness: $!";
8d063cd8 28while (<of>) {
a0d0e21e 29 (3 .. 5) && ($foo .= $_);
8d063cd8
LW
30}
31$x = ($foo =~ y/\n/\n/);
32
5376f8ad 33is($x, 3);
3e3baf6d
TB
34
35$x = 3.14;
5376f8ad 36ok(($x...$x) eq "1");
790090df
HS
37
38{
ee95e30c 39 # coredump reported in bug 20001018.008 (#4474)
790090df
HS
40 readline(UNKNOWN);
41 $. = 1;
4e3399f9 42 $x = 1..10;
5376f8ad 43 ok(1);
4e3399f9
YST
44}
45
790090df 46}
4e3399f9 47
5376f8ad 48ok(!defined $.);
4e3399f9
YST
49
50use warnings;
51my $warn='';
52$SIG{__WARN__} = sub { $warn .= join '', @_ };
53
5376f8ad 54ok(scalar(0..2));
4e3399f9 55
5376f8ad 56like($warn, qr/uninitialized/);
4e3399f9
YST
57$warn = '';
58
59$x = "foo".."bar";
60
5376f8ad 61ok((() = ($warn =~ /isn't numeric/g)) == 2);
4e3399f9
YST
62$warn = '';
63
64$. = 15;
5376f8ad 65ok(scalar(15..0));
14d91147
FC
66
67push @_, \scalar(0..0) for 1,2;
68isnt $_[0], $_[1], '\scalar($a..$b) gives a different scalar each time';
00e40766
FC
69
70# This evil little example from ticket #122829 abused the fact that each
71# recursion level maintained its own flip-flip state. The following com-
72# ment describes how it *used* to work.
73
74# This routine maintains multiple flip-flop states, each with its own
75# numeric ID, starting from 1. Pass the ID as the argument.
76sub f {
77 my $depth = shift() - 1;
78 return f($depth) if $depth;
79 return /3/../5/;
80}
81{
82 my $accumulator;
83 for(1..20) {
84 if (f(1)) {
85 my $outer = $_;
86 for(1..10){
87 $accumulator .= "$outer $_\n" if f(2);
88 }
89 }
90 }
91 is $accumulator, <<EOT, 'recursion shares state';
923 1
933 2
943 3
953 4
963 5
9713 1
9813 2
9913 3
10013 4
10113 5
102EOT
103}
1e2dd519
FC
104
105# Void context gives parenthesized lhs scalar context
106no warnings 'void';
107sub c { $context = qw[ void scalar list ][wantarray + defined wantarray] }
108(c())x34;
109is $context, 'scalar', '(...)x... in void context';