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