What does ($ a, $ b, $ c) = @array mean in Perl?

I would do it if I could, but to be honest, I don’t know what to look for (an inherent problem with character-heavy languages)!

($aSvnRemote, $aSvnLocal, $aSvnRef, $aSvnOptions) = @{$aSvnPair}; 

I assume $aSvnPair is an array of 4 values ​​(in this case, it is a very weakly named variable!), And that just breaks it down into specific variables ...?

+4
source share
3 answers

This is nothing more than a list assignment. The first RHS value is assigned to the first var for LHS, etc. It means

 ($aSvnRemote, $aSvnLocal, $aSvnRef, $aSvnOptions) = @{$aSvnPair}; 

coincides with

 $aSvnRemote = $aSvnPair->[0]; $aSvnLocal = $aSvnPair->[1]; $aSvnRef = $aSvnPair->[2]; $aSvnOptions = $aSvnPair->[3]; 
+10
source

The $aSvnPair is an array reference. Adding the @ character causes the array to bind. In this example, the array is unpacked and its elements are assigned to the variables on the right.

Here is an example of what is happening:

 $aSvnPair= [ qw(foo bar baz xyxxy) ]; ($aSvnRemote, $aSvnLocal, $aSvnRef, $aSvnOptions) = @{$aSvnPair}; 

After this operation, you will receive the following:

 $aSvnRemote = "foo"; $aSvnLocal = "bar"; $aSvnRef = "baz"; $aSvnOptions = "xyxxy"; 
+6
source

$aSvnPair must be an array reference, so @{$aSvnPair} it. (A "link" is the equivalent of a Perl pointer.)

Then the operator assigns the values ​​of this array to the four variables on the left side in order.

See this tutorial for some examples: Perl dereferencing

+1
source

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


All Articles