Perl如何解析未引用的裸词? [英] How does Perl parse unquoted bare words?

查看:97
本文介绍了Perl如何解析未引用的裸词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

未引号的单词在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.

  1. 在语法上预期的语法上定义的含义.

  1. 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}
    

  • 关键字.

  • Keyword.

    while (1) { }
    sub { }
    BEGIN { }
    use
    __END__
    

  • 变量名称,当整个取消引用表达式时.

  • Variable name, when the entirety of the dereference expression.

    ${foo}
    ${main::foo}
    

    请注意,使用命名运算符的名称将导致ambiguous use警告.

    Note that using the name of a named operator will result in an ambiguous use warning.

    子调用,当先前导入的子名称时使用.

    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;
    

  • 标签,当用作nextlastredogoto的操作数时.

  • 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)
    

  • 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!\n";         # Equivalent to print *FH ...;
    print main::FH "Hello, World!\n";   # 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参数的参数时.

  • When used as an argument for the a sub parameter with a prototype of *.

    sub myprint(*@);
    myprint(FH, "Hello, World\n");
    myprint(main::FH, "Hello, World\n");
    

  • 字符串文字. 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.

    这篇关于Perl如何解析未引用的裸词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆