Darkwing has asked for the wisdom of the Perl Monks concerning the following question:
the docu of constant says that constants such as
are inlined. In constant.pm, the corresponding lines of code are:use constant DEBUG => 0;
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.... if (@_ == 1) { my $scalar = $_[0]; *{"${pkg}::$name"} = sub () { $scalar }; } ...
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 | |
by ikegami (Patriarch) on Jun 30, 2016 at 18:27 UTC | |
by Darkwing (Scribe) on Jun 30, 2016 at 07:47 UTC | |
|
Re: Are "use constant" constants really inlined?
by haukex (Archbishop) on Jun 30, 2016 at 07:30 UTC | |
by Darkwing (Scribe) on Jun 30, 2016 at 07:51 UTC | |
|
Re: Are "use constant" constants really inlined?
by shawnhcorey (Friar) on Jun 30, 2016 at 12:13 UTC |