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,


In reply to Re^2: Using constants as hash keys by Athanasius
in thread Using constants as hash keys by choroba

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.