xref: /openbsd/gnu/usr.bin/perl/t/base/lex.t (revision 09467b48)
1#!./perl
2
3print "1..120\n";
4
5$x = 'x';
6
7print "#1	:$x: eq :x:\n";
8if ($x eq 'x') {print "ok 1\n";} else {print "not ok 1\n";}
9
10$x = $#[0];
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";}
21
22eval 'while (0) {
23    print "foo\n";
24}
25/^/ && (print "ok 5\n");
26';
27
28eval '$foo{1} / 1;';
29if (!$@) {print "ok 6\n";} else {print "not ok 6 $@\n";}
30
31eval '$foo = 123+123.4+123e4+123.4E5+123.4e+5+.12;';
32
33$foo = int($foo * 100 + .5);
34if ($foo eq 2591024652) {print "ok 7\n";} else {print "not ok 7 :$foo:\n";}
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
56print <<'EOS' . <<\EOF;
57ok 12 - make sure single quotes are honored \nnot ok
58EOS
59ok 13
60EOF
61
62print qq/ok 14\n/;
63print qq(ok 15\n);
64
65print qq
66[ok 16\n]
67;
68
69print q<ok 17
70>;
71
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.
77
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
92$foo = FOO;
93$bar = BAR;
94$foo{$bar} = BAZ;
95$ary[0] = ABC;
96
97print "$foo{$bar}" eq "BAZ" ? "ok 21\n" : "not ok 21\n";
98
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";
101
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";
105
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";
110
111print (((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
117EOF
118print $foo;
119
120# Tests for new extended control-character variables
121# MJD 19990227
122
123my $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  ${^TEST}= "splat";
159  @{^TEST}= ("foo", "bar");
160  %{^TEST}= ("foo" => "FOO", "bar" => "BAR" );
161
162  print "not " if "${^TEST}" ne "splat";
163  print "ok $test\n"; $test++;
164
165  print "not " if "${ ^TEST }" ne "splat";
166  print "ok $test\n"; $test++;
167
168  print "not " if "${^TEST}[0]" ne "splat[0]";
169  print "ok $test\n"; $test++;
170
171  print "not " if "${^TEST[0]}" ne "foo";
172  print "ok $test\n"; $test++;
173
174  print "not " if "${ ^TEST [1] }" ne "bar";
175  print "ok $test\n"; $test++;
176
177  print "not " if "${^TEST}{foo}" ne "splat{foo}";
178  print "ok $test\n"; $test++;
179
180  print "not " if "${^TEST{foo}}" ne "FOO";
181  print "ok $test\n"; $test++;
182
183  print "not " if "${ ^TEST {bar} }" ne "BAR";
184  print "ok $test\n"; $test++;
185
186
187# Now let's make sure that caret variables are all forced into the main package.
188  package Someother;
189  $^Q = 'Someother';
190  $ {^Quixote} = 'Someother 2';
191  $ {^M} = 'Someother 3';
192  package main;
193  print "not " unless $^Q eq 'Someother';
194  print "ok $test\n"; $test++;
195  print "not " unless $ {^Quixote} eq 'Someother 2';
196  print "ok $test\n"; $test++;
197  print "not " unless $ {^M} eq 'Someother 3';
198  print "ok $test\n"; $test++;
199
200
201}
202
203# see if eval '', s///e, and heredocs mix
204
205sub T {
206    my ($where, $num) = @_;
207    my ($p,$f,$l) = caller;
208    print "# $p:$f:$l vs /$where/\nnot " unless "$p:$f:$l" =~ /$where/;
209    print "ok $num\n";
210}
211
212{
213# line 42 "plink"
214    local $_ = "not ok ";
215    eval q{
216	s/^not /<<EOT/e and T '^main:\(eval \d+\):2$', $test++;
217# uggedaboudit
218EOT
219        print $_, $test++, "\n";
220	T('^main:\(eval \d+\):6$', $test++);
221# line 1 "plunk"
222	T('^main:plunk:1$', $test++);
223    };
224    print "# $@\nnot ok $test\n" if $@;
225    T '^main:plink:53$', $test++;
226}
227
228# tests 47--51 start here
229# tests for new array interpolation semantics:
230# arrays now *always* interpolate into "..." strings.
231# 20000522 MJD (mjd@plover.com)
232{
233  eval(q(">@nosuch<" eq "><")) || print "# $@", "not ";
234  print "ok $test\n";
235  ++$test;
236
237  # Look at this!  This is going to be a common error in the future:
238  eval(q("fred@example.com" eq "fred.com")) || print "# $@", "not ";
239  print "ok $test\n";
240  ++$test;
241
242  # Let's make sure that normal array interpolation still works right
243  # For some reason, this appears not to be tested anywhere else.
244  my @a = (1,2,3);
245  print +((">@a<" eq ">1 2 3<") ? '' : 'not '), "ok $test\n";
246  ++$test;
247
248  # Ditto.
249  eval(q{@nosuch = ('a', 'b', 'c'); ">@nosuch<" eq ">a b c<"})
250      || print "# $@", "not ";
251  print "ok $test\n";
252  ++$test;
253
254  # This isn't actually a lex test, but it's testing the same feature
255  sub makearray {
256    my @array = ('fish', 'dog', 'carrot');
257    *R::crackers = \@array;
258  }
259
260  eval(q{makearray(); ">@R::crackers<" eq ">fish dog carrot<"})
261    || print "# $@", "not ";
262  print "ok $test\n";
263  ++$test;
264}
265
266# Tests 52-54
267# => should only quote foo::bar if it isn't a real sub. AMS, 20010621
268
269sub xyz::foo { "bar" }
270my %str = (
271    foo      => 1,
272    xyz::foo => 1,
273    xyz::bar => 1,
274);
275
276print ((exists $str{foo}      ? "" : "not ")."ok $test\n"); ++$test;
277print ((exists $str{bar}      ? "" : "not ")."ok $test\n"); ++$test;
278print ((exists $str{xyz::bar} ? "" : "not ")."ok $test\n"); ++$test;
279
280sub foo::::::bar { print "ok $test\n"; $test++ }
281foo::::::bar;
282
283# \xDF is a non-ASCII alpha on both ASCII and EBCDIC.
284eval "\$x =\xDFfoo";
285if ($@ =~ /Unrecognized character \\xDF; marked by <-- HERE after \$x =<-- HERE near column 5/) { print "ok $test\n"; } else { print "not ok $test\n"; }
286$test++;
287
288# Is "[~" scanned correctly?
289@a = (1,2,3);
290print "not " unless($a[~~2] == 3);
291print "ok $test\n"; $test++;
292
293$_ = "";
294eval 's/(?:)/"ok $test" . "${\q||}".<<\END/e;
295 - heredoc after "" in s/// in eval
296END
297';
298print $_ || "not ok $test\n"; $test++;
299
300$_ = "";
301eval 's|(?:)|"ok $test" . "${\<<\END}"
302 - heredoc in "" in multiline s///e in eval
303END
304|e
305';
306print $_ || "not ok $test\n"; $test++;
307
308$_ = "";
309eval "s/(?:)/<<foo/e #\0
310ok $test - null on same line as heredoc in s/// in eval
311foo
312";
313print $_ || "not ok $test\n"; $test++;
314
315$_ = "";
316eval ' s/(?:)/"${\<<END}"/e;
317ok $test - heredoc in "" in single-line s///e in eval
318END
319';
320print $_ || "not ok $test\n"; $test++;
321
322$_ = "";
323s|(?:)|"${\<<END}"
324ok $test - heredoc in "" in multiline s///e outside eval
325END
326|e;
327print $_ || "not ok $test\n"; $test++;
328
329$_ = "not ok $test - s/// in s/// pattern\n";
330s/${s|||;\""}not //;
331print; $test++;
332
333/(?{print <<END
334ok $test - here-doc in re-eval
335END
336})/; $test++;
337
338eval '/(?{print <<END
339ok $test - here-doc in re-eval in string eval
340END
341})/'; $test++;
342
343eval 'print qq ;ok $test - eval ending with semicolon\n;'
344  or print "not ok $test - eval ending with semicolon\n"; $test++;
345
346print "not " unless qr/(?{<<END})/ eq '(?^:(?{<<END}))';
347foo
348END
349print "ok $test - here-doc in single-line re-eval\n"; $test++;
350
351$_ = qr/(?{"${<<END}"
352foo
353END
354})/;
355print "not " unless /foo/;
356print "ok $test - here-doc in quotes in multiline re-eval\n"; $test++;
357
358eval 's//<<END/e if 0; $_ = "a
359END
360b"';
361print "not " if $_ =~ /\n\n/;
362print "ok $test - eval 's//<<END/' does not leave extra newlines\n"; $test++;
363
364$_ = a;
365eval "s/a/'b\0'#/e";
366print 'not ' unless $_ eq "b\0";
367print "ok $test - # after null in s/// repl\n"; $test++;
368
369s//"#" . <<END/e;
370foo
371END
372print "ok $test - s//'#' . <<END/e\n"; $test++;
373
374eval "s//3}->{3/e";
375print "not " unless $@;
376print "ok $test - s//3}->{3/e\n"; $test++;
377
378$_ = "not ok $test";
379$x{3} = "not ";
380eval 's/${\%x}{3}//e';
381print "$_ - s//\${\\%x}{3}/e\n"; $test++;
382
383eval 's/${foo#}//e';
384print "not " unless $@;
385print "ok $test - s/\${foo#}//e\n"; $test++;
386
387eval 'warn ({$_ => 1} + 1) if 0';
388print "not " if $@;
389print "ok $test - listop({$_ => 1} + 1)\n"; $test++;
390print "# $@" if $@;
391
392for(qw< require goto last next redo CORE::dump >) {
393    eval "sub { $_ foo << 2 }";
394    print "not " if $@;
395    print "ok ", $test++, " - [perl #105924] $_ WORD << ...\n";
396    print "# $@" if $@;
397}
398
399# http://rt.perl.org/rt3/Ticket/Display.html?id=56880
400my $counter = 0;
401eval 'v23: $counter++; goto v23 unless $counter == 2';
402print "not " unless $counter == 2;
403print "ok $test - Use v[0-9]+ as a label\n"; $test++;
404$counter = 0;
405eval 'v23 : $counter++; goto v23 unless $counter == 2';
406print "not " unless $counter == 2;
407print "ok $test - Use v[0-9]+ as a label with space before colon\n"; $test++;
408
409my $output = "";
410eval "package v10::foo; sub test2 { return 'v10::foo' }
411      package v10; sub test { return v10::foo::test2(); }
412      package main; \$output = v10::test(); ";
413print "not " unless $output eq 'v10::foo';
414print "ok $test - call a function in package v10::foo\n"; $test++;
415
416print "not " unless (1?v65:"bar") eq chr(65);
417print "ok $test - colon detection after vstring does not break ? vstring :\n"; $test++;
418
419print ((ord("A") == 65) ? v35 : v123);  # NUMBER SIGN is the same for all
420                                        # supported EBCDIC platforms
421print "not ";
422print ((ord("A") == 65) ? v10 : "\n");  # LF varies on EBCDIC, if the v123 for
423                                        # '#' works above, consider it good
424                                        # enough.
425    print "ok $test - print vstring prints the vstring\n";
426$test++;
427
428# Test pyoq ops with comments before the first delim
429q # comment
430 "b"#
431  eq 'b' or print "not ";
432print "ok $test - q <comment> <newline> ...\n"; $test++;
433qq # comment
434 "b"#
435  eq 'b' or print "not ";
436print "ok $test - qq <comment> <newline> ...\n"; $test++;
437qw # comment
438 "b"#
439  [0] eq 'b' or print "not ";
440print "ok $test - qw <comment> <newline> ...\n"; $test++;
441"b" =~ m # comment
442 "b"#
443   or print "not ";
444print "ok $test - m <comment> <newline> ...\n"; $test++;
445qr # comment
446 "b"#
447   eq qr/b/ or print "not ";
448print "ok $test - qr <comment> <newline> ...\n"; $test++;
449$_ = "a";
450s # comment
451 [a] #
452 [b] #
453 ;
454print "not " unless $_ eq 'b';
455print "ok $test - s <comment> <newline> ...\n"; $test++;
456$_ = "a";
457tr # comment
458 [a] #
459 [b] #
460 ;
461print "not " unless $_ eq 'b';
462print "ok $test - tr <comment> <newline> ...\n"; $test++;
463$_ = "a";
464y # comment
465 [a] #
466 [b] #
467 ;
468print "not " unless $_ eq 'b';
469print "ok $test - y <comment> <newline> ...\n"; $test++;
470
471print "not " unless (time
472                     =>) eq time=>;
473print "ok $test - => quotes keywords across lines\n"; $test++;
474
475# [perl #80368]
476print "not " unless eval '"a\U="' eq "a=";
477print "ok $test - [perl #80368] qq <a\\U=>\n"; $test++;
478
479sub Function_with_side_effects { $_ = "sidekick function called" }
480print "not " unless
481    (eval '${Function_with_side_effects,\$_}' || $@)
482      eq "sidekick function called";
483print "ok $test - \${...} where {...} looks like hash\n"; $test++;
484
485@_ = map{BEGIN {$_122782 = 'tst2'}; "rhu$_"} 'barb2';
486print "not " unless "@_" eq 'rhubarb2';
487print "ok $test - map{BEGIN...\n"; $test++;
488print "not " unless $_122782 eq 'tst2';
489print "ok $test - map{BEGIN...\n"; $test++;
490${
491=pod
492blah blah blah
493=cut
494\$_ } = 42;
495print "not "unless $_ == 42;
496print "ok $test - \${ <newline> =pod\n"; $test++;
497@_ = map{
498=pod
499blah blah blah
500=cut
501$_+1 } 1;
502print "not "unless "@_" eq 2;
503print "ok $test - map{ <newline> =pod\n"; $test++;
504eval { ${...}++ };
505print "not " unless $@ =~ /^Unimplemented at /;
506print "ok $test - \${...} (literal triple-dot)\n"; $test++;
507eval { () = map{...} @_ };
508print "not " unless $@ =~ /^Unimplemented at /;
509print "ok $test - map{...} (literal triple-dot)\n"; $test++;
510print "not " unless &{sub :lvalue { "a" }} eq "a";
511print "ok $test - &{sub :lvalue...}\n"; $test++;
512print "not " unless ref +(map{sub :lvalue { "a" }} 1)[0] eq "CODE";
513print "ok $test - map{sub :lvalue...}\n"; $test++;
514
515# Used to crash [perl #123711]
5160-5x-l{0};
517
518# Used to fail an assertion [perl #123617] [perl #123955]
519eval '"$a{ 1 m// }"; //';
520eval '"@0{0s 000";eval"$"';
521
522# Pending token stack overflow [perl #123677]
523{
524 local $SIG{__WARN__}=sub{};
525 eval q|s)$0{0h());qx(@0);qx(@0);qx(@0)|;
526}
527
528# Used to crash [perl #123801]
529eval q|s##[}#e|;
530
531# Used to fail an assertion [perl #123763]
532{
533 local $SIG{__WARN__}=sub{};
534 eval q|my($_);0=split|;
535 eval q|my $_; @x = split|;
536}
537
538{
539 # Used to crash [perl #124187]
540 eval q|qq{@{[{}}*sub{]]}}}=u|;
541}
542
543{
544 # Used to crash [perl #124385]
545 eval '0; qq{@{sub{]]}}}}}';
546 print "ok $test - 124385\n"; $test++;
547}
548
549{
550 # Used to crash [perl #125350]
551 eval ('qq{@{[0}*sub{]]}}}=sub{0' . "\c[");
552 print "ok $test - 125350\n"; $test++;
553}
554
555{
556 # Used to crash [perl #128171]
557 eval ('/@0{0*->@*/*]');
558 print "ok $test - 128171\n"; $test++;
559}
560{
561  # various sub-parse recovery issues that crashed perl
562  eval 's//${sub{b{]]]{}#$/ sub{}';
563  print "ok $test - 132640\n"; $test++;
564  eval 'qq{@{sub{]]}}}};shift';
565  print "ok $test - 125351\n"; $test++;
566  eval 'qq{@{sub{]]}}}}-shift';
567  print "ok $test - 126192\n"; $test++;
568}
569
570$foo = "WRONG"; $foo:: = "bar"; $bar = "baz";
571print "not " unless "$foo::$bar" eq "barbaz";
572print qq|ok $test - [perl #128478] "\$foo::\$bar"\n|; $test++;
573@bar = ("baz","bonk");
574print "not " unless "$foo::@bar" eq "barbaz bonk";
575print qq|ok $test - [perl #128478] "\$foo::\@bar"\n|; $test ++;
576
577# Test that compilation of tentative indirect method call syntax which
578# turns out not to be such does not upgrade constants to full globs in the
579# symbol table.
580sub fop() { 0 }
581sub bas() { 0 }
582{ local $SIG{__WARN__}=sub{}; eval 'fop bas'; }
583print "not " unless ref $::{fop} eq 'SCALAR';
584print "ok $test - first constant in 'const1 const2' is not upgraded\n";
585$test++;
586print "not " unless ref $::{bas} eq 'SCALAR';
587print "ok $test - second constant in 'const1 const2' is not upgraded\n";
588$test++;
589