I call a flattened hash the list that is produced when a %-literal is used in
a list context ; it can be thought as a "print representation" (LISP-speak)
of a hash object. It is a list consisting of hash keys in odd position (0, 2,
...) with corresponding hash values in subsequent even position (1, 3,
...) (0 is first position in Perl lists, therefore odd :) ). It's used when copying
hashes to/from other hashes or arrays or passed to arbitrary list contexts; also
a hash can be initialized from an arbitrarily generated list, which is
interpreted with flattened hash convention, as in the most common and idiomatic hash
initialization construct:
%h = (one => 1, two => 2);. Most monks
are already familiar with these issues, so I won't press on.
While meditating on these flattened hashes, a question occurred to me: do they
preserve lvalueness of value (even) positions when used in aliasing contexts
(subroutine call and foreach)? A quick experiment revealed that
they do:
$ perl
my %h = 'a'..'f';
sub modify {
for (my $i = 1 ; $i < @_ ; $i+=2) {
$_[$i] .= '_modified';
}
}
modify(%h);
print "$_ => $h{$_}\n" for sort keys %h;
^D
a => b_modified
c => d_modified
e => f_modified
$ perl
my %h = 'a'..'f';
# assignment to key (odd) positions has no side effects
$_ .= '_modified' for %h;
print "$_ => $h{$_}\n" for sort keys %h;
^D
[the same output]
Now I'm not advocating this as a technique for hash modification in called
subroutines etc.! The usual method of call-by-reference is much cleaner and
probably(?) more efficient. I only have a few questions for monks who are more intimate
with Perl internals/development history:
-
Is this intentional / documented behaviour or just incidental?
-
If intentional, what's the reason? Historical? Incidental behaviour become
standard with time? Implemented just to stay consistent? ...
-
Do you think it conforms to the Principle of Least Surprise (I can't make up my
mind on it)
LVALUEness of values
My experiments revealed that
values is also lvaluable (and this is
documented). With aliasing, it's useful for key-agnostic bulk value
modifications.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.