This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #122829] Flip-flop under recursion
[perl5.git] / t / base / lex.t
CommitLineData
8d063cd8
LW
1#!./perl
2
8380b690 3print "1..93\n";
8d063cd8 4
79072805 5$x = 'x';
8d063cd8 6
79072805
LW
7print "#1 :$x: eq :x:\n";
8if ($x eq 'x') {print "ok 1\n";} else {print "not ok 1\n";}
8d063cd8 9
1a9b3510 10$x = $#[0];
8d063cd8
LW
11
12if ($x eq '') {print "ok 2\n";} else {print "not ok 2\n";}
13
14$x = $#x;
15
16if ($x eq '-1') {print "ok 3\n";} else {print "not ok 3\n";}
17
18$x = '\\'; # ';
19
20if (length($x) == 1) {print "ok 4\n";} else {print "not ok 4\n";}
a559c259
LW
21
22eval 'while (0) {
23 print "foo\n";
24}
25/^/ && (print "ok 5\n");
26';
27
28eval '$foo{1} / 1;';
79072805 29if (!$@) {print "ok 6\n";} else {print "not ok 6 $@\n";}
378cc40b
LW
30
31eval '$foo = 123+123.4+123e4+123.4E5+123.4e+5+.12;';
32
33$foo = int($foo * 100 + .5);
87250799 34if ($foo eq 2591024652) {print "ok 7\n";} else {print "not ok 7 :$foo:\n";}
a687059c
LW
35
36print <<'EOF';
37ok 8
38EOF
39
40$foo = 'ok 9';
41print <<EOF;
42$foo
43EOF
44
45eval <<\EOE, print $@;
46print <<'EOF';
47ok 10
48EOF
49
50$foo = 'ok 11';
51print <<EOF;
52$foo
53EOF
54EOE
55
972e7321
MS
56print <<'EOS' . <<\EOF;
57ok 12 - make sure single quotes are honored \nnot ok
a687059c
LW
58EOS
59ok 13
60EOF
61
62print qq/ok 14\n/;
63print qq(ok 15\n);
64
65print qq
a0d0e21e 66[ok 16\n]
a687059c
LW
67;
68
69print q<ok 17
70>;
71
f3365a56
NC
72print "ok 18 - was the test for the deprecated use of bare << to mean <<\"\"\n";
73#print <<; # Yow!
74#ok 18
75#
76## previous line intentionally left blank.
79072805 77
2ba53c57
HS
78print <<E1 eq "foo\n\n" ? "ok 19\n" : "not ok 19\n";
79@{[ <<E2 ]}
80foo
81E2
82E1
83
84print <<E1 eq "foo\n\n" ? "ok 20\n" : "not ok 20\n";
85@{[
86 <<E2
87foo
88E2
89]}
90E1
91
79072805
LW
92$foo = FOO;
93$bar = BAR;
94$foo{$bar} = BAZ;
95$ary[0] = ABC;
96
2ba53c57 97print "$foo{$bar}" eq "BAZ" ? "ok 21\n" : "not ok 21\n";
79072805 98
2ba53c57
HS
99print "${foo}{$bar}" eq "FOO{BAR}" ? "ok 22\n" : "not ok 22\n";
100print "${foo{$bar}}" eq "BAZ" ? "ok 23\n" : "not ok 23\n";
79072805 101
2ba53c57
HS
102print "FOO:" =~ /$foo[:]/ ? "ok 24\n" : "not ok 24\n";
103print "ABC" =~ /^$ary[$A]$/ ? "ok 25\n" : "not ok 25\n";
104print "FOOZ" =~ /^$foo[$A-Z]$/ ? "ok 26\n" : "not ok 26\n";
1bcde0ca 105
f27ffc4a
GS
106# MJD 19980425
107($X, @X) = qw(a b c d);
108print "d" =~ /^$X[-1]$/ ? "ok 27\n" : "not ok 27\n";
109print "a1" !~ /^$X[-1]$/ ? "ok 28\n" : "not ok 28\n";
a2c06652 110
f27ffc4a
GS
111print (((q{{\{\(}} . q{{\)\}}}) eq '{{\(}{\)}}') ? "ok 29\n" : "not ok 29\n");
112
113
114$foo = "not ok 30\n";
a2c06652
HS
115$foo =~ s/^not /substr(<<EOF, 0, 0)/e;
116 Ignored
117EOF
118print $foo;
2b92dfce
GS
119
120# Tests for new extended control-character variables
121# MJD 19990227
122
6a4ad6ad
BF
123my $test = 31;
124
2b92dfce
GS
125{ my $CX = "\cX";
126 my $CXY ="\cXY";
127 $ {$CX} = 17;
128 $ {$CXY} = 23;
129 if ($ {^XY} != 23) { print "not " }
6a4ad6ad 130 print "ok $test\n"; $test++;
2b92dfce 131
2b92dfce
GS
132# Does the old UNBRACED syntax still do what it used to?
133 if ("$^XY" ne "17Y") { print "not " }
6a4ad6ad 134 print "ok $test\n"; $test++;
2b92dfce
GS
135
136 sub XX () { 6 }
766c8ce8
JH
137 $ {"\cQ\cXX"} = 119;
138 $^Q = 5; # This should be an unused ^Var.
2b92dfce
GS
139 $N = 5;
140 # The second caret here should be interpreted as an xor
766c8ce8 141 if (($^Q^XX) != 3) { print "not " }
6a4ad6ad 142 print "ok $test\n"; $test++;
2b92dfce 143# if (($N ^ XX()) != 3) { print "not " }
6a4ad6ad 144# print "ok $test\n"; $test++;
2b92dfce
GS
145
146 # These next two tests are trying to make sure that
15a8c21e 147 # $^FOO is always global; it doesn't make sense to 'my' it.
2b92dfce 148 #
0244c3a4 149
2b92dfce
GS
150 eval 'my $^X;';
151 print "not " unless index ($@, 'Can\'t use global $^X in "my"') > -1;
6a4ad6ad 152 print "ok $test\n"; $test++;
2b92dfce
GS
153# print "($@)\n" if $@;
154
155 eval 'my $ {^XYZ};';
156 print "not " unless index ($@, 'Can\'t use global $^XYZ in "my"') > -1;
6a4ad6ad 157 print "ok $test\n"; $test++;
2b92dfce
GS
158# print "($@)\n" if $@;
159
160# Now let's make sure that caret variables are all forced into the main package.
161 package Someother;
766c8ce8
JH
162 $^Q = 'Someother';
163 $ {^Quixote} = 'Someother 2';
2b92dfce
GS
164 $ {^M} = 'Someother 3';
165 package main;
766c8ce8 166 print "not " unless $^Q eq 'Someother';
6a4ad6ad 167 print "ok $test\n"; $test++;
766c8ce8 168 print "not " unless $ {^Quixote} eq 'Someother 2';
6a4ad6ad 169 print "ok $test\n"; $test++;
2b92dfce 170 print "not " unless $ {^M} eq 'Someother 3';
6a4ad6ad 171 print "ok $test\n"; $test++;
2b92dfce
GS
172
173
174}
175
0244c3a4 176# see if eval '', s///e, and heredocs mix
2b92dfce 177
0244c3a4
GS
178sub T {
179 my ($where, $num) = @_;
180 my ($p,$f,$l) = caller;
181 print "# $p:$f:$l vs /$where/\nnot " unless "$p:$f:$l" =~ /$where/;
182 print "ok $num\n";
183}
184
0244c3a4
GS
185{
186# line 42 "plink"
187 local $_ = "not ok ";
188 eval q{
189 s/^not /<<EOT/e and T '^main:\(eval \d+\):2$', $test++;
7d66e4bd 190# uggedaboudit
0244c3a4
GS
191EOT
192 print $_, $test++, "\n";
193 T('^main:\(eval \d+\):6$', $test++);
194# line 1 "plunk"
195 T('^main:plunk:1$', $test++);
196 };
197 print "# $@\nnot ok $test\n" if $@;
198 T '^main:plink:53$', $test++;
199}
8593bda5
GS
200
201# tests 47--51 start here
202# tests for new array interpolation semantics:
203# arrays now *always* interpolate into "..." strings.
204# 20000522 MJD (mjd@plover.com)
205{
8593bda5
GS
206 eval(q(">@nosuch<" eq "><")) || print "# $@", "not ";
207 print "ok $test\n";
208 ++$test;
209
210 # Look at this! This is going to be a common error in the future:
211 eval(q("fred@example.com" eq "fred.com")) || print "# $@", "not ";
212 print "ok $test\n";
213 ++$test;
214
215 # Let's make sure that normal array interpolation still works right
216 # For some reason, this appears not to be tested anywhere else.
217 my @a = (1,2,3);
218 print +((">@a<" eq ">1 2 3<") ? '' : 'not '), "ok $test\n";
219 ++$test;
220
221 # Ditto.
222 eval(q{@nosuch = ('a', 'b', 'c'); ">@nosuch<" eq ">a b c<"})
223 || print "# $@", "not ";
224 print "ok $test\n";
225 ++$test;
226
227 # This isn't actually a lex test, but it's testing the same feature
228 sub makearray {
229 my @array = ('fish', 'dog', 'carrot');
230 *R::crackers = \@array;
231 }
232
233 eval(q{makearray(); ">@R::crackers<" eq ">fish dog carrot<"})
234 || print "# $@", "not ";
235 print "ok $test\n";
236 ++$test;
237}
ce29ac45
JH
238
239# Tests 52-54
240# => should only quote foo::bar if it isn't a real sub. AMS, 20010621
241
242sub xyz::foo { "bar" }
243my %str = (
244 foo => 1,
245 xyz::foo => 1,
246 xyz::bar => 1,
247);
248
ce29ac45
JH
249print ((exists $str{foo} ? "" : "not ")."ok $test\n"); ++$test;
250print ((exists $str{bar} ? "" : "not ")."ok $test\n"); ++$test;
251print ((exists $str{xyz::bar} ? "" : "not ")."ok $test\n"); ++$test;
62444305
AE
252
253sub foo::::::bar { print "ok $test\n"; $test++ }
254foo::::::bar;
356c7adf
CJ
255
256eval "\$x =\xE2foo";
b1fc3636 257if ($@ =~ /Unrecognized character \\xE2; marked by <-- HERE after \$x =<-- HERE near column 5/) { print "ok $test\n"; } else { print "not ok $test\n"; }
356c7adf 258$test++;
df3467db
IG
259
260# Is "[~" scanned correctly?
f50fa36f
IG
261@a = (1,2,3);
262print "not " unless($a[~~2] == 3);
6a4ad6ad 263print "ok $test\n"; $test++;
565b52df
FC
264
265$_ = "";
6a4ad6ad
BF
266eval 's/(?:)/"ok $test" . "${\q||}".<<\END/e;
267 - heredoc after "" in s/// in eval
565b52df
FC
268END
269';
6a4ad6ad 270print $_ || "not ok $test\n"; $test++;
043cc6c6
FC
271
272$_ = "";
6a4ad6ad
BF
273eval 's|(?:)|"ok $test" . "${\<<\END}"
274 - heredoc in "" in multiline s///e in eval
043cc6c6
FC
275END
276|e
277';
6a4ad6ad 278print $_ || "not ok $test\n"; $test++;
62abd0d7
FC
279
280$_ = "";
281eval "s/(?:)/<<foo/e #\0
6a4ad6ad 282ok $test - null on same line as heredoc in s/// in eval
62abd0d7
FC
283foo
284";
6a4ad6ad 285print $_ || "not ok $test\n"; $test++;
99bd9d90
FC
286
287$_ = "";
288eval ' s/(?:)/"${\<<END}"/e;
6a4ad6ad 289ok $test - heredoc in "" in single-line s///e in eval
99bd9d90
FC
290END
291';
6a4ad6ad 292print $_ || "not ok $test\n"; $test++;
99bd9d90
FC
293
294$_ = "";
295s|(?:)|"${\<<END}"
6a4ad6ad 296ok $test - heredoc in "" in multiline s///e outside eval
99bd9d90
FC
297END
298|e;
6a4ad6ad 299print $_ || "not ok $test\n"; $test++;
7cc34111 300
6a4ad6ad 301$_ = "not ok $test - s/// in s/// pattern\n";
7cc34111 302s/${s|||;\""}not //;
6a4ad6ad 303print; $test++;
db444266
FC
304
305/(?{print <<END
6a4ad6ad 306ok $test - here-doc in re-eval
db444266 307END
6a4ad6ad 308})/; $test++;
db444266
FC
309
310eval '/(?{print <<END
6a4ad6ad 311ok $test - here-doc in re-eval in string eval
db444266 312END
6a4ad6ad 313})/'; $test++;
11076590 314
6a4ad6ad
BF
315eval 'print qq ;ok $test - eval ending with semicolon\n;'
316 or print "not ok $test - eval ending with semicolon\n"; $test++;
3328ab5a
FC
317
318print "not " unless qr/(?{<<END})/ eq '(?^:(?{<<END}))';
319foo
320END
6a4ad6ad 321print "ok $test - here-doc in single-line re-eval\n"; $test++;
3328ab5a
FC
322
323$_ = qr/(?{"${<<END}"
324foo
325END
326})/;
327print "not " unless /foo/;
6a4ad6ad 328print "ok $test - here-doc in quotes in multiline re-eval\n"; $test++;
76f9939e
FC
329
330eval 's//<<END/e if 0; $_ = "a
331END
332b"';
333print "not " if $_ =~ /\n\n/;
6a4ad6ad 334print "ok $test - eval 's//<<END/' does not leave extra newlines\n"; $test++;
9c74ccc9
FC
335
336$_ = a;
337eval "s/a/'b\0'#/e";
338print 'not ' unless $_ eq "b\0";
6a4ad6ad 339print "ok $test - # after null in s/// repl\n"; $test++;
9c74ccc9
FC
340
341s//"#" . <<END/e;
342foo
343END
6a4ad6ad 344print "ok $test - s//'#' . <<END/e\n"; $test++;
6b00f562
FC
345
346eval "s//3}->{3/e";
347print "not " unless $@;
6a4ad6ad 348print "ok $test - s//3}->{3/e\n"; $test++;
f777953f 349
6a4ad6ad 350$_ = "not ok $test";
f777953f
FC
351$x{3} = "not ";
352eval 's/${\%x}{3}//e';
6a4ad6ad 353print "$_ - s//\${\\%x}{3}/e\n"; $test++;
90a536e1
FC
354
355eval 's/${foo#}//e';
356print "not " unless $@;
6a4ad6ad 357print "ok $test - s/\${foo#}//e\n"; $test++;
819b004e
FC
358
359eval 'warn ({$_ => 1} + 1) if 0';
360print "not " if $@;
6a4ad6ad 361print "ok $test - listop({$_ => 1} + 1)\n"; $test++;
819b004e 362print "# $@" if $@;
c31f6d3b 363
c31f6d3b
FC
364for(qw< require goto last next redo dump >) {
365 eval "sub { $_ foo << 2 }";
366 print "not " if $@;
367 print "ok ", $test++, " - [perl #105924] $_ WORD << ...\n";
368 print "# $@" if $@;
369}
e9d2327d
FC
370
371# http://rt.perl.org/rt3/Ticket/Display.html?id=56880
372my $counter = 0;
373eval 'v23: $counter++; goto v23 unless $counter == 2';
374print "not " unless $counter == 2;
6a4ad6ad 375print "ok $test - Use v[0-9]+ as a label\n"; $test++;
e9d2327d
FC
376$counter = 0;
377eval 'v23 : $counter++; goto v23 unless $counter == 2';
378print "not " unless $counter == 2;
6a4ad6ad 379print "ok $test - Use v[0-9]+ as a label with space before colon\n"; $test++;
e9d2327d
FC
380
381my $output = "";
382eval "package v10::foo; sub test2 { return 'v10::foo' }
383 package v10; sub test { return v10::foo::test2(); }
384 package main; \$output = v10::test(); ";
385print "not " unless $output eq 'v10::foo';
6a4ad6ad 386print "ok $test - call a function in package v10::foo\n"; $test++;
e9d2327d
FC
387
388print "not " unless (1?v65:"bar") eq 'A';
6a4ad6ad 389print "ok $test - colon detection after vstring does not break ? vstring :\n"; $test++;
8b12970a
FC
390
391# Test pyoq ops with comments before the first delim
392q # comment
393 "b"#
394 eq 'b' or print "not ";
6a4ad6ad 395print "ok $test - q <comment> <newline> ...\n"; $test++;
8b12970a
FC
396qq # comment
397 "b"#
398 eq 'b' or print "not ";
6a4ad6ad 399print "ok $test - qq <comment> <newline> ...\n"; $test++;
8b12970a
FC
400qw # comment
401 "b"#
402 [0] eq 'b' or print "not ";
6a4ad6ad 403print "ok $test - qw <comment> <newline> ...\n"; $test++;
8b12970a
FC
404"b" =~ m # comment
405 "b"#
406 or print "not ";
6a4ad6ad 407print "ok $test - m <comment> <newline> ...\n"; $test++;
8b12970a
FC
408qr # comment
409 "b"#
410 eq qr/b/ or print "not ";
6a4ad6ad 411print "ok $test - qr <comment> <newline> ...\n"; $test++;
8b12970a
FC
412$_ = "a";
413s # comment
414 [a] #
415 [b] #
416 ;
417print "not " unless $_ eq 'b';
6a4ad6ad 418print "ok $test - s <comment> <newline> ...\n"; $test++;
8b12970a
FC
419$_ = "a";
420tr # comment
421 [a] #
422 [b] #
423 ;
424print "not " unless $_ eq 'b';
6a4ad6ad 425print "ok $test - tr <comment> <newline> ...\n"; $test++;
8b12970a
FC
426$_ = "a";
427y # comment
428 [a] #
429 [b] #
430 ;
431print "not " unless $_ eq 'b';
6a4ad6ad 432print "ok $test - y <comment> <newline> ...\n"; $test++;
21791330
FC
433
434print "not " unless (time
435 =>) eq time=>;
6a4ad6ad 436print "ok $test - => quotes keywords across lines\n"; $test++;
e4916dd1
FC
437
438# [perl #80368]
439print "not " unless eval '"a\U="' eq "a=";
440print "ok $test - [perl #80368] qq <a\\U=>\n"; $test++;
8380b690
FC
441
442sub Function_with_side_effects { $_ = "sidekick function called" }
443print "not " unless
444 (eval '${Function_with_side_effects,\$_}' || $@)
445 eq "sidekick function called";
446print "ok $test - \${...} where {...} looks like hash\n"; $test++;