What pseudo-operators exist in Perl 5?

后端 未结 9 1514
孤城傲影
孤城傲影 2020-11-28 07:05

I am currently documenting all of Perl 5\'s operators (see the perlopref GitHub project) and I have decided to include Perl 5\'s pseudo-operators as well. To me, a pseudo-o

相关标签:
9条回答
  • 2020-11-28 07:06

    The << >> operator, for multi-line comments:

    <<q==q>>;
        This is a
        multiline
        comment
    q
    
    0 讨论(0)
  • 2020-11-28 07:08

    Don't forget the Flaming X-Wing =<>=~.

    The Fun With Perl mailing list will prove useful for your research.

    0 讨论(0)
  • 2020-11-28 07:08

    From this question, I discovered the %{{}} operator to cast a list as a hash. Useful in contexts where a hash argument (and not a hash assignment) are required.

    @list = (a,1,b,2);
    print values @list;        # arg 1 to values must be hash (not array dereference)
    print values %{@list}      # prints nothing
    print values (%temp=@list) # arg 1 to values must be hash (not list assignment)
    print values %{{@list}}    # success: prints 12
    

    If @list does not contain any duplicate keys (odd-elements), this operator also provides a way to access the odd or even elements of a list:

    @even_elements = keys %{{@list}}     # @list[0,2,4,...]
    @odd_elements = values %{{@list}}    # @list[1,3,5,...]
    
    0 讨论(0)
  • 2020-11-28 07:11

    In Perl these are generally referred to as "secret operators".

    A partial list of "secret operators" can be had here. The best and most complete list is probably in possession of Philippe Bruhad aka BooK and his Secret Perl Operators talk but I don't know where its available. You might ask him. You can probably glean some more from Obfuscation, Golf and Secret Operators.

    0 讨论(0)
  • How about the "Boolean one-or-zero" operator: 1&!!

    For example:

    my %result_of = (
        " 1&!! '0 but true' " => 1&!! '0 but true',
        " 1&!! '0'          " => 1&!! '0',
        " 1&!! 'text'       " => 1&!! 'text',
        " 1&!! 0            " => 1&!! 0,
        " 1&!! 1            " => 1&!! 1,
        " 1&!! undef        " => 1&!! undef,
    );
    
    for my $expression ( sort keys %result_of){
        print "$expression = " . $result_of{$expression} . "\n";
    }
    

    gives the following output:

     1&!! '0 but true'  = 1
     1&!! '0'           = 0
     1&!! 'text'        = 1
     1&!! 0             = 0
     1&!! 1             = 1
     1&!! undef         = 0
    
    0 讨论(0)
  • 2020-11-28 07:16

    You have two "countof" (pseudo-)operators, and I don't really see the difference between them.

    From the examples of "the countof operator":

    my $count = ()= f();              #$count is now 5
    my $string = "cat cat dog cat";
    my $cats = ()= $string =~ /cat/g; #$cats is now 3
    

    From the examples of "the goatse/countof operator":

    my $count =()= f();              #$count is now 5
    my $string = "cat cat dog cat";
    my $cats =()= $string =~ /cat/g; #$cats is now 3
    

    Both sets of examples are identical, modulo whitespace. What is your reasoning for considering them to be two distinct pseudo-operators?

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