This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
t/op/utf8decode.t: Update tests from new source material
[perl5.git] / t / base / lex.t
1 #!./perl
2
3 print "1..103\n";
4
5 $x = 'x';
6
7 print "#1       :$x: eq :x:\n";
8 if ($x eq 'x') {print "ok 1\n";} else {print "not ok 1\n";}
9
10 $x = $#[0];
11
12 if ($x eq '') {print "ok 2\n";} else {print "not ok 2\n";}
13
14 $x = $#x;
15
16 if ($x eq '-1') {print "ok 3\n";} else {print "not ok 3\n";}
17
18 $x = '\\'; # ';
19
20 if (length($x) == 1) {print "ok 4\n";} else {print "not ok 4\n";}
21
22 eval 'while (0) {
23     print "foo\n";
24 }
25 /^/ && (print "ok 5\n");
26 ';
27
28 eval '$foo{1} / 1;';
29 if (!$@) {print "ok 6\n";} else {print "not ok 6 $@\n";}
30
31 eval '$foo = 123+123.4+123e4+123.4E5+123.4e+5+.12;';
32
33 $foo = int($foo * 100 + .5);
34 if ($foo eq 2591024652) {print "ok 7\n";} else {print "not ok 7 :$foo:\n";}
35
36 print <<'EOF';
37 ok 8
38 EOF
39
40 $foo = 'ok 9';
41 print <<EOF;
42 $foo
43 EOF
44
45 eval <<\EOE, print $@;
46 print <<'EOF';
47 ok 10
48 EOF
49
50 $foo = 'ok 11';
51 print <<EOF;
52 $foo
53 EOF
54 EOE
55
56 print <<'EOS' . <<\EOF;
57 ok 12 - make sure single quotes are honored \nnot ok
58 EOS
59 ok 13
60 EOF
61
62 print qq/ok 14\n/;
63 print qq(ok 15\n);
64
65 print qq
66 [ok 16\n]
67 ;
68
69 print q<ok 17
70 >;
71
72 print "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.
77
78 print <<E1 eq "foo\n\n" ? "ok 19\n" : "not ok 19\n";
79 @{[ <<E2 ]}
80 foo
81 E2
82 E1
83
84 print <<E1 eq "foo\n\n" ? "ok 20\n" : "not ok 20\n";
85 @{[
86   <<E2
87 foo
88 E2
89 ]}
90 E1
91
92 $foo = FOO;
93 $bar = BAR;
94 $foo{$bar} = BAZ;
95 $ary[0] = ABC;
96
97 print "$foo{$bar}" eq "BAZ" ? "ok 21\n" : "not ok 21\n";
98
99 print "${foo}{$bar}" eq "FOO{BAR}" ? "ok 22\n" : "not ok 22\n";
100 print "${foo{$bar}}" eq "BAZ" ? "ok 23\n" : "not ok 23\n";
101
102 print "FOO:" =~ /$foo[:]/ ? "ok 24\n" : "not ok 24\n";
103 print "ABC" =~ /^$ary[$A]$/ ? "ok 25\n" : "not ok 25\n";
104 print "FOOZ" =~ /^$foo[$A-Z]$/ ? "ok 26\n" : "not ok 26\n";
105
106 # MJD 19980425
107 ($X, @X) = qw(a b c d); 
108 print "d" =~ /^$X[-1]$/ ? "ok 27\n" : "not ok 27\n";
109 print "a1" !~ /^$X[-1]$/ ? "ok 28\n" : "not ok 28\n";
110
111 print (((q{{\{\(}} . q{{\)\}}}) eq '{{\(}{\)}}') ? "ok 29\n" : "not ok 29\n");
112
113
114 $foo = "not ok 30\n";
115 $foo =~ s/^not /substr(<<EOF, 0, 0)/e;
116   Ignored
117 EOF
118 print $foo;
119
120 # Tests for new extended control-character variables
121 # MJD 19990227
122
123 my $test = 31;
124
125 { my $CX = "\cX";
126   my $CXY  ="\cXY";
127   $ {$CX} = 17;
128   $ {$CXY} = 23;
129   if ($ {^XY} != 23) { print "not "  }
130   print "ok $test\n"; $test++;
131  
132 # Does the old UNBRACED syntax still do what it used to?
133   if ("$^XY" ne "17Y") { print "not " }
134   print "ok $test\n"; $test++;
135
136   sub XX () { 6 }
137   $ {"\cQ\cXX"} = 119; 
138   $^Q = 5; #  This should be an unused ^Var.
139   $N = 5;
140   # The second caret here should be interpreted as an xor
141   if (($^Q^XX) != 3) { print "not " } 
142   print "ok $test\n"; $test++;
143
144   # These next two tests are trying to make sure that
145   # $^FOO is always global; it doesn't make sense to 'my' it.
146   # 
147
148   eval 'my $^X;';
149   print "not " unless index ($@, 'Can\'t use global $^X in "my"') > -1;
150   print "ok $test\n"; $test++;
151 #  print "($@)\n" if $@;
152
153   eval 'my $ {^XYZ};';
154   print "not " unless index ($@, 'Can\'t use global $^XYZ in "my"') > -1;
155   print "ok $test\n"; $test++;
156 #  print "($@)\n" if $@;
157
158 # Now let's make sure that caret variables are all forced into the main package.
159   package Someother;
160   $^Q = 'Someother';
161   $ {^Quixote} = 'Someother 2';
162   $ {^M} = 'Someother 3';
163   package main;
164   print "not " unless $^Q eq 'Someother';
165   print "ok $test\n"; $test++;
166   print "not " unless $ {^Quixote} eq 'Someother 2';
167   print "ok $test\n"; $test++;
168   print "not " unless $ {^M} eq 'Someother 3';
169   print "ok $test\n"; $test++;
170
171   
172 }
173
174 # see if eval '', s///e, and heredocs mix
175
176 sub 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
183 {
184 # line 42 "plink"
185     local $_ = "not ok ";
186     eval q{
187         s/^not /<<EOT/e and T '^main:\(eval \d+\):2$', $test++;
188 # uggedaboudit
189 EOT
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 }
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 {
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 }
236
237 # Tests 52-54
238 # => should only quote foo::bar if it isn't a real sub. AMS, 20010621
239
240 sub xyz::foo { "bar" }
241 my %str = (
242     foo      => 1,
243     xyz::foo => 1,
244     xyz::bar => 1,
245 );
246
247 print ((exists $str{foo}      ? "" : "not ")."ok $test\n"); ++$test;
248 print ((exists $str{bar}      ? "" : "not ")."ok $test\n"); ++$test;
249 print ((exists $str{xyz::bar} ? "" : "not ")."ok $test\n"); ++$test;
250
251 sub foo::::::bar { print "ok $test\n"; $test++ }
252 foo::::::bar;
253
254 # \xDF is a non-ASCII alpha on both ASCII and EBCDIC.
255 eval "\$x =\xDFfoo";
256 if ($@ =~ /Unrecognized character \\xDF; marked by <-- HERE after \$x =<-- HERE near column 5/) { print "ok $test\n"; } else { print "not ok $test\n"; }
257 $test++;
258
259 # Is "[~" scanned correctly?
260 @a = (1,2,3);
261 print "not " unless($a[~~2] == 3);
262 print "ok $test\n"; $test++;
263
264 $_ = "";
265 eval 's/(?:)/"ok $test" . "${\q||}".<<\END/e;
266  - heredoc after "" in s/// in eval
267 END
268 ';
269 print $_ || "not ok $test\n"; $test++;
270
271 $_ = "";
272 eval 's|(?:)|"ok $test" . "${\<<\END}"
273  - heredoc in "" in multiline s///e in eval
274 END
275 |e
276 ';
277 print $_ || "not ok $test\n"; $test++;
278
279 $_ = "";
280 eval "s/(?:)/<<foo/e #\0
281 ok $test - null on same line as heredoc in s/// in eval
282 foo
283 ";
284 print $_ || "not ok $test\n"; $test++;
285
286 $_ = "";
287 eval ' s/(?:)/"${\<<END}"/e;
288 ok $test - heredoc in "" in single-line s///e in eval
289 END
290 ';
291 print $_ || "not ok $test\n"; $test++;
292
293 $_ = "";
294 s|(?:)|"${\<<END}"
295 ok $test - heredoc in "" in multiline s///e outside eval
296 END
297 |e;
298 print $_ || "not ok $test\n"; $test++;
299
300 $_ = "not ok $test - s/// in s/// pattern\n";
301 s/${s|||;\""}not //;
302 print; $test++;
303
304 /(?{print <<END
305 ok $test - here-doc in re-eval
306 END
307 })/; $test++;
308
309 eval '/(?{print <<END
310 ok $test - here-doc in re-eval in string eval
311 END
312 })/'; $test++;
313
314 eval 'print qq ;ok $test - eval ending with semicolon\n;'
315   or print "not ok $test - eval ending with semicolon\n"; $test++;
316
317 print "not " unless qr/(?{<<END})/ eq '(?^:(?{<<END}))';
318 foo
319 END
320 print "ok $test - here-doc in single-line re-eval\n"; $test++;
321
322 $_ = qr/(?{"${<<END}"
323 foo
324 END
325 })/;
326 print "not " unless /foo/;
327 print "ok $test - here-doc in quotes in multiline re-eval\n"; $test++;
328
329 eval 's//<<END/e if 0; $_ = "a
330 END
331 b"';
332 print "not " if $_ =~ /\n\n/;
333 print "ok $test - eval 's//<<END/' does not leave extra newlines\n"; $test++;
334
335 $_ = a;
336 eval "s/a/'b\0'#/e";
337 print 'not ' unless $_ eq "b\0";
338 print "ok $test - # after null in s/// repl\n"; $test++;
339
340 s//"#" . <<END/e;
341 foo
342 END
343 print "ok $test - s//'#' . <<END/e\n"; $test++;
344
345 eval "s//3}->{3/e";
346 print "not " unless $@;
347 print "ok $test - s//3}->{3/e\n"; $test++;
348
349 $_ = "not ok $test";
350 $x{3} = "not ";
351 eval 's/${\%x}{3}//e';
352 print "$_ - s//\${\\%x}{3}/e\n"; $test++;
353
354 eval 's/${foo#}//e';
355 print "not " unless $@;
356 print "ok $test - s/\${foo#}//e\n"; $test++;
357
358 eval 'warn ({$_ => 1} + 1) if 0';
359 print "not " if $@;
360 print "ok $test - listop({$_ => 1} + 1)\n"; $test++;
361 print "# $@" if $@;
362
363 for(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 }
369
370 # http://rt.perl.org/rt3/Ticket/Display.html?id=56880
371 my $counter = 0;
372 eval 'v23: $counter++; goto v23 unless $counter == 2';
373 print "not " unless $counter == 2;
374 print "ok $test - Use v[0-9]+ as a label\n"; $test++;
375 $counter = 0;
376 eval 'v23 : $counter++; goto v23 unless $counter == 2';
377 print "not " unless $counter == 2;
378 print "ok $test - Use v[0-9]+ as a label with space before colon\n"; $test++;
379  
380 my $output = "";
381 eval "package v10::foo; sub test2 { return 'v10::foo' }
382       package v10; sub test { return v10::foo::test2(); }
383       package main; \$output = v10::test(); "; 
384 print "not " unless $output eq 'v10::foo';
385 print "ok $test - call a function in package v10::foo\n"; $test++;
386
387 print "not " unless (1?v65:"bar") eq chr(65);
388 print "ok $test - colon detection after vstring does not break ? vstring :\n"; $test++;
389 if (ord("A") == 65) {
390     print v35;
391     print "not ";
392     print v10;
393     print "ok $test - print vstring prints the vstring\n";
394 }
395 else {
396     print "ok $test # skipped on EBCDIC\n";
397 }
398 $test++;
399
400 # Test pyoq ops with comments before the first delim
401 q # comment
402  "b"#
403   eq 'b' or print "not ";
404 print "ok $test - q <comment> <newline> ...\n"; $test++;
405 qq # comment
406  "b"#
407   eq 'b' or print "not ";
408 print "ok $test - qq <comment> <newline> ...\n"; $test++;
409 qw # comment
410  "b"#
411   [0] eq 'b' or print "not ";
412 print "ok $test - qw <comment> <newline> ...\n"; $test++;
413 "b" =~ m # comment
414  "b"#
415    or print "not ";
416 print "ok $test - m <comment> <newline> ...\n"; $test++;
417 qr # comment
418  "b"#
419    eq qr/b/ or print "not ";
420 print "ok $test - qr <comment> <newline> ...\n"; $test++;
421 $_ = "a";
422 s # comment
423  [a] #
424  [b] #
425  ;
426 print "not " unless $_ eq 'b';
427 print "ok $test - s <comment> <newline> ...\n"; $test++;
428 $_ = "a";
429 tr # comment
430  [a] #
431  [b] #
432  ;
433 print "not " unless $_ eq 'b';
434 print "ok $test - tr <comment> <newline> ...\n"; $test++;
435 $_ = "a";
436 y # comment
437  [a] #
438  [b] #
439  ;
440 print "not " unless $_ eq 'b';
441 print "ok $test - y <comment> <newline> ...\n"; $test++;
442
443 print "not " unless (time
444                      =>) eq time=>;
445 print "ok $test - => quotes keywords across lines\n"; $test++;
446
447 # [perl #80368]
448 print "not " unless eval '"a\U="' eq "a=";
449 print "ok $test - [perl #80368] qq <a\\U=>\n"; $test++;
450
451 sub Function_with_side_effects { $_ = "sidekick function called" }
452 print "not " unless
453     (eval '${Function_with_side_effects,\$_}' || $@)
454       eq "sidekick function called";
455 print "ok $test - \${...} where {...} looks like hash\n"; $test++;
456
457 @_ = map{BEGIN {$_122782 = 'tst2'}; "rhu$_"} 'barb2';
458 print "not " unless "@_" eq 'rhubarb2';
459 print "ok $test - map{BEGIN...\n"; $test++;
460 print "not " unless $_122782 eq 'tst2';
461 print "ok $test - map{BEGIN...\n"; $test++;
462 ${
463 =pod
464 blah blah blah
465 =cut
466 \$_ } = 42;
467 print "not "unless $_ == 42;
468 print "ok $test - \${ <newline> =pod\n"; $test++;
469 @_ = map{
470 =pod
471 blah blah blah
472 =cut
473 $_+1 } 1;
474 print "not "unless "@_" eq 2;
475 print "ok $test - map{ <newline> =pod\n"; $test++;
476 eval { ${...}++ };
477 print "not " unless $@ =~ /^Unimplemented at /;
478 print "ok $test - \${...} (literal triple-dot)\n"; $test++;
479 eval { () = map{...} @_ };
480 print "not " unless $@ =~ /^Unimplemented at /;
481 print "ok $test - map{...} (literal triple-dot)\n"; $test++;
482 print "not " unless &{sub :lvalue { "a" }} eq "a";
483 print "ok $test - &{sub :lvalue...}\n"; $test++;
484 print "not " unless ref +(map{sub :lvalue { "a" }} 1)[0] eq "CODE";
485 print "ok $test - map{sub :lvalue...}\n"; $test++;
486
487 # Used to crash [perl #123711]
488 0-5x-l{0};
489
490 # Used to fail an assertion [perl #123617] [perl #123955]
491 eval '"$a{ 1 m// }"; //';
492 eval '"@0{0s 000";eval"$"';
493
494 # Pending token stack overflow [perl #123677]
495 {
496  local $SIG{__WARN__}=sub{};
497  eval q|s)$0{0h());qx(@0);qx(@0);qx(@0)|;
498 }
499
500 # Used to crash [perl #123801]
501 eval q|s##[}#e|;
502
503 # Used to fail an assertion [perl #123763]
504 {
505  local $SIG{__WARN__}=sub{};
506  eval q|my($_);0=split|;
507  eval q|my $_; @x = split|;
508 }
509
510 {
511  # Used to crash [perl #124187]
512  eval q|qq{@{[{}}*sub{]]}}}=u|;
513 }
514
515 {
516  # Used to crash [perl #124385]
517  eval '0; qq{@{sub{]]}}}}}';
518  print "ok $test - 124385\n"; $test++;
519 }