Darkwing has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

the docu of constant says that constants such as

use constant DEBUG => 0;
are inlined. In constant.pm, the corresponding lines of code are:
... if (@_ == 1) { my $scalar = $_[0]; *{"${pkg}::$name"} = sub () { $scalar }; } ...
But here, a local variable is defined and then used within a sub defined in this block. This should result in a closure rather than in an inlinable subroutine: whenever this block is executed, a new $scalar variable is created and it won't be removed since the subroutine accesses it. So, i understand closures to work. But then the body of the sub isn't a constant value, it is a variable, and so it should not result in an inlinable constant.

Well, i'm wrong? Are those constants really inlined? Bit if so, why does it work? Why aren't they closures?

Replies are listed 'Best First'.
Re: Are "use constant" constants really inlined?
by Athanasius (Archbishop) on Jun 30, 2016 at 07:25 UTC

      B::Concise is even more definitive since it shows very accurate information about the opcodes (whereas B::Deparse synthesizes a Perl code approximation).

      $ perl -MO=Concise,-exec -e'use constant { C => 42 }; print C;' 1 <0> enter 2 <;> nextstate(main 187 -e:1) v:{ 3 <0> pushmark s 4 <$> const[IV 42] s*/FOLD 5 <@> print vK 6 <@> leave[1 ref] vKP/REFC -e syntax OK

      Compare to a sub call:

      $ perl -MO=Concise,-exec -e'sub C { 42 } print C;' 1 <0> enter 2 <;> nextstate(main 3 -e:1) v:{ 3 <0> pushmark s 4 <0> pushmark s 5 <#> gv[IV \] s 6 <1> entersub lKS 7 <@> print vK 8 <@> leave[1 ref] vKP/REFC -e syntax OK
      Thanks, and B::Deparse is an interesting module; didn't know that.
Re: Are "use constant" constants really inlined?
by haukex (Archbishop) on Jun 30, 2016 at 07:30 UTC

    Hi Darkwing,

    From "Constant Functions" in perlsub (emphasis mine):

    Functions with a prototype of () are potential candidates for inlining. If the result after optimization and constant folding is either a constant or a lexically-scoped scalar which has no other references, then it will be used in place of function calls made without &.

    The documentation then goes on to show an example like the one you're asking about (note that use constant ... in the main program essentially causes the module to be executed inside a BEGIN block in the main program, see use).

    Athanasius already showed how to use B::Deparse to check if they are inlined.

    Hope this helps,
    -- Hauke D

      Thanks!

      "a lexically-scoped scalar which has no other references,"

      This makes it clear. In fact, i looked into the docu, but read it too briefly so i missed this sentence!
Re: Are "use constant" constants really inlined?
by shawnhcorey (Friar) on Jun 30, 2016 at 12:13 UTC