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

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

Replies are listed 'Best First'.
Re^3: What's the difference between a hash ?
by BrowserUk (Patriarch) on Nov 19, 2006 at 08:33 UTC

    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
        Ingy seems to have found a use for it.

        Okay. That's different. He's dumping the hash into the file using Data::Dumper which ensures everything is fully quoted etc. and avoids having to mess with the escaping he'd have to do if he tried to produce the list manually as a part of a heredoc.

        Though he could have done this (Note: '(' and ')' in the heredocs and no backslash in the Dumper call):

        print MF <<END; use ExtUtils::MakeMaker; my %options = ( END local $Data::Dumper::Terse = 1; local $Data::Dumper::Indent = 1; print MF Data::Dumper::Dumper( %options); print MF <<END; ); WriteMakefile(\%options); # Remove the Makefile dependency. Causes problems on a few systems. sub MY::makefile { '' } END

        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.