|Welcome to the Monastery|
Assigning to Hash Values (updated)by haukex (Abbot)
|on Apr 28, 2017 at 16:49 UTC||Need Help??|
Note that the values are not copied, which means modifying them will modify the contents of the hash.
This means that $_++ for values %hash; will modify the original hash values, and the following (based on this AM post) will assign to the values of the hash:
The above work because both the foreach loop variable and the elements of @_ are aliases to the original scalars. The slice in the second example is necessary, since @_ = ... would clobber the array and remove the aliasing, instead of assigning to its elements.
However, values itself is not lvalue, meaning that values(%hash) = qw/a b c/; won't work. Neither will (values(%hash)) = 'a';.
Here's my "invention", which I present as a curiosity rather than a suggested solution. It makes use of Lvalue subroutines and Prototypes. The parens on the left are necessary for the sub to be called in list context.
Updates 1&2: However, note that repeated calls to (lvalues %hash) = values %hash; will
Minor updates for clarification.