This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
test.pl: Allow NAME to be used with --FILE--
[perl5.git] / t / uni / parser.t
CommitLineData
1a96a5b7
BF
1#!./perl
2
3# Checks if the parser behaves correctly in edge cases
4# (including weird syntax errors)
5
6BEGIN {
7 require './test.pl';
8}
9
86fe3f36 10plan (tests => 47);
1a96a5b7
BF
11
12use utf8;
13use open qw( :utf8 :std );
14
15ok *tèst, "*main::tèst", "sanity check.";
16ok $::{"tèst"}, "gets the right glob in the stash.";
17
18my $glob_by_sub = sub { *main::method }->();
19
20is *main::method, "*main::method", "glob stringy works";
21is "" . *main::method, "*main::method", "glob stringify-through-concat works";
22is $glob_by_sub, "*main::method", "glob stringy works";
23is "" . $glob_by_sub, "*main::method", "";
24
25sub gimme_glob {
26 no strict 'refs';
27 is *{$_[0]}, "*main::$_[0]";
28 *{$_[0]};
29}
30
31is "" . gimme_glob("下郎"), "*main::下郎";
32$a = *下郎;
33is "" . $a, "*main::下郎";
34
35*{gimme_glob("下郎")} = sub {};
36
37{
38 ok defined *{"下郎"}{CODE};
39 ok !defined *{"\344\270\213\351\203\216"}{CODE};
40}
41
42$Lèon = 1;
43is ${*Lèon{SCALAR}}, 1, "scalar define in the right glob,";
44ok !${*{"L\303\250on"}{SCALAR}}, "..and nothing in the wrong one.";
45
46my $a = "foo" . chr(190);
47my $b = $a . chr(256);
48chop $b; # $b is $a with utf8 on
49
50is $a, $b, '$a equals $b';
51
52*$b = sub { 5 };
53
54is eval { main->$a }, 5, q!$a can call $b's sub!;
55ok !$@, "..and there's no error.";
56
57my $c = $b;
58utf8::encode($c);
59ok $b ne $c, '$b unequal $c';
60eval { main->$c };
61ok $@, q!$c can't call $b's sub.!;
62
63# Now define another sub under the downgraded name:
64*$a = sub { 6 };
65# Call it:
66is eval { main->$a }, 6, "Adding a new sub to *a and calling it works,";
67ok !$@, "..without errors.";
68eval { main->$c };
69ok $@, "but it's still unreachable through *c";
70
71*$b = \10;
72is ${*$a{SCALAR}}, 10;
73is ${*$b{SCALAR}}, 10;
74is ${*$c{SCALAR}}, undef;
75
76opendir FÒÒ, ".";
77closedir FÒÒ;
78::ok($::{"FÒÒ"}, "Bareword generates the right glob.");
79::ok(!$::{"F\303\222\303\222"});
80
81sub участники { 1 }
82
83ok $::{"участники"}, "non-const sub declarations generate the right glob";
84ok *{$::{"участники"}}{CODE};
85is *{$::{"участники"}}{CODE}->(), 1;
86
3453414d
BF
87sub 原 () { 1 }
88
89is grep({ $_ eq "\x{539f}" } keys %::), 1, "Constant subs generate the right glob.";
90is grep({ $_ eq "\345\216\237" } keys %::), 0;
91
108f32a5
BF
92#These should probably go elsewhere.
93eval q{ sub wròng1 (_$); wròng1(1,2) };
94like( $@, qr/Malformed prototype for main::wròng1/, 'Malformed prototype croak is clean.' );
95
96eval q{ sub ча::ики ($__); ча::ики(1,2) };
97like( $@, qr/Malformed prototype for ча::ики/ );
98
23b0eed2
BF
99our $問 = 10;
100is $問, 10, "our works";
101is $main::問, 10, "...as does getting the same variable through the fully qualified name";
102is ${"main::\345\225\217"}, undef, "..and using the encoded form doesn't";
e2f06df0
BF
103
104{
105 use charnames qw( :full );
106
107 eval qq! my \$\x{30cb} \N{DROMEDARY CAMEL} !;
055745ba 108 $@ =~ s/eval \d+/eval 11/;
08454bd8 109 is $@, 'Unrecognized character \x{1f42a}; marked by <-- HERE after my $ニ <-- HERE near column 8 at (eval 11) line 1.
e2f06df0
BF
110', "'Unrecognized character' croak is UTF-8 clean";
111}
3c54b17a
BF
112
113{
114 use feature 'state';
115 for ( qw( my state our ) ) {
116 local $@;
117 eval "$_ Foo $x = 1;";
118 like $@, qr/No such class Foo/u, "'No such class' warning for $_ is UTF-8 clean";
119 }
4bca4ee0
BF
120}
121
122{
123 local $@;
124 eval "our \$main::\x{30cb};";
125 like $@, qr!No package name allowed for variable \$main::\x{30cb} in "our"!, "'No such package name allowed for variable' is UTF-8 clean";
126}
58576270
BF
127
128{
129 use feature 'state';
130 local $@;
131 for ( qw( my state ) ) {
132 eval "$_ \$::\x{30cb};";
133 like $@, qr!"$_" variable \$::\x{30cb} can't be in a package!, qq!'"$_" variable %s can't be in a package' is UTF-8 clean!;
134 }
135}
d0fb66e4
BF
136
137{
138 local $@;
139 eval qq!print \x{30cb}, "comma""!;
140 like $@, qr/No comma allowed after filehandle/, "No comma allowed after filehandle triggers correctly for UTF-8 filehandles.";
141}
86fe3f36
BF
142
143# tests for "Bad name"
144eval q{ Foo::$bar };
145like( $@, qr/Bad name after Foo::/, 'Bad name after Foo::' );
146eval q{ Foo''bar };
147like( $@, qr/Bad name after Foo'/, 'Bad name after Foo\'' );