in reply to What's the difference between a hash ?

The second varient is stupid.

  1. It builds one (anonymous) hash;
  2. flattens it to a list by dereferencing it in a list context;
  3. assigns the list to build a second named hash;
  4. and discards the anonymous hash.

The first only does step 2.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
  • Comment on Re: What's the difference between a hash ?

Replies are listed 'Best First'.
Re^2: What's the difference between a hash ?
by syphilis (Archbishop) on Nov 19, 2006 at 07:55 UTC
    Bah ... part of my problem was that I had tried replacing the anonymous hashref with a named hashref, and found it didn't work. Apparently, I had made some simple coding error, because the following does, in fact, work as expected:
    use warnings; use strict; my $h = {FOO => 1, BAR => 2}; my %hash = %{$h}; print $hash{FOO}, "\n"; print $hash{BAR}, "\n";
    Incidentally, that "stupid" way of assigning hashes was taken from the Makefile.PL that Inline::C autogenerates. (I guess that has something to do with the way that Inline::C determines its parameters.)

    Thanks guys.

    Cheers,
    Rob

      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! ;)


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Q: Why is a duck ?
        A: To keep its wings apart

        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

        However, Ingy seems to have found a use for it. See the write_Makefile_PL subroutine in Inline/C.pm. Of course, I have no idea whether his actions in this regard arose from laziness or from necessity ... or even from ignorance. (I know only about ducks :-)

        ps. I didn't say it didn't work. Only that there was no point to it! ;)

        Yep ... understood :-)

        Cheers,
        Rob