...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.
|