in reply to Hash assignment: Undocumented feature?
... a method of changing several values at once.
I don't see this as a valid way of looking at this behavior. A hash key is always unique, and a hash key always has exactly one scalar value, although this scalar may be a reference to... well, anything, opening up vast possibilities for data structures, code dispatch tables, etc., etc., etc. So the only way to "[change] several values at once" is to replace a (scalar) reference to a bunch of stuff with another (scalar) reference to a different bunch of stuff.
Maybe one useful approximation to "changing several values at once", but with "changing" better understood as "dealing with", is the Perl idiom for implementing named, defaulted, non-positional function arguments. It goes something like:
>perl -wMstrict -le "sub D { my %defaults = qw(foo 999 bar 888 baz 777); ;; my ($hr_args) = @_; my %arguments = (%defaults, %{ $hr_args || {} }); ;; print qq{foo $arguments{foo} bar $arguments{bar} baz $arguments{b +az}}; } ;; D(); ;; D({bar => 2}); ;; D({baz => 1, foo => 3}); " foo 999 bar 888 baz 777 foo 999 bar 2 baz 777 foo 3 bar 888 baz 1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Hash assignment: UndERDocumented feature?
by fsavigny (Novice) on Aug 11, 2013 at 17:18 UTC | |
by AnomalousMonk (Archbishop) on Aug 11, 2013 at 17:53 UTC |