Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Using constants as hash keys

by karlgoethebier (Abbot)
on Jan 05, 2018 at 13:41 UTC ( [id://1206752]=note: print w/replies, xml ) Need Help??


in reply to Using constants as hash keys

Very nice. Thanks for sharing this choroba.

The inevitable little addendum:

#!/usr/bin/env perl use strict; use warnings; use Const::Fast; use Data::Dump; use feature qw(say); const our $A => 12; const our $B => 13; print $]; print qx(corelist Const::Fast); my %hash = ( $A => 'twelve', $B => 'thirteen', ); dd \%hash; say $hash{$A}; say $hash{$B}; for ( sort ( keys(%hash) ) ) { say qq($_ => $hash{$_}); } __END__ karls-mac-mini:playground karl$ ./constant.pl 5.024001 Data for 2017-01-14 Const::Fast was not in CORE (or so I think) { 12 => "twelve", 13 => "thirteen" } twelve thirteen 12 => twelve 13 => thirteen

Best regards, Karl

«The Crux of the Biscuit is the Apostrophe»

perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Replies are listed 'Best First'.
Re^2: Using constants as hash keys
by Athanasius (Archbishop) on Jan 05, 2018 at 15:10 UTC

    Hello karlgoethebier,

    I’ve been a fan of Const::Fast since I discovered it recently in the Perl Advent Calendar 2017. But constant does have one important advantage over Const::Fast: it is applied at compile time, whereas Const::Fast takes effect only at run time. So, in this respect, Const::Fast suffers from the same limitation, identified by choroba, as the &CONSTANT syntax for the constant pragma — the constant isn’t inlined:

    0:59 >perl -Mstrict -MData::Dump -MConst::Fast -we "use constant A => + 12; const our $B => 13; my %h = ( (A) => 'twelve', $B => 'thirteen') +; dd \%h;" { 12 => "twelve", 13 => "thirteen" } 0:59 >perl -Mstrict -MO=Deparse -MData::Dump -MConst::Fast -we "use c +onstant A => 12; const our $B => 13; my %h = ( (A) => 'twelve', $B => + 'thirteen'); dd \%h;" BEGIN { $^W = 1; } use strict; use Data::Dump; use Const::Fast; use constant ('A', 12); &const(\our $B, 13); my(%h) = (12, 'twelve', $B, 'thirteen'); dd(\%h); -e syntax OK 0:59 >

    (AFAICT, this also makes Const::Fast unsuitable in situations where one wants to write code like this:

    use constant DEBUG => 0; ... perform_some_debugging_operation() if DEBUG; ...

    and have the compiler remove the expensive operation when it sees that DEBUG is false.)

    Update: Changed perform_expensive_debugging_operation() to perform_some_debugging_operation(), because, as Eily pointed out in a private message, the cost of the operation is irrelevant if DEBUG (or $DEBUG) is false.

    Cheers,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      "...the same limitation..."

      Yes. Thanks. Unfortunately this is true. And i begin to see it clearly now. Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1206752]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-20 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found