Assigning anonymous hash to a list

Joseph Werner telcodev at gmail.com
Tue Jul 30 21:33:33 BST 2013


I still disagree.  This is a straightforward assignment to the first
element of a list.

In Perl, if you assign a scalar value to a list, the first variable in
the list will take that value, if it is assignable:

my ($i1, $i2, $i3) = 4;

say "\$i1 = ", $i1;
say "\$i2 = ", $i2;
say "\$i3 = ", $i3;

which gives:

$i1 = 4
Use of uninitialized value $i2 in say at test.pl line 11.
$i2 =
Use of uninitialized value $i3 in say at test.pl line 12.
$i3 =

Here I have assigned the scalar value 4 to the list element $i1, which
is assignable.

The comma operator is a valid component of a Perl expression.

my $str = 'text', {a => 1, b => 2, c => 3};
say $str;

which gives:

Useless use of anonymous hash ({}) in void context at test.pl line 5.
text

Again, this is a simple assignment of a scalar value to the first
element of a list, precedence is not involved.

Christian

On Tue, Jul 30, 2013 at 3:49 PM, Abigail <abigail at abigail.be> wrote:
> On Tue, Jul 30, 2013 at 03:34:48PM -0400, Joseph Werner wrote:
>> I disagree.
>>
>> This is a straightforward  assignment to the first element of a list.
>> Precedence is not involved. A scalar assignment vs a list assignment
>> is the issue.
>>
>
>
> Thank you for playing.
>
>
> You are right it's straighforward, but you're wrong that it's scalar
> assignment vs list assignment.
>
> The fact there's "my ($str, $ref)" on the LHS of the assignment makes
> that Perl considers this a list assignment:
>
>
>   $ perl -MO=Terse -e 'my ($str, $ref) = "text", {a => 1, b => 2, c => 3}'
>   LISTOP (0x100324de0) leave [1]
>       OP (0x100324e20) enter
>       COP (0x100324d90) nextstate
>       LISTOP (0x100301f10) list
>           OP (0x100301ee0) pushmark
>           BINOP (0x1003093c0) aassign [3]
>               UNOP (0x100309950) null [148]
>                   OP (0x100309390) pushmark
>                   SVOP (0x100309d90) const  PV (0x1008143c0) "text"
>               UNOP (0x100329530) null [148]
>                   OP (0x100329570) pushmark
>                   OP (0x1003096e0) padsv [1]
>                   OP (0x100309600) padsv [2]
>           LISTOP (0x100309460) anonhash
>               OP (0x1003094a0) pushmark
>               SVOP (0x100309400) const  PV (0x100814408) "a"
>               SVOP (0x100309430) const  IV (0x1008143f0) 1
>               SVOP (0x1003094d0) const  PV (0x100814048) "b"
>               SVOP (0x100309500) const  IV (0x1008143a8) 2
>               SVOP (0x100309530) const  PV (0x100814378) "c"
>               SVOP (0x100309560) const  IV (0x100814390) 3
>   -e syntax OK
>   $
>
>
> Note the line: BINOP (0x1003093c0) aassign [3], and compare:
>
>   $ perl -MO=Terse -e 'my ($str, $ref) = ("text", {a => 1, b => 2, c => 3})'
>   LISTOP (0x100324d90) leave [1]
>       OP (0x100324dd0) enter
>       COP (0x100324d40) nextstate
>       BINOP (0x100309570) aassign [3]
>           UNOP (0x100301f10) null [148]
>               OP (0x100301ee0) pushmark
>               SVOP (0x100309d90) const  PV (0x1008143c0) "text"
>               LISTOP (0x1003093c0) anonhash
>                   OP (0x100309400) pushmark
>                   SVOP (0x100309950) const  PV (0x100814450) "a"
>                   SVOP (0x100309390) const  IV (0x100814408) 1
>                   SVOP (0x100309430) const  PV (0x1008143f0) "b"
>                   SVOP (0x100309460) const  IV (0x100814048) 2
>                   SVOP (0x100309490) const  PV (0x1008143a8) "c"
>                   SVOP (0x1003094c0) const  IV (0x100814378) 3
>           UNOP (0x100329530) null [148]
>               OP (0x100329570) pushmark
>               OP (0x1003096e0) padsv [1]
>               OP (0x100309600) padsv [2]
>   -e syntax OK
>   $
>
>
>
> Abigail



-- 
Best Regards,
[Joseph] Christian Werner Sr
C 360.920.7183
H 757.304.0502
Txt 757.304.0502


More information about the london.pm mailing list