How can I disable constant folding in Perl?

118 views Asked by At

Given a command like,

perl -MO=Deparse -E'use constant FOO => 42; print FOO()'

How can I disable constant folding such that

print 42;

Shows me

print FOO();

Or the like. Ideally, I would like this to be a compiler option that works for all of Perl. You can see this talked about in this thread on the perl mailing list, [perl #97942] [PATCH] Add -DO option to disable optimizations and disable constant folding and the peephole optimizer when used.. I tried -DO and it didn't work. If that option doesn't work, I'm open to workarounds however they may come.

2

There are 2 answers

0
Evan Carroll On BEST ANSWER

One method you can do is to prefix the constant with &,

perl -MO=Deparse -E'use constant FOO => 42; print &FOO()'

From the docs on perldoc perlsub

Constant Functions 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 &. Calls made using & are never inlined.

0
ikegami On

Constants present themselves as subs. Things you can do with a sub:

  • Call it as a function (FOO, FOO())
  • Call it as a function while overriding prototypes (&FOO())
  • Call it as a function while inheriting @_ (&FOO)
  • Call it as a method (__PACKAGE__->FOO)
  • Create a reference to it (\&FOO, *FOO{CODE}. Also *FOO where a code ref is expected.)

Only the first gets folded. As such, you could use

$ perl -MO=Concise,-exec -E'use constant FOO=>42; say &FOO()' 2>&1 | grep FOO
5  <#> gv[*FOO] s
$ perl -MO=Concise,-exec -E'use constant FOO=>42; say &FOO' 2>&1 | grep FOO
5  <#> gv[*FOO] s
$ perl -MO=Concise,-exec -E'use constant FOO=>42; say __PACKAGE__->FOO' 2>&1 | grep FOO
6  <.> method_named[PV "FOO"] l
$ perl -MO=Concise,-exec -E'use constant FOO=>42; say+ (\&FOO)->()' 2>&1 | grep FOO
5  <#> gv[*FOO] s
$ perl -MO=Concise,-exec -E'use constant FOO=>42; say *FOO{CODE}->()' 2>&1 | grep FOO
5  <#> gv[*FOO] s
$ perl -MO=Concise,-exec -E'use constant FOO=>42; say *FOO->()' 2>&1 | grep FOO
5  <#> gv[*FOO] s