Perl 如何解析未引用的裸词? [英] How does Perl parse unquoted bare words?
问题描述
未引用的单词在 Perl 中似乎有很多含义.
Unquoted words seem to have a great many meanings in Perl.
print STDERR $msg;
$hash{key}
func( param => $arg )
my $x = str;
如何确定这些的含义?
推荐答案
下图显示了 Perl 如何按优先级降序解析标识符.
The following chart shows how Perl resolves identifiers in order of descending priority.
除非另有说明,否则它也适用于由 ::
链接的标识符(我将其称为限定标识符").
It also applies to identifiers chained by ::
(which I'll call "qualified identifiers") unless otherwise stated.
语法上定义的含义,在语法上符合预期.
Syntactically-defined meaning, when syntactically expected.
sub foo { } # «foo» («sub» is covered later)
sub main::foo { } # «main::foo» («sub» is covered later)
method Class # «Class» («method» is covered later)
method Some::Class # «Some::Class» («method» is covered later)
$foo
$main::foo
//i
=head
<<FOO
Class::
Some::Class::
LABEL:
字符串文字,后跟 =>
或整个哈希索引表达式时.
String literal, when followed by a =>
or when the entirety of a hash index expression.
这不适用于限定标识符.
my %h = ( a => 1 );
$h{a}
变量名,当整个解引用表达式时.
Variable name, when the entirety of the dereference expression.
${foo}
${main::foo}
请注意,使用关键字、命名运算符或声明的 sub 的名称将导致歧义使用
警告.
Note that using the name of a keyword, named operator or declared sub will result in an ambiguous use
warning.
关键字.
while (1) { }
sub { }
use
__END__
Sub 调用,当之前导入的 sub 名称时.
Sub call, when the name of a previously imported sub.
use Time::HiRes qw( time );
time
main::time
调用命名列表运算符、命名一元运算符或命名空运算符.
Invocation of a named list operator, named unary operator or named nullary operator.
print $x, $y, $z;
$c = chr $i;
$t = time;
$t = CORE::time;
标签,当用作next
、last
、redo
或goto
的操作数时.
Label, when used as the operand for next
, last
, redo
or goto
.
将限定标识符视为标签会导致编译错误,因为标签不能是限定标识符.
next LABEL;
子调用或内联常量,当为先前声明的子或常量的名称时.
Sub call or inlined constant, when the name of a previously declared sub or constant.
sub foo { }
foo # Calls sub «foo»
main::foo # Calls sub «foo»
sub bar;
bar # Calls sub «bar»
use constant FOO => 123;
FOO # Replaced with the value of the constant.
间接方法调用,当后跟一个可能限定的标识符、一个以 ::
为后缀的可能限定的标识符、一个标量(包括数组元素或散列元素)或一个块时.
Indirect method call, when followed by a possibly-qualified identifier, a possibly-qualified identifier suffixed with ::
, a scalar (incl array element or hash element) or a block.
method Class # Calls method «method» («Class» is covered earlier)
method Some::Class # Calls method «method» («Some::Class» is covered earlier)
method Class:: # Calls method «method» («Class» is covered earlier)
method Some::Class:: # Calls method «method» («Some::Class» is covered earlier)
method $o # Calls method «method»
method { $o } # Calls method «method»
Base::method Class # Calls method «Base::method» («Class» is covered earlier)
您可以使用 无间接
pragma 警告代码何时以这种方式解析.
You can use the no indirect
pragma to warn when code is parsed this way.
Glob,当用作需要文件句柄的运算符的操作数时.
Glob, when used as the operand for an operator expecting a file handle.
open(FH, '>', $qfn) or die $!; # Equivalent to open(*FH, ...) or ...;
print FH "Hello, World!
"; # Equivalent to print *FH ...;
print main::FH "Hello, World!
"; # Equivalent to print *main::FH ...;
字符串字面量,在以下情况下:
String literal, in the following situations:
用作直接方法调用的调用者时.
When used as the invocant of a direct method call.
Class->method(@args) # Uses the string «Class» as the invocant.
Some::Class->method(@args) # Uses the string «Some::Class» as the invocant.
用作一元减号的操作数时.
When used as the operand for unary minus.
-foo
-foo::bar
当用作原型为 *
的子参数的参数时.
sub myprint(*@);
myprint(FH, "Hello, World
");
myprint(main::FH, "Hello, World
");
字符串字面量.use strict qw( subs );
不允许这样做.
String literal. This is disallowed by use strict qw( subs );
.
希望我没有错过任何一个.
Hopefully, I didn't miss any.
感谢@mosvy、@Grinnz 和@stevesliva!每个人都发现了一些我遗漏的案例.
Thanks to @mosvy, @Grinnz and @stevesliva! Each has uncovered a few cases I had missed.
目前缺少:
funcname
insort funcname
.
BEGIN
和类似的.它们有时充当关键字,有时充当声明的子项.
BEGIN
and similar. They sometimes act as keyword, and sometimes as a declared sub.
导入名为 print
的 sub 不遵循上述步骤.
Importing a sub named print
doesn't follow the above steps.
$ perl -M5.010 -e'
use subs qw( time );
eval { time; };
say $@ =~ /Undefined sub/ ? "ok" : "bad";
'
ok
$ perl -M5.010 -e'
use subs qw( system );
eval { system; };
say $@ =~ /Undefined sub/ ? "ok" : "bad";
'
ok
$ perl -M5.010 -e'
use subs qw( print );
eval { print; };
say $@ =~ /Undefined sub/ ? "ok" : "bad";
'
bad
我不知道是什么让那个特别,我不知道是否还有其他的.我猜是因为 print
没有原型,但 system
也没有原型.
I don't know what makes that one special, and I don't know if there are others or not. I was guessing it's because print
doesn't have a prototype, but system
has no prototype either.
这篇关于Perl 如何解析未引用的裸词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!