calin has asked for the wisdom of the Perl Monks concerning the following question:
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:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: LVALUEness in flattened hashes
by jeffa (Bishop) on Jan 09, 2004 at 21:25 UTC | |
|
Re: LVALUEness in flattened hashes (efficient)
by tye (Sage) on Jan 09, 2004 at 21:19 UTC | |
|
Re: LVALUEness in flattened hashes
by chromatic (Archbishop) on Jan 09, 2004 at 20:24 UTC | |
|
Re: LVALUEness in flattened hashes
by antirice (Priest) on Jan 09, 2004 at 19:35 UTC | |
by calin (Deacon) on Jan 09, 2004 at 20:18 UTC | |
by antirice (Priest) on Jan 09, 2004 at 20:50 UTC | |
|
Re: LVALUEness in flattened hashes
by duff (Parson) on Jan 09, 2004 at 22:18 UTC |