📜 ⬆️ ⬇️

I'll just leave it here

I do not set a goal to tarnish Pearl. I love Pearl.
Just need to distinguish between a list and an array in Perl.

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 tarnish Pearl. I love Pearl.

 use Perl or die; 


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



Offtopic, from conversations with different people I learned a couple of 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 is not necessarily quoted, it is immediately presented as a string, the unary plus also calculates the + shift operation, that is, $ _ [x], where x is the index in the @_ array


 s ggggg; 

This is equivalent to
 s///g; 


Operator s allows to replace / delimiters with other characters, 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 function sggggg; It is also obvious that the space cannot be a separator instead of /

Again, I remind you about the O = Deparse module, about which I spoke in my article One-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 minus sign is concatenated with the identifier is returned.

Minus works as a minus if it is before a number. If there is an identifier, it returns a string consisting of a minus and an identifier.

UPD : Probably at the beginning I was misunderstood and began to minus, added the phrase "I do not set a goal ..."

UPD 2 :
About the last example. The documentation is written
"List assignment in scalar context returns the number of elements
produced by 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()); 

the number of elements returned by the f () function is set in $ x
UPD 3 : I see that people revealed minus-movers, so most likely I would put the article in drafts 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, thanks to 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.

Source: https://habr.com/ru/post/150140/


All Articles