...the expression definitely can be evaluated at compile time.

Depends on how you define "compile time" :)

The constant pragma implies a BEGIN block, which has its own compile and execution time — with the execution happening right after the BEGIN block has been compiled, but before the rest of the code is being parsed and compiled.

In other words,

use constant FOOBAR => "a" x 3;

is roughly equivalent to

BEGIN { my $x = "a" x 3; *::FOOBAR = sub () { $x } }

And when you investigate it with B::Concise, you'll see that the opcodes in question have just moved to a different place and execution time, but they're still there:

$ perl -MO=Concise,BEGIN,-main -e'use constant FOOBAR => "a" x 3; prin +t FOOBAR' | grep -C 3 '"a' 14 <$> const[PV "constant"] sM ->15 15 <$> const[PV "FOOBAR"] sM/BARE ->16 18 <2> repeat[t1] sKM/2 ->19 # <--- 16 <$> const[PV "a"] s ->17 # <--- 17 <$> const[IV 3] s ->18 # <--- 19 <$> method_named[PV "import"] ->1a BEGIN 5: -e syntax OK -- 4i <;> nextstate(main 102 -e:1) v:{ ->4j 4l <@> print vK ->4m 4j <0> pushmark s ->4k 4k <$> const[PV "aaa"] s ->4l

The mechanism to define constants via Constant Functions is rather different from constant folding in the sense of 42+99 being replaced with 141 at compile time.

Consider the following

$ perl -MO=Concise -e'use constant FOOBAR => scalar <STDIN>; print FOO +BAR' abc 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 102 -e:1) v:{ ->3 5 <@> print vK ->6 3 <0> pushmark s ->4 4 <$> const[PV "abc\n"] s ->5 # <---

Clearly, the value "abc" isn't part of the code, but entered via the keyboard at runtime (of the respective BEGIN block), yet it can be inlined as a constant in code sections compiled subsequently.


In reply to Re^3: Why is const x const not a const? by Eliya
in thread Why is const x const not a const? by PerlGrey

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.