This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Move a recently-added test
[perl5.git] / t / base / lex.t
CommitLineData
8d063cd8
LW
1#!./perl
2
d9d2b74c 3print "1..107\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
GS
143
144 # These next two tests are trying to make sure that
15a8c21e 145 # $^FOO is always global; it doesn't make sense to 'my' it.
2b92dfce 146 #
0244c3a4 147
2b92dfce
GS
148 eval 'my $^X;';
149 print "not " unless index ($@, 'Can\'t use global $^X in "my"') > -1;
6a4ad6ad 150 print "ok $test\n"; $test++;
2b92dfce
GS
151# print "($@)\n" if $@;
152
153 eval 'my $ {^XYZ};';
154 print "not " unless index ($@, 'Can\'t use global $^XYZ in "my"') > -1;
6a4ad6ad 155 print "ok $test\n"; $test++;
2b92dfce
GS
156# print "($@)\n" if $@;
157
158# Now let's make sure that caret variables are all forced into the main package.
159 package Someother;
766c8ce8
JH
160 $^Q = 'Someother';
161 $ {^Quixote} = 'Someother 2';
2b92dfce
GS
162 $ {^M} = 'Someother 3';
163 package main;
766c8ce8 164 print "not " unless $^Q eq 'Someother';
6a4ad6ad 165 print "ok $test\n"; $test++;
766c8ce8 166 print "not " unless $ {^Quixote} eq 'Someother 2';
6a4ad6ad 167 print "ok $test\n"; $test++;
2b92dfce 168 print "not " unless $ {^M} eq 'Someother 3';
6a4ad6ad 169 print "ok $test\n"; $test++;
2b92dfce
GS
170
171
172}
173
0244c3a4 174# see if eval '', s///e, and heredocs mix
2b92dfce 175
0244c3a4
GS
176sub T {
177 my ($where, $num) = @_;
178 my ($p,$f,$l) = caller;
179 print "# $p:$f:$l vs /$where/\nnot " unless "$p:$f:$l" =~ /$where/;
180 print "ok $num\n";
181}
182
0244c3a4
GS
183{
184# line 42 "plink"
185 local $_ = "not ok ";
186 eval q{
187 s/^not /<<EOT/e and T '^main:\(eval \d+\):2$', $test++;
7d66e4bd 188# uggedaboudit
0244c3a4
GS
189EOT
190 print $_, $test++, "\n";
191 T('^main:\(eval \d+\):6$', $test++);
192# line 1 "plunk"
193 T('^main:plunk:1$', $test++);
194 };
195 print "# $@\nnot ok $test\n" if $@;
196 T '^main:plink:53$', $test++;
197}
8593bda5
GS
198
199# tests 47--51 start here
200# tests for new array interpolation semantics:
201# arrays now *always* interpolate into "..." strings.
202# 20000522 MJD (mjd@plover.com)
203{
8593bda5
GS
204 eval(q(">@nosuch<" eq "><")) || print "# $@", "not ";
205 print "ok $test\n";
206 ++$test;
207
208 # Look at this! This is going to be a common error in the future:
209 eval(q("fred@example.com" eq "fred.com")) || print "# $@", "not ";
210 print "ok $test\n";
211 ++$test;
212
213 # Let's make sure that normal array interpolation still works right
214 # For some reason, this appears not to be tested anywhere else.
215 my @a = (1,2,3);
216 print +((">@a<" eq ">1 2 3<") ? '' : 'not '), "ok $test\n";
217 ++$test;
218
219 # Ditto.
220 eval(q{@nosuch = ('a', 'b', 'c'); ">@nosuch<" eq ">a b c<"})
221 || print "# $@", "not ";
222 print "ok $test\n";
223 ++$test;
224
225 # This isn't actually a lex test, but it's testing the same feature
226 sub makearray {
227 my @array = ('fish', 'dog', 'carrot');
228 *R::crackers = \@array;
229 }
230
231 eval(q{makearray(); ">@R::crackers<" eq ">fish dog carrot<"})
232 || print "# $@", "not ";
233 print "ok $test\n";
234 ++$test;
235}
ce29ac45
JH
236
237# Tests 52-54
238# => should only quote foo::bar if it isn't a real sub. AMS, 20010621
239
240sub xyz::foo { "bar" }
241my %str = (
242 foo => 1,
243 xyz::foo => 1,
244 xyz::bar => 1,
245);
246
ce29ac45
JH
247print ((exists $str{foo} ? "" : "not ")."ok $test\n"); ++$test;
248print ((exists $str{bar} ? "" : "not ")."ok $test\n"); ++$test;
249print ((exists $str{xyz::bar} ? "" : "not ")."ok $test\n"); ++$test;
62444305
AE
250
251sub foo::::::bar { print "ok $test\n"; $test++ }
252foo::::::bar;
356c7adf 253
6345b1dc
KW
254# \xDF is a non-ASCII alpha on both ASCII and EBCDIC.
255eval "\$x =\xDFfoo";
256if ($@ =~ /Unrecognized character \\xDF; marked by <-- HERE after \$x =<-- HERE near column 5/) { print "ok $test\n"; } else { print "not ok $test\n"; }
356c7adf 257$test++;
df3467db
IG
258
259# Is "[~" scanned correctly?
f50fa36f
IG
260@a = (1,2,3);
261print "not " unless($a[~~2] == 3);
6a4ad6ad 262print "ok $test\n"; $test++;
565b52df
FC
263
264$_ = "";
6a4ad6ad
BF
265eval 's/(?:)/"ok $test" . "${\q||}".<<\END/e;
266 - heredoc after "" in s/// in eval
565b52df
FC
267END
268';
6a4ad6ad 269print $_ || "not ok $test\n"; $test++;
043cc6c6
FC
270
271$_ = "";
6a4ad6ad
BF
272eval 's|(?:)|"ok $test" . "${\<<\END}"
273 - heredoc in "" in multiline s///e in eval
043cc6c6
FC
274END
275|e
276';
6a4ad6ad 277print $_ || "not ok $test\n"; $test++;
62abd0d7
FC
278
279$_ = "";
280eval "s/(?:)/<<foo/e #\0
6a4ad6ad 281ok $test - null on same line as heredoc in s/// in eval
62abd0d7
FC
282foo
283";
6a4ad6ad 284print $_ || "not ok $test\n"; $test++;
99bd9d90
FC
285
286$_ = "";
287eval ' s/(?:)/"${\<<END}"/e;
6a4ad6ad 288ok $test - heredoc in "" in single-line s///e in eval
99bd9d90
FC
289END
290';
6a4ad6ad 291print $_ || "not ok $test\n"; $test++;
99bd9d90
FC
292
293$_ = "";
294s|(?:)|"${\<<END}"
6a4ad6ad 295ok $test - heredoc in "" in multiline s///e outside eval
99bd9d90
FC
296END
297|e;
6a4ad6ad 298print $_ || "not ok $test\n"; $test++;
7cc34111 299
6a4ad6ad 300$_ = "not ok $test - s/// in s/// pattern\n";
7cc34111 301s/${s|||;\""}not //;
6a4ad6ad 302print; $test++;
db444266
FC
303
304/(?{print <<END
6a4ad6ad 305ok $test - here-doc in re-eval
db444266 306END
6a4ad6ad 307})/; $test++;
db444266
FC
308
309eval '/(?{print <<END
6a4ad6ad 310ok $test - here-doc in re-eval in string eval
db444266 311END
6a4ad6ad 312})/'; $test++;
11076590 313
6a4ad6ad
BF
314eval 'print qq ;ok $test - eval ending with semicolon\n;'
315 or print "not ok $test - eval ending with semicolon\n"; $test++;
3328ab5a
FC
316
317print "not " unless qr/(?{<<END})/ eq '(?^:(?{<<END}))';
318foo
319END
6a4ad6ad 320print "ok $test - here-doc in single-line re-eval\n"; $test++;
3328ab5a
FC
321
322$_ = qr/(?{"${<<END}"
323foo
324END
325})/;
326print "not " unless /foo/;
6a4ad6ad 327print "ok $test - here-doc in quotes in multiline re-eval\n"; $test++;
76f9939e
FC
328
329eval 's//<<END/e if 0; $_ = "a
330END
331b"';
332print "not " if $_ =~ /\n\n/;
6a4ad6ad 333print "ok $test - eval 's//<<END/' does not leave extra newlines\n"; $test++;
9c74ccc9
FC
334
335$_ = a;
336eval "s/a/'b\0'#/e";
337print 'not ' unless $_ eq "b\0";
6a4ad6ad 338print "ok $test - # after null in s/// repl\n"; $test++;
9c74ccc9
FC
339
340s//"#" . <<END/e;
341foo
342END
6a4ad6ad 343print "ok $test - s//'#' . <<END/e\n"; $test++;
6b00f562
FC
344
345eval "s//3}->{3/e";
346print "not " unless $@;
6a4ad6ad 347print "ok $test - s//3}->{3/e\n"; $test++;
f777953f 348
6a4ad6ad 349$_ = "not ok $test";
f777953f
FC
350$x{3} = "not ";
351eval 's/${\%x}{3}//e';
6a4ad6ad 352print "$_ - s//\${\\%x}{3}/e\n"; $test++;
90a536e1
FC
353
354eval 's/${foo#}//e';
355print "not " unless $@;
6a4ad6ad 356print "ok $test - s/\${foo#}//e\n"; $test++;
819b004e
FC
357
358eval 'warn ({$_ => 1} + 1) if 0';
359print "not " if $@;
6a4ad6ad 360print "ok $test - listop({$_ => 1} + 1)\n"; $test++;
819b004e 361print "# $@" if $@;
c31f6d3b 362
c31f6d3b
FC
363for(qw< require goto last next redo dump >) {
364 eval "sub { $_ foo << 2 }";
365 print "not " if $@;
366 print "ok ", $test++, " - [perl #105924] $_ WORD << ...\n";
367 print "# $@" if $@;
368}
e9d2327d
FC
369
370# http://rt.perl.org/rt3/Ticket/Display.html?id=56880
371my $counter = 0;
372eval 'v23: $counter++; goto v23 unless $counter == 2';
373print "not " unless $counter == 2;
6a4ad6ad 374print "ok $test - Use v[0-9]+ as a label\n"; $test++;
e9d2327d
FC
375$counter = 0;
376eval 'v23 : $counter++; goto v23 unless $counter == 2';
377print "not " unless $counter == 2;
6a4ad6ad 378print "ok $test - Use v[0-9]+ as a label with space before colon\n"; $test++;
e9d2327d
FC
379
380my $output = "";
381eval "package v10::foo; sub test2 { return 'v10::foo' }
382 package v10; sub test { return v10::foo::test2(); }
383 package main; \$output = v10::test(); ";
384print "not " unless $output eq 'v10::foo';
6a4ad6ad 385print "ok $test - call a function in package v10::foo\n"; $test++;
e9d2327d 386
6345b1dc 387print "not " unless (1?v65:"bar") eq chr(65);
6a4ad6ad 388print "ok $test - colon detection after vstring does not break ? vstring :\n"; $test++;
770ed452
KW
389
390print ((ord("A") == 65) ? v35 : v123); # NUMBER SIGN is the same for all
391 # supported EBCDIC platforms
392print "not ";
393print ((ord("A") == 65) ? v10 : "\n"); # LF varies on EBCDIC, if the v123 for
394 # '#' works above, consider it good
395 # enough.
a9d49ba7 396 print "ok $test - print vstring prints the vstring\n";
a9d49ba7 397$test++;
8b12970a
FC
398
399# Test pyoq ops with comments before the first delim
400q # comment
401 "b"#
402 eq 'b' or print "not ";
6a4ad6ad 403print "ok $test - q <comment> <newline> ...\n"; $test++;
8b12970a
FC
404qq # comment
405 "b"#
406 eq 'b' or print "not ";
6a4ad6ad 407print "ok $test - qq <comment> <newline> ...\n"; $test++;
8b12970a
FC
408qw # comment
409 "b"#
410 [0] eq 'b' or print "not ";
6a4ad6ad 411print "ok $test - qw <comment> <newline> ...\n"; $test++;
8b12970a
FC
412"b" =~ m # comment
413 "b"#
414 or print "not ";
6a4ad6ad 415print "ok $test - m <comment> <newline> ...\n"; $test++;
8b12970a
FC
416qr # comment
417 "b"#
418 eq qr/b/ or print "not ";
6a4ad6ad 419print "ok $test - qr <comment> <newline> ...\n"; $test++;
8b12970a
FC
420$_ = "a";
421s # comment
422 [a] #
423 [b] #
424 ;
425print "not " unless $_ eq 'b';
6a4ad6ad 426print "ok $test - s <comment> <newline> ...\n"; $test++;
8b12970a
FC
427$_ = "a";
428tr # comment
429 [a] #
430 [b] #
431 ;
432print "not " unless $_ eq 'b';
6a4ad6ad 433print "ok $test - tr <comment> <newline> ...\n"; $test++;
8b12970a
FC
434$_ = "a";
435y # comment
436 [a] #
437 [b] #
438 ;
439print "not " unless $_ eq 'b';
6a4ad6ad 440print "ok $test - y <comment> <newline> ...\n"; $test++;
21791330
FC
441
442print "not " unless (time
443 =>) eq time=>;
6a4ad6ad 444print "ok $test - => quotes keywords across lines\n"; $test++;
e4916dd1
FC
445
446# [perl #80368]
447print "not " unless eval '"a\U="' eq "a=";
448print "ok $test - [perl #80368] qq <a\\U=>\n"; $test++;
8380b690
FC
449
450sub Function_with_side_effects { $_ = "sidekick function called" }
451print "not " unless
452 (eval '${Function_with_side_effects,\$_}' || $@)
453 eq "sidekick function called";
454print "ok $test - \${...} where {...} looks like hash\n"; $test++;
e660c409
FC
455
456@_ = map{BEGIN {$_122782 = 'tst2'}; "rhu$_"} 'barb2';
457print "not " unless "@_" eq 'rhubarb2';
458print "ok $test - map{BEGIN...\n"; $test++;
459print "not " unless $_122782 eq 'tst2';
460print "ok $test - map{BEGIN...\n"; $test++;
461${
462=pod
463blah blah blah
464=cut
465\$_ } = 42;
466print "not "unless $_ == 42;
467print "ok $test - \${ <newline> =pod\n"; $test++;
468@_ = map{
469=pod
470blah blah blah
471=cut
472$_+1 } 1;
473print "not "unless "@_" eq 2;
474print "ok $test - map{ <newline> =pod\n"; $test++;
475eval { ${...}++ };
476print "not " unless $@ =~ /^Unimplemented at /;
477print "ok $test - \${...} (literal triple-dot)\n"; $test++;
478eval { () = map{...} @_ };
479print "not " unless $@ =~ /^Unimplemented at /;
480print "ok $test - map{...} (literal triple-dot)\n"; $test++;
481print "not " unless &{sub :lvalue { "a" }} eq "a";
482print "ok $test - &{sub :lvalue...}\n"; $test++;
712a4517 483print "not " unless ref +(map{sub :lvalue { "a" }} 1)[0] eq "CODE";
e660c409 484print "ok $test - map{sub :lvalue...}\n"; $test++;
83a85f49
FC
485
486# Used to crash [perl #123711]
4870-5x-l{0};
eabab8bc 488
ce7c414e 489# Used to fail an assertion [perl #123617] [perl #123955]
eabab8bc 490eval '"$a{ 1 m// }"; //';
ce7c414e 491eval '"@0{0s 000";eval"$"';
7aa8cb0d
FC
492
493# Pending token stack overflow [perl #123677]
494{
495 local $SIG{__WARN__}=sub{};
496 eval q|s)$0{0h());qx(@0);qx(@0);qx(@0)|;
497}
f4460c6f
FC
498
499# Used to crash [perl #123801]
500eval q|s##[}#e|;
179b3fad
FC
501
502# Used to fail an assertion [perl #123763]
503{
504 local $SIG{__WARN__}=sub{};
505 eval q|my($_);0=split|;
55b39803 506 eval q|my $_; @x = split|;
179b3fad 507}
b12396ac
TC
508
509{
510 # Used to crash [perl #124187]
511 eval q|qq{@{[{}}*sub{]]}}}=u|;
512}
de0885da
DM
513
514{
515 # Used to crash [perl #124385]
516 eval '0; qq{@{sub{]]}}}}}';
517 print "ok $test - 124385\n"; $test++;
518}
a293d0fd
SF
519
520{
521 # Used to crash [perl #125350]
522 eval ('qq{@{[0}*sub{]]}}}=sub{0' . "\c[");
523 print "ok $test - 125350\n"; $test++;
524}
d6744494
FC
525
526{
527 # Used to crash [perl #128171]
528 eval ('/@0{0*->@*/*]');
529 print "ok $test - 128171\n"; $test++;
530}
d9d2b74c
FC
531
532$foo = "WRONG"; $foo:: = "bar"; $bar = "baz";
533print "not " unless "$foo::$bar" eq "barbaz";
534print qq|ok $test - [perl #128478] "\$foo::\$bar"\n|; $test++;
535@bar = ("baz","bonk");
536print "not " unless "$foo::@bar" eq "barbaz bonk";
537print qq|ok $test - [perl #128478] "\$foo::\@bar"\n|; $test ++;