Commit | Line | Data |
---|---|---|
8d063cd8 LW |
1 | #!./perl |
2 | ||
138b8230 | 3 | BEGIN { |
bf253806 | 4 | chdir 't'; |
fc39925c FC |
5 | @INC = "../lib"; |
6 | require "./test.pl"; | |
138b8230 GG |
7 | } |
8 | ||
fc39925c | 9 | plan(26); |
8d063cd8 | 10 | |
138b8230 GG |
11 | my $tmpfile = tempfile(); |
12 | open (tmp,'>', $tmpfile) || die "Can't create Cmd_while.tmp."; | |
8d063cd8 LW |
13 | print tmp "tvi925\n"; |
14 | print tmp "tvi920\n"; | |
15 | print tmp "vt100\n"; | |
16 | print tmp "Amiga\n"; | |
17 | print tmp "paper\n"; | |
d1e4d418 | 18 | close tmp or die "Could not close: $!"; |
8d063cd8 LW |
19 | |
20 | # test "last" command | |
21 | ||
138b8230 | 22 | open(fh, $tmpfile) || die "Can't open Cmd_while.tmp."; |
8d063cd8 LW |
23 | while (<fh>) { |
24 | last if /vt100/; | |
25 | } | |
138b8230 | 26 | ok(!eof && /vt100/); |
8d063cd8 LW |
27 | |
28 | # test "next" command | |
29 | ||
30 | $bad = ''; | |
138b8230 | 31 | open(fh, $tmpfile) || die "Can't open Cmd_while.tmp."; |
8d063cd8 LW |
32 | while (<fh>) { |
33 | next if /vt100/; | |
34 | $bad = 1 if /vt100/; | |
35 | } | |
138b8230 | 36 | ok(eof && !/vt100/ && !$bad); |
8d063cd8 LW |
37 | |
38 | # test "redo" command | |
39 | ||
40 | $bad = ''; | |
138b8230 | 41 | open(fh,$tmpfile) || die "Can't open Cmd_while.tmp."; |
8d063cd8 LW |
42 | while (<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 | 50 | ok(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 | 57 | open(fh,$tmpfile) || die "Can't open Cmd_while.tmp."; |
8d063cd8 LW |
58 | line: while (<fh>) { |
59 | if (/vt100/) {last line;} | |
60 | } continue { | |
61 | $badcont = 1 if /vt100/; | |
62 | } | |
138b8230 GG |
63 | ok(!eof && /vt100/); |
64 | ok(!$badcont); | |
8d063cd8 LW |
65 | |
66 | # test "next" command | |
67 | ||
68 | $bad = ''; | |
69 | $badcont = 1; | |
138b8230 | 70 | open(fh,$tmpfile) || die "Can't open Cmd_while.tmp."; |
8d063cd8 LW |
71 | entry: while (<fh>) { |
72 | next entry if /vt100/; | |
73 | $bad = 1 if /vt100/; | |
74 | } continue { | |
75 | $badcont = '' if /vt100/; | |
76 | } | |
138b8230 GG |
77 | ok(eof && !/vt100/ && !$bad); |
78 | ok(!$badcont); | |
8d063cd8 LW |
79 | |
80 | # test "redo" command | |
81 | ||
82 | $bad = ''; | |
83 | $badcont = ''; | |
138b8230 | 84 | open(fh,$tmpfile) || die "Can't open Cmd_while.tmp."; |
8d063cd8 LW |
85 | loop: 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 |
95 | ok(eof && !$bad); |
96 | ok(!$badcont); | |
8d063cd8 | 97 | |
bbad3607 | 98 | close(fh) || die "Can't close Cmd_while.tmp."; |
8d063cd8 LW |
99 | |
100 | $i = 9; | |
101 | { | |
102 | $i++; | |
103 | } | |
138b8230 | 104 | is($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/; |
109 | WHILE: | |
110 | while (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 | 121 | is($` . $& . $', "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 | |
181 | sub 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 | |
218 | fresh_perl_is <<'72406', "foobar\n", {}, | |
219 | { package o; use overload bool => sub { die unless $::ok++; return 1 } } | |
220 | use constant OK => bless [], o::; | |
221 | do{print("foobar\n");}until OK; | |
222 | 72406 | |
223 | "[perl #72406] segv with do{}until CONST where const is not folded"; |