Perl5 = (equal) operator priority

 $a,$b,$c = 1,2,3;
 print "$a, $b, $c\n";

returns

 , , 1

So, has value = (equal to) a higher priority than building a tuple - by doing this?

 $a,$b,($c=1),2,3;
+3
source share
2 answers

Yes. There is a priority table in perlop . Assignment operators are level 19, and commas are level 20. In general, Perl operators have the same priority as the corresponding C operators (for those operators that have the corresponding C operator).

If you had in mind ($a,$b,$c) = (1,2,3);, you should use parens.

+8
source

The comma operator, as you used it (in a scalar context), is not for constructing a tuple, it is for evaluating several expressions and returning the latter.

Perl - , , , , , , ... . perldoc perldata .

, :

perl -e '$a = (1 and 4,2,0); print"$a\n"'

0, 4,2,0 C, .

4,2,0 :

perl -e '$a = (1 and @a=(4,2,0)); print"$a\n"'

3, ( cjm) (, RHS and ) - , ( and Perl , ).

, cjm, :

($a,$b,$c) = (1,2,3);

.

:

$ perl -e '$a,$b,$c = (7,6,8); print "$a $b $c\n"'
8

8.

$ perl -e '($a,$b,$c) = (7,6,8); print "$a $b $c\n"'
7 6 8

.

$ perl -e '$a,$b,$c = () = (7,6,8); print "$a $b $c\n"'
3

, , $c , .

+2

All Articles