in reply to Re^2: What's the difference between a hash ?
in thread What's the difference between a hash ?
Unless there is something that you've omitted by the simplification for the OP example code, there is no utility at all in building a named hash from a constant list via an anonymous hash.
In your modified example above, there is the potential for utility, though it is not realised. That of retaining a copy of the original (anonymous) hash. This only comes about because a reference to the anonymous hash is retained which it isn't in the OP.
This allows modifications to be made to the copy whilst retaining the original. This can be used for setting defaults in object constructors and the like:
package someclass; my $defaults = { foo=>1, bar=>23 }; sub new { my $class = shift; my %self = ( %{ $defaults }, @_ ); return bless \$self, $class; }
Here keys foo and bar in $self will get the default values unless they are overidden by named arguments on the constructor, and $defaults remains unmodified.
Though there is little merit in using a named anonymous hash rather than real hash unless $defaults is passed to a subroutine somewhere.
ps. I didn't say it didn't work. Only that there was no point to it! ;)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: What's the difference between a hash ?
by syphilis (Archbishop) on Nov 19, 2006 at 09:17 UTC | |
by BrowserUk (Patriarch) on Nov 19, 2006 at 12:06 UTC | |
by syphilis (Archbishop) on Nov 19, 2006 at 13:52 UTC | |
by BrowserUk (Patriarch) on Nov 19, 2006 at 15:20 UTC |