NetWallah has asked for the wisdom of the Perl Monks concerning the following question:

Brethren - I found myself requiring :
($self->{ uno },$self->{ dos }, $self->{ tres }) = qw|some three va +lues|;
and thought - surely there is a better way, but could not figure out a functioning lazier syntax, either via a map, or variants of :
# does not work @{$self->{qw|uno dos tres|}} = qw|some three values|;
Which would seem reasonable.

Is there such an animal as a hashref-slice ? as an LVALUE ?

If not, can this be done using map ? (LVALUE map ?)

What would Perl6 do ?

     ..to maintain is to slowly feel your soul, sanity and sentience ebb away as you become one with the Evil.

Replies are listed 'Best First'.
Re: hashref slice syntax ?
by wfsp (Abbot) on Feb 11, 2009 at 07:18 UTC
Re: hashref slice syntax ?
by moritz (Cardinal) on Feb 11, 2009 at 07:21 UTC
    See How do I take a slice of a hash reference?, @{$self}{qw(uno dos tres}.
    What would Perl6 do ?
    $hashref<uno dos tres> = <some three values>;

    (note that <...> is the replacement for qw(...) and for hash subscripts with literal strings).

    Update: It's already implemented in Rakudo:

    $ ./perl6 -e 'my $h = {}; $h<a b c> = 1..3; say $h.perl' {"a" => 1, "b" => 2, "c" => 3}
Re: hashref slice syntax ?
by jwkrahn (Abbot) on Feb 11, 2009 at 07:19 UTC

    Your hashref slice would be:

    @{ $self }{ qw/uno dos tres/ } = qw|some three values|;
Re: hashref slice syntax ?
by NetWallah (Canon) on Feb 11, 2009 at 22:22 UTC
    Thanks for the enlightenment.

    Still curious though - can this be done using map ?

    i.e. can the result of a map be an LVALUE ?

    (map {$self->{$_}} qw|uno dos tres|) = qw|some three values|; # Gives syntax error: # Can't modify map iterator in list assignment
    For perl6, I imagine I may use the zip operator
    for zip(qw|uno dos tres|; qw|some three values|) -> $k, $v { $self->{$k} = $v; }
    as an alternative to moritz's simpler solution.

         ..to maintain is to slowly feel your soul, sanity and sentience ebb away as you become one with the Evil.