What is “%_” in perl?

前端 未结 1 1988
滥情空心
滥情空心 2020-12-08 10:29

I\'ve just been given a code snippet:

@list = grep { !$_{$_}++ } @list;

As an idiom for deduplication. It seems to work, but - there\'s no

相关标签:
1条回答
  • 2020-12-08 10:37

    It's a hash. You can have a hash named _ because _ is a valid name for a variable. (I'm sure you are familiar with $_ and @_.)

    No Perl builtin currently sets it or reads %_ implicitly, but punctuation variables such as %_ are reserved.

    Perl variable names may also be a sequence of digits or a single punctuation or control character (with the literal control character form deprecated). These names are all reserved for special uses by Perl


    Note that punctuation variables are also special in that they are "super globals". This means that unqualified %_ refers to %_ in the root package, not %_ in the current package.

    $ perl -E'
       %::x    = ( name => "%::x"    );
       %::_    = ( name => "%::_"    );
       %Foo::x = ( name => "%Foo::x" );
       %Foo::_ = ( name => "%Foo::_" );
    
       package Foo;
    
       say "%::x    = $::x{name}";
       say "%::_    = $::_{name}";
       say "%Foo::x = $Foo::x{name}";
       say "%Foo::_ = $Foo::_{name}";
    
       say "%x      = $x{name}";
       say "%_      = $_{name}";
    '
    %::x    = %::x
    %::_    = %::_
    %Foo::x = %Foo::x
    %Foo::_ = %Foo::_
    %x      = %Foo::x
    %_      = %::_      <-- surprise!
    

    This means that forgetting to use local %_ (as you did) can have very far-reaching effects.

    0 讨论(0)
提交回复
热议问题