This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Get t/uni/cache.t working under minitest
[perl5.git] / t / op / while.t
CommitLineData
8d063cd8
LW
1#!./perl
2
138b8230 3BEGIN {
bf253806 4 chdir 't';
fc39925c
FC
5 @INC = "../lib";
6 require "./test.pl";
138b8230
GG
7}
8
fc39925c 9plan(26);
8d063cd8 10
138b8230
GG
11my $tmpfile = tempfile();
12open (tmp,'>', $tmpfile) || die "Can't create Cmd_while.tmp.";
8d063cd8
LW
13print tmp "tvi925\n";
14print tmp "tvi920\n";
15print tmp "vt100\n";
16print tmp "Amiga\n";
17print tmp "paper\n";
d1e4d418 18close tmp or die "Could not close: $!";
8d063cd8
LW
19
20# test "last" command
21
138b8230 22open(fh, $tmpfile) || die "Can't open Cmd_while.tmp.";
8d063cd8
LW
23while (<fh>) {
24 last if /vt100/;
25}
138b8230 26ok(!eof && /vt100/);
8d063cd8
LW
27
28# test "next" command
29
30$bad = '';
138b8230 31open(fh, $tmpfile) || die "Can't open Cmd_while.tmp.";
8d063cd8
LW
32while (<fh>) {
33 next if /vt100/;
34 $bad = 1 if /vt100/;
35}
138b8230 36ok(eof && !/vt100/ && !$bad);
8d063cd8
LW
37
38# test "redo" command
39
40$bad = '';
138b8230 41open(fh,$tmpfile) || die "Can't open Cmd_while.tmp.";
8d063cd8
LW
42while (<fh>) {
43 if (s/vt100/VT100/g) {
44 s/VT100/Vt100/g;
45 redo;
46 }
47 $bad = 1 if /vt100/;
48 $bad = 1 if /VT100/;
49}
138b8230 50ok(eof && !$bad);
8d063cd8
LW
51
52# now do the same with a label and a continue block
53
54# test "last" command
55
56$badcont = '';
138b8230 57open(fh,$tmpfile) || die "Can't open Cmd_while.tmp.";
8d063cd8
LW
58line: while (<fh>) {
59 if (/vt100/) {last line;}
60} continue {
61 $badcont = 1 if /vt100/;
62}
138b8230
GG
63ok(!eof && /vt100/);
64ok(!$badcont);
8d063cd8
LW
65
66# test "next" command
67
68$bad = '';
69$badcont = 1;
138b8230 70open(fh,$tmpfile) || die "Can't open Cmd_while.tmp.";
8d063cd8
LW
71entry: while (<fh>) {
72 next entry if /vt100/;
73 $bad = 1 if /vt100/;
74} continue {
75 $badcont = '' if /vt100/;
76}
138b8230
GG
77ok(eof && !/vt100/ && !$bad);
78ok(!$badcont);
8d063cd8
LW
79
80# test "redo" command
81
82$bad = '';
83$badcont = '';
138b8230 84open(fh,$tmpfile) || die "Can't open Cmd_while.tmp.";
8d063cd8
LW
85loop: while (<fh>) {
86 if (s/vt100/VT100/g) {
87 s/VT100/Vt100/g;
88 redo loop;
89 }
90 $bad = 1 if /vt100/;
91 $bad = 1 if /VT100/;
92} continue {
93 $badcont = 1 if /vt100/;
94}
138b8230
GG
95ok(eof && !$bad);
96ok(!$badcont);
8d063cd8 97
bbad3607 98close(fh) || die "Can't close Cmd_while.tmp.";
8d063cd8
LW
99
100$i = 9;
101{
102 $i++;
103}
138b8230 104is($i, 10);
2158dd9b
GS
105
106# Check curpm is reset when jumping out of a scope
138b8230 107$i = 0;
2158dd9b
GS
108'abc' =~ /b/;
109WHILE:
110while (1) {
111 $i++;
138b8230 112 is($` . $& . $', "abc");
2158dd9b
GS
113 { # Localize changes to $` and friends
114 'end' =~ /end/;
138b8230
GG
115 redo WHILE if $i == 1;
116 next WHILE if $i == 2;
117 # 3 do a normal loop
118 last WHILE if $i == 4;
2158dd9b
GS
119 }
120}
138b8230 121is($` . $& . $', "abc");
1ee98724
GS
122
123# check that scope cleanup happens right when there's a continue block
124{
125 my $var = 16;
138b8230 126 my (@got_var, @got_i);
1ee98724
GS
127 while (my $i = ++$var) {
128 next if $i == 17;
129 last if $i > 17;
130 my $i = 0;
131 }
132 continue {
138b8230 133 ($got_var, $got_i) = ($var, $i);
1ee98724 134 }
138b8230
GG
135 is($got_var, 17);
136 is($got_i, 17);
1ee98724 137}
87246558
GS
138
139{
138b8230 140 my $got_l;
87246558
GS
141 local $l = 18;
142 {
143 local $l = 0
144 }
145 continue {
138b8230 146 $got_l = $l;
87246558 147 }
138b8230 148 is($got_l, 18);
87246558
GS
149}
150
151{
138b8230 152 my $got_l;
87246558
GS
153 local $l = 19;
154 my $x = 0;
155 while (!$x++) {
156 local $l = 0
157 }
158 continue {
138b8230 159 $got_l = $l;
87246558 160 }
138b8230 161 is($got_l, $l);
87246558 162}
1ba6ee2b 163
1ba6ee2b 164{
138b8230
GG
165 my $ok = 1;
166 $i = 20;
1ba6ee2b
GS
167 while (1) {
168 my $x;
138b8230 169 $ok = 0 if defined $x;
1ba6ee2b
GS
170 if ($i == 21) {
171 next;
172 }
173 last;
174 }
175 continue {
138b8230 176 ++$i;
1ba6ee2b 177 }
138b8230 178 ok($ok);
1ba6ee2b 179}
5e17f11b
GG
180
181sub save_context { $_[0] = wantarray; $_[1] }
182
183{
184 my $context = -1;
185 my $p = sub {
186 my $x = 1;
187 while ($x--) {
188 save_context($context, "foo");
189 }
190 };
191 is(scalar($p->()), 0);
192 is($context, undef, "last statement in while block has 'void' context");
193}
194
195{
196 my $context = -1;
197 my $p = sub {
198 my $x = 1;
199 {
200 save_context($context, "foo");
201 }
202 };
203 is(scalar($p->()), "foo");
204 is($context, "", "last statement in block has 'scalar' context");
205}
206
207{
208 # test scope is cleaned
209 my $i = 0;
210 my @a;
211 while ($i++ < 2) {
212 my $x;
213 push @a, \$x;
214 }
215 ok($a[0] ne $a[1]);
216}
fc39925c
FC
217
218fresh_perl_is <<'72406', "foobar\n", {},
219{ package o; use overload bool => sub { die unless $::ok++; return 1 } }
220use constant OK => bless [], o::;
221do{print("foobar\n");}until OK;
22272406
223 "[perl #72406] segv with do{}until CONST where const is not folded";