It's (another) area where I find myself questioning the logic.

Why do Perl programmer's expect to be able to interpolate constants--into strings?

In most every other languages, you would need to interpret the value of a symbolic constant into a string using whatever their equivalent of (s)printf is. Admittedly, they also require you to do the same with variables, which Perl doesn't. But that's a convenience that Perl provides--yah Perl--not something that should be expected.

It is similarly impossible to directly interpolate a hash into a string without resorting to special measures:

perl> %hash = 1 .. 10;; perl> print "%hash";; %hash perl> print "@{[ %hash ]}";; 1 2 3 4 7 8 9 10 5 6

and even then the results are pretty unsatisfactory.

And you can achieve a similar, complicated interpolation of constant subs into strings.

perl> use constant X => 12345;; perl> print X;; 12345 perl> print "${ \ X }";; 12345 perl> print "@{[ X ]}";; 12345 perl> print 'stuff ' . X .' other stuff';; stuff 12345 other stuff perl> printf "stuff %s other stuff\n", X;; stuff 12345 other stuff

Given the other benefits of constant subs:

  • Inlining (when instantiated at the right time :).
  • Readonly, without overhead.
  • Clearly distingished from variables without reliance upon convention.

    Discarding them on the basis that they do not interpolate seems like throwing the baby out with the bathwater, when there seems to me to be a very simple and effective solution to the problem:

    perl> *X = sub{ 12345 };; perl> *X = \12345;; perl> print X;; 12345 perl> print "${X}";; 12345 perl> print "$X";; 12345

    If the constant module were modified to instantiate scalar constants as both constant subs and constant scalars with the same name, it renders constants that retain all the advantages of constant subs whilst giving the ability for them to be interpolated.

    The third usage above "$X" would be deprecate in favour of the second "$(X}" that retains the visual continuity with the bareword version, and incidently allows abuttment of the constant with other text or constants without misinterpretation.


    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.

    In reply to Re: Import constants as scalar instead of bareword sub by BrowserUk
    in thread Import constants as scalar instead of bareword sub by powerman

    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.