I'll just leave it here

    I do not set a goal to denigrate Pearl. I love Pearl.
    You just need to distinguish between a list and an array in Pearl.

    use Perl or die;
    


    #!/usr/bin/perl
    use strict;
    my $x = ('a', 'b');
    print $x;
    

    Conclusion:
    b



    #!/usr/bin/perl
    use strict;
    my @a = ('a', 'b');
    my $x = @a;
    print $x;
    

    Conclusion:
    2


    #!/usr/bin/perl
    my @a = ('a', 'b');
    my ($x) = @a;
    print $x;
    

    Conclusion:
    a


    #!/usr/bin/perl
    my @a = ('a', 'b');
    print scalar @a;
    

    Conclusion:
    2


    #!/usr/bin/perl
    print scalar ('a', 'b');
    

    Conclusion:
    b


    #!/usr/bin/perl
    $\ = "\n";
    my $x = ('a', 'b', 'c');
    print "x = $x";
    my $x = @{['a', 'b', 'c']};
    print "x = $x";
    my $x = (@{['a', 'b', 'c']});
    print "x = $x"; 
    


    Conclusion:
    x = c
    x = 3
    x = 3


    #!/usr/bin/perl
    use strict;
    my $x;
    my $y;
    $y = ($x = ('a', 'b', 'c'));
    print "x = $x\n";
    print "y = $y\n";
    

    x = с
    y = с

    #!/usr/bin/perl
    use strict;
    my $x;
    my $y;
    $y = (($x) = ('a', 'b', 'c'));
    print "x = $x\n";
    print "y = $y\n";
    

    x = a
    y = 3

    I do not set a goal to denigrate Pearl. I love Pearl.

    use Perl or die;
    


    Details here: perldoc perldata, read from “List value constructors”



    Offtop, from conversations with different people I learned a couple more examples:
    #!/usr/bin/perl
    use strict;
    use Data::Dumper;
    sub test1 {
    	my %hash;
    	$hash{shift} = 1;
    	print Dumper \%hash;
    }
    sub test2 {
    	my %hash;
    	$hash{+shift} = 1;
    	print Dumper \%hash;
    }
    test1('test');
    test2('test');
    

    Conclusion:

    $ VAR1 = {
              'shift' => 1
            };
    $ VAR1 = {
              'test' => 1
            };
    


    in hashes, the key name does not have to be quoted, it immediately appears as a string, the unary plus also calculates the + shift operation, that is, $ _ [x], where x is the index in the @_ array


    s ggggg;
    

    It is equivalent
    s///g;
    


    the s operator allows you to replace / delimiters with other characters, the character g
    is selected here, that is, it is s /// gg;, which in turn is equivalent to just s /// g; Obviously, there is a space after s so that the pearl can distinguish this operator from calling some sggggg function; It is also obvious that a space cannot be a separator instead of /

    Again, I remind you of the O = Deparse module, about which I spoke in my article Single-line Perl programs , which will show everything:
    perl -MO=Deparse -le "s ggggg;"
    

    BEGIN {$ / = "\ n"; $ \ = "\ n"; }
    s /// g;
    -e syntax OK
    




    #!/usr/bin/perl
    use strict;
    use Data::Dumper;
    use constant key => 1;
    my %hash;
    $hash{key} = 1;
    print Dumper \%hash;
    my %hash2;
    $hash2{+key} = 1;
    print Dumper \%hash2;
    my %hash3;
    $hash3{-key} = 1;
    print Dumper \%hash3;
    

    Conclusion:
    $ VAR1 = {
              'key' => 1
            };
    $ VAR1 = {
              '1' => 1
            };
    $ VAR1 = {
              '-key' => 1
            };
    

    Unary "-" performs arithmetic negation if the operand is numeric. If the operand is an identifier, a string consisting of a minus sign concatenated with the identifier is returned.

    Minus works as a minus if it is in front of a number. If it comes before the identifier, then a string consisting of minus and identifier is returned.

    UPD : Probably at first they misunderstood me and began to minus me, added the phrase “I do not set a goal ...”

    UPD 2 :
    About the last example. The documentation says
    "List assignment in scalar context returns the number of elements
    produced by the expression on the right side of the assignment"

    that is, if we assign something to the list, then the number of elements that we assign to the list will be returned.
     $x = (($foo,$bar) = f());
    

    $ x relies on the number of elements returned by the f () function.
    UPD 3 : I see that people have opened minuscules, so most likely I will post the article in draft form during the day (maybe at 15:00) so as not to lose karma.
    If you liked the article, save it.
    UPD 4 : Karma is not so bad, I thank those who supported me, added more examples with hash {shift} and s ggggg;
    UPD 5 (08.28.2012) : Added an example with a unary minus.

    Also popular now: