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


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.

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
    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.
        But that doesn't work with the AUTOWRAP script that I have ... and I'm not able to come to an understanding of why that is so ... given my (now) current state of insobriety. Perhaps all will become clear to me tomorrow. In the meantime, here's 'autowrap.pl':
        use warnings; use Inline C => Config => BUILD_NOISY => 1, TYPEMAPS => 'simple_typemap.txt', AUTO_INCLUDE => '#include "D:/C/simple.h"', CLEAN_AFTER_BUILD => 0, ENABLE => AUTOWRAP, LIBS => '-LD:/C -lsimple'; use Inline C => q{simple_double simple(simple_double); }; print simple(1.032);
        and here's what happens when I run 'perl autowrap.pl' (using your amendments to C.pm):
        . . Starting Build Compile Stage Starting "perl Makefile.PL" Stage String found where operator expected at Makefile.PL line 8, near "'NAM +E'" (Missing semicolon on previous line?) String found where operator expected at Makefile.PL line 9, near "'aut +owrap_pl_e 826'" (Missing semicolon on previous line?) String found where operator expected at Makefile.PL line 10, near "'IN +C'" (Missing semicolon on previous line?) String found where operator expected at Makefile.PL line 11, near "'-I +D:/pscrpt/ inline/special'" (Missing semicolon on previous line?) String found where operator expected at Makefile.PL line 12, near "'LI +BS'" (Missing semicolon on previous line?) String found where operator expected at Makefile.PL line 16, near "'VE +RSION'" (Missing semicolon on previous line?) String found where operator expected at Makefile.PL line 17, near "'0. +00'" (Missing semicolon on previous line?) syntax error at Makefile.PL line 4, near "[" Execution of Makefile.PL aborted due to compilation errors. A problem was encountered while attempting to compile and install your + Inline C code. The command that failed was: D:\perl58_M\5.8.8\bin\perl.exe Makefile.PL The build directory was: D:\pscrpt\inline\special\_Inline\build\autowrap_pl_e826 To debug the problem, cd to the build directory, and inspect the outpu +t files. at autowrap.pl line 11 BEGIN failed--compilation aborted at autowrap.pl line 11.
        'simple.h' looks like:
        #define simple_double double simple_double simple(simple_double d);
        and 'simple_typemap.txt' looks like:
        simple_double T_S_DOUBLE INPUT T_S_DOUBLE $var = (simple_double)SvNV($arg) OUTPUT T_S_DOUBLE sv_setnv($arg, (simple_double)$var);
        Perhaps it's just a simple syntax error in the code you posted ... but I couldn't spot it. It's creating a Makefile.PL that looks like this:
        use ExtUtils::MakeMaker; my %options = ( 'TYPEMAPS' [ 'D:\\perl58_M\\5.8.8\\lib\\ExtUtils\\typemap', 'D:\\pscrpt\\inline\\special\\simple_typemap.txt' ] 'NAME' 'autowrap_pl_e826' 'INC' '-ID:/pscrpt/inline/special' 'LIBS' [ '-LD:/C -lsimple' ] 'VERSION' '0.00' ); WriteMakefile(%options); # Remove the Makefile dependency. Causes problems on a few systems. sub MY::makefile { '' }
        Gotta go to bed :-)

        Cheers,
        Rob