xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/t/base/lex.t (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!./perl
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateprint "1..55\n";
4*0Sstevel@tonic-gate
5*0Sstevel@tonic-gate$x = 'x';
6*0Sstevel@tonic-gate
7*0Sstevel@tonic-gateprint "#1	:$x: eq :x:\n";
8*0Sstevel@tonic-gateif ($x eq 'x') {print "ok 1\n";} else {print "not ok 1\n";}
9*0Sstevel@tonic-gate
10*0Sstevel@tonic-gate$x = $#;	# this is the register $#
11*0Sstevel@tonic-gate
12*0Sstevel@tonic-gateif ($x eq '') {print "ok 2\n";} else {print "not ok 2\n";}
13*0Sstevel@tonic-gate
14*0Sstevel@tonic-gate$x = $#x;
15*0Sstevel@tonic-gate
16*0Sstevel@tonic-gateif ($x eq '-1') {print "ok 3\n";} else {print "not ok 3\n";}
17*0Sstevel@tonic-gate
18*0Sstevel@tonic-gate$x = '\\'; # ';
19*0Sstevel@tonic-gate
20*0Sstevel@tonic-gateif (length($x) == 1) {print "ok 4\n";} else {print "not ok 4\n";}
21*0Sstevel@tonic-gate
22*0Sstevel@tonic-gateeval 'while (0) {
23*0Sstevel@tonic-gate    print "foo\n";
24*0Sstevel@tonic-gate}
25*0Sstevel@tonic-gate/^/ && (print "ok 5\n");
26*0Sstevel@tonic-gate';
27*0Sstevel@tonic-gate
28*0Sstevel@tonic-gateeval '$foo{1} / 1;';
29*0Sstevel@tonic-gateif (!$@) {print "ok 6\n";} else {print "not ok 6 $@\n";}
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gateeval '$foo = 123+123.4+123e4+123.4E5+123.4e+5+.12;';
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate$foo = int($foo * 100 + .5);
34*0Sstevel@tonic-gateif ($foo eq 2591024652) {print "ok 7\n";} else {print "not ok 7 :$foo:\n";}
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gateprint <<'EOF';
37*0Sstevel@tonic-gateok 8
38*0Sstevel@tonic-gateEOF
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gate$foo = 'ok 9';
41*0Sstevel@tonic-gateprint <<EOF;
42*0Sstevel@tonic-gate$foo
43*0Sstevel@tonic-gateEOF
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gateeval <<\EOE, print $@;
46*0Sstevel@tonic-gateprint <<'EOF';
47*0Sstevel@tonic-gateok 10
48*0Sstevel@tonic-gateEOF
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate$foo = 'ok 11';
51*0Sstevel@tonic-gateprint <<EOF;
52*0Sstevel@tonic-gate$foo
53*0Sstevel@tonic-gateEOF
54*0Sstevel@tonic-gateEOE
55*0Sstevel@tonic-gate
56*0Sstevel@tonic-gateprint <<'EOS' . <<\EOF;
57*0Sstevel@tonic-gateok 12 - make sure single quotes are honored \nnot ok
58*0Sstevel@tonic-gateEOS
59*0Sstevel@tonic-gateok 13
60*0Sstevel@tonic-gateEOF
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gateprint qq/ok 14\n/;
63*0Sstevel@tonic-gateprint qq(ok 15\n);
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gateprint qq
66*0Sstevel@tonic-gate[ok 16\n]
67*0Sstevel@tonic-gate;
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gateprint q<ok 17
70*0Sstevel@tonic-gate>;
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gateprint <<;   # Yow!
73*0Sstevel@tonic-gateok 18
74*0Sstevel@tonic-gate
75*0Sstevel@tonic-gate# previous line intentionally left blank.
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gateprint <<E1 eq "foo\n\n" ? "ok 19\n" : "not ok 19\n";
78*0Sstevel@tonic-gate@{[ <<E2 ]}
79*0Sstevel@tonic-gatefoo
80*0Sstevel@tonic-gateE2
81*0Sstevel@tonic-gateE1
82*0Sstevel@tonic-gate
83*0Sstevel@tonic-gateprint <<E1 eq "foo\n\n" ? "ok 20\n" : "not ok 20\n";
84*0Sstevel@tonic-gate@{[
85*0Sstevel@tonic-gate  <<E2
86*0Sstevel@tonic-gatefoo
87*0Sstevel@tonic-gateE2
88*0Sstevel@tonic-gate]}
89*0Sstevel@tonic-gateE1
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gate$foo = FOO;
92*0Sstevel@tonic-gate$bar = BAR;
93*0Sstevel@tonic-gate$foo{$bar} = BAZ;
94*0Sstevel@tonic-gate$ary[0] = ABC;
95*0Sstevel@tonic-gate
96*0Sstevel@tonic-gateprint "$foo{$bar}" eq "BAZ" ? "ok 21\n" : "not ok 21\n";
97*0Sstevel@tonic-gate
98*0Sstevel@tonic-gateprint "${foo}{$bar}" eq "FOO{BAR}" ? "ok 22\n" : "not ok 22\n";
99*0Sstevel@tonic-gateprint "${foo{$bar}}" eq "BAZ" ? "ok 23\n" : "not ok 23\n";
100*0Sstevel@tonic-gate
101*0Sstevel@tonic-gateprint "FOO:" =~ /$foo[:]/ ? "ok 24\n" : "not ok 24\n";
102*0Sstevel@tonic-gateprint "ABC" =~ /^$ary[$A]$/ ? "ok 25\n" : "not ok 25\n";
103*0Sstevel@tonic-gateprint "FOOZ" =~ /^$foo[$A-Z]$/ ? "ok 26\n" : "not ok 26\n";
104*0Sstevel@tonic-gate
105*0Sstevel@tonic-gate# MJD 19980425
106*0Sstevel@tonic-gate($X, @X) = qw(a b c d);
107*0Sstevel@tonic-gateprint "d" =~ /^$X[-1]$/ ? "ok 27\n" : "not ok 27\n";
108*0Sstevel@tonic-gateprint "a1" !~ /^$X[-1]$/ ? "ok 28\n" : "not ok 28\n";
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gateprint (((q{{\{\(}} . q{{\)\}}}) eq '{{\(}{\)}}') ? "ok 29\n" : "not ok 29\n");
111*0Sstevel@tonic-gate
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate$foo = "not ok 30\n";
114*0Sstevel@tonic-gate$foo =~ s/^not /substr(<<EOF, 0, 0)/e;
115*0Sstevel@tonic-gate  Ignored
116*0Sstevel@tonic-gateEOF
117*0Sstevel@tonic-gateprint $foo;
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gate# Tests for new extended control-character variables
120*0Sstevel@tonic-gate# MJD 19990227
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gate{ my $CX = "\cX";
123*0Sstevel@tonic-gate  my $CXY  ="\cXY";
124*0Sstevel@tonic-gate  $ {$CX} = 17;
125*0Sstevel@tonic-gate  $ {$CXY} = 23;
126*0Sstevel@tonic-gate  if ($ {^XY} != 23) { print "not "  }
127*0Sstevel@tonic-gate  print "ok 31\n";
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gate# Does the syntax where we use the literal control character still work?
130*0Sstevel@tonic-gate  if (eval "\$ {\cX}" != 17 or $@) { print "not "  }
131*0Sstevel@tonic-gate  print "ok 32\n";
132*0Sstevel@tonic-gate
133*0Sstevel@tonic-gate  eval "\$\cQ = 24";                 # Literal control character
134*0Sstevel@tonic-gate  if ($@ or ${"\cQ"} != 24) {  print "not "  }
135*0Sstevel@tonic-gate  print "ok 33\n";
136*0Sstevel@tonic-gate  if ($^Q != 24) {  print "not "  }  # Control character escape sequence
137*0Sstevel@tonic-gate  print "ok 34\n";
138*0Sstevel@tonic-gate
139*0Sstevel@tonic-gate# Does the old UNBRACED syntax still do what it used to?
140*0Sstevel@tonic-gate  if ("$^XY" ne "17Y") { print "not " }
141*0Sstevel@tonic-gate  print "ok 35\n";
142*0Sstevel@tonic-gate
143*0Sstevel@tonic-gate  sub XX () { 6 }
144*0Sstevel@tonic-gate  $ {"\cQ\cXX"} = 119;
145*0Sstevel@tonic-gate  $^Q = 5; #  This should be an unused ^Var.
146*0Sstevel@tonic-gate  $N = 5;
147*0Sstevel@tonic-gate  # The second caret here should be interpreted as an xor
148*0Sstevel@tonic-gate  if (($^Q^XX) != 3) { print "not " }
149*0Sstevel@tonic-gate  print "ok 36\n";
150*0Sstevel@tonic-gate#  if (($N  ^  XX()) != 3) { print "not " }
151*0Sstevel@tonic-gate#  print "ok 32\n";
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate  # These next two tests are trying to make sure that
154*0Sstevel@tonic-gate  # $^FOO is always global; it doesn't make sense to `my' it.
155*0Sstevel@tonic-gate  #
156*0Sstevel@tonic-gate
157*0Sstevel@tonic-gate  eval 'my $^X;';
158*0Sstevel@tonic-gate  print "not " unless index ($@, 'Can\'t use global $^X in "my"') > -1;
159*0Sstevel@tonic-gate  print "ok 37\n";
160*0Sstevel@tonic-gate#  print "($@)\n" if $@;
161*0Sstevel@tonic-gate
162*0Sstevel@tonic-gate  eval 'my $ {^XYZ};';
163*0Sstevel@tonic-gate  print "not " unless index ($@, 'Can\'t use global $^XYZ in "my"') > -1;
164*0Sstevel@tonic-gate  print "ok 38\n";
165*0Sstevel@tonic-gate#  print "($@)\n" if $@;
166*0Sstevel@tonic-gate
167*0Sstevel@tonic-gate# Now let's make sure that caret variables are all forced into the main package.
168*0Sstevel@tonic-gate  package Someother;
169*0Sstevel@tonic-gate  $^Q = 'Someother';
170*0Sstevel@tonic-gate  $ {^Quixote} = 'Someother 2';
171*0Sstevel@tonic-gate  $ {^M} = 'Someother 3';
172*0Sstevel@tonic-gate  package main;
173*0Sstevel@tonic-gate  print "not " unless $^Q eq 'Someother';
174*0Sstevel@tonic-gate  print "ok 39\n";
175*0Sstevel@tonic-gate  print "not " unless $ {^Quixote} eq 'Someother 2';
176*0Sstevel@tonic-gate  print "ok 40\n";
177*0Sstevel@tonic-gate  print "not " unless $ {^M} eq 'Someother 3';
178*0Sstevel@tonic-gate  print "ok 41\n";
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gate
181*0Sstevel@tonic-gate}
182*0Sstevel@tonic-gate
183*0Sstevel@tonic-gate# see if eval '', s///e, and heredocs mix
184*0Sstevel@tonic-gate
185*0Sstevel@tonic-gatesub T {
186*0Sstevel@tonic-gate    my ($where, $num) = @_;
187*0Sstevel@tonic-gate    my ($p,$f,$l) = caller;
188*0Sstevel@tonic-gate    print "# $p:$f:$l vs /$where/\nnot " unless "$p:$f:$l" =~ /$where/;
189*0Sstevel@tonic-gate    print "ok $num\n";
190*0Sstevel@tonic-gate}
191*0Sstevel@tonic-gate
192*0Sstevel@tonic-gatemy $test = 42;
193*0Sstevel@tonic-gate
194*0Sstevel@tonic-gate{
195*0Sstevel@tonic-gate# line 42 "plink"
196*0Sstevel@tonic-gate    local $_ = "not ok ";
197*0Sstevel@tonic-gate    eval q{
198*0Sstevel@tonic-gate	s/^not /<<EOT/e and T '^main:\(eval \d+\):2$', $test++;
199*0Sstevel@tonic-gate# fuggedaboudit
200*0Sstevel@tonic-gateEOT
201*0Sstevel@tonic-gate        print $_, $test++, "\n";
202*0Sstevel@tonic-gate	T('^main:\(eval \d+\):6$', $test++);
203*0Sstevel@tonic-gate# line 1 "plunk"
204*0Sstevel@tonic-gate	T('^main:plunk:1$', $test++);
205*0Sstevel@tonic-gate    };
206*0Sstevel@tonic-gate    print "# $@\nnot ok $test\n" if $@;
207*0Sstevel@tonic-gate    T '^main:plink:53$', $test++;
208*0Sstevel@tonic-gate}
209*0Sstevel@tonic-gate
210*0Sstevel@tonic-gate# tests 47--51 start here
211*0Sstevel@tonic-gate# tests for new array interpolation semantics:
212*0Sstevel@tonic-gate# arrays now *always* interpolate into "..." strings.
213*0Sstevel@tonic-gate# 20000522 MJD (mjd@plover.com)
214*0Sstevel@tonic-gate{
215*0Sstevel@tonic-gate  my $test = 47;
216*0Sstevel@tonic-gate  eval(q(">@nosuch<" eq "><")) || print "# $@", "not ";
217*0Sstevel@tonic-gate  print "ok $test\n";
218*0Sstevel@tonic-gate  ++$test;
219*0Sstevel@tonic-gate
220*0Sstevel@tonic-gate  # Look at this!  This is going to be a common error in the future:
221*0Sstevel@tonic-gate  eval(q("fred@example.com" eq "fred.com")) || print "# $@", "not ";
222*0Sstevel@tonic-gate  print "ok $test\n";
223*0Sstevel@tonic-gate  ++$test;
224*0Sstevel@tonic-gate
225*0Sstevel@tonic-gate  # Let's make sure that normal array interpolation still works right
226*0Sstevel@tonic-gate  # For some reason, this appears not to be tested anywhere else.
227*0Sstevel@tonic-gate  my @a = (1,2,3);
228*0Sstevel@tonic-gate  print +((">@a<" eq ">1 2 3<") ? '' : 'not '), "ok $test\n";
229*0Sstevel@tonic-gate  ++$test;
230*0Sstevel@tonic-gate
231*0Sstevel@tonic-gate  # Ditto.
232*0Sstevel@tonic-gate  eval(q{@nosuch = ('a', 'b', 'c'); ">@nosuch<" eq ">a b c<"})
233*0Sstevel@tonic-gate      || print "# $@", "not ";
234*0Sstevel@tonic-gate  print "ok $test\n";
235*0Sstevel@tonic-gate  ++$test;
236*0Sstevel@tonic-gate
237*0Sstevel@tonic-gate  # This isn't actually a lex test, but it's testing the same feature
238*0Sstevel@tonic-gate  sub makearray {
239*0Sstevel@tonic-gate    my @array = ('fish', 'dog', 'carrot');
240*0Sstevel@tonic-gate    *R::crackers = \@array;
241*0Sstevel@tonic-gate  }
242*0Sstevel@tonic-gate
243*0Sstevel@tonic-gate  eval(q{makearray(); ">@R::crackers<" eq ">fish dog carrot<"})
244*0Sstevel@tonic-gate    || print "# $@", "not ";
245*0Sstevel@tonic-gate  print "ok $test\n";
246*0Sstevel@tonic-gate  ++$test;
247*0Sstevel@tonic-gate}
248*0Sstevel@tonic-gate
249*0Sstevel@tonic-gate# Tests 52-54
250*0Sstevel@tonic-gate# => should only quote foo::bar if it isn't a real sub. AMS, 20010621
251*0Sstevel@tonic-gate
252*0Sstevel@tonic-gatesub xyz::foo { "bar" }
253*0Sstevel@tonic-gatemy %str = (
254*0Sstevel@tonic-gate    foo      => 1,
255*0Sstevel@tonic-gate    xyz::foo => 1,
256*0Sstevel@tonic-gate    xyz::bar => 1,
257*0Sstevel@tonic-gate);
258*0Sstevel@tonic-gate
259*0Sstevel@tonic-gatemy $test = 52;
260*0Sstevel@tonic-gateprint ((exists $str{foo}      ? "" : "not ")."ok $test\n"); ++$test;
261*0Sstevel@tonic-gateprint ((exists $str{bar}      ? "" : "not ")."ok $test\n"); ++$test;
262*0Sstevel@tonic-gateprint ((exists $str{xyz::bar} ? "" : "not ")."ok $test\n"); ++$test;
263*0Sstevel@tonic-gate
264*0Sstevel@tonic-gatesub foo::::::bar { print "ok $test\n"; $test++ }
265*0Sstevel@tonic-gatefoo::::::bar;
266