in reply to Import constants as scalar instead of bareword sub

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.