Is there a local constant?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi All,

     my use constant bar => 10;

Obviously doesn't work.  Is there no such thing as
a local constant in Perl?  Or am I thinking in
Modula2 again?

Many thanks,

Re: Is there a local constant?

On Saturday, 1 August 2015 11:29:23 UTC+5:30, T  wrote:

Quoted text here. Click to load it

use Readonly;

Readonly my $BAR => 10;

Then, $BAR is a scalar constant & lexical to boot. So if a Readonly var. is
declared within a scope, then the constant is visible within it.

Re: Is there a local constant?

On 07/31/2015 11:43 PM, wrote:
Quoted text here. Click to load it

Thank you!

I was trying to avoid Readonly because its variables
are addressed with "$" etc.  And that might confuse
several hundred line into my code.  Constant
does to have a prefix.

But, if I can't get a local constant, I will use Readonly


Re: Is there a local constant? writes:
Quoted text here. Click to load it

A variable whose value can't be changed without manipulating internal
perl data structures and 'a constant' are two very much different things
(and the degree to which $BAR can't be changed would need to be
determined -- this could be implemented via tie). The usual meaning of
constant would be a symbolic name for constant value which is used in
the source code to make it easier to understand to a human reader with
the name being replaced by the corresponding value when
compiling. That's also the usual meaning for Perl, see 'Constant
Functions' in perldoc perlsub.

A read-only variable my serve the intended purpose, too, but "just don't
change the value" would also work fine.

Re: Is there a local constant?

Quoted text here. Click to load it

Fittingly, this was originally implemented via tie until 'other people'
started publishing similar 'Crash at runtime on rarely used codepaths!'
modules using the XS API functions for marking scalars as
read-only. This then prompted a rewrite of the Readonly module with XS
support. According to the Readonly documentation, this caused no ends of
'very hard to track down bugs' in code using the module. But this was in
seriously ancient times as it all happened prior to the release of Perl
5.8. Since then, at least these XS API routines used for this are
accessible to Perl code but there doesn't seem to be any documentation
for this. Something which performs the operation indicated above can be
implemented in a single line of code:

use Devel::Peek;

sub ro { Internals::SvREADONLY($_[0], 1) }

ro my $x = 5;


But whoever feels like it can - of course - still download the original
tie-implementation which wasn't even updated to stop checking whether
the XS module was also loaded before trying to use the corresponding
function --- never touch working code! Just the indicator variable
supposed to signal availability of a sensible implementation is now
forced to be 1 in the init code ...

I'd be willing to risk a bet that there is 'production code' in the wild
where people use the internal function to disable 'read-only ness'
temporarily while using the Readonly module to pretend a variable wasn't
been written to because anything else would again involve more thought
and possibly even changing the "Thank god it works 95% of the time!"

Re: Is there a local constant?

Quoted text here. Click to load it

Just like C, Perl doesn't support lexically scoped declarations of
anything but variables. Hence, there are neither 'local subroutines' nor
'local constants'. Both can be emulated to some degree in various ways
but while I have occasionally used local subroutines, I never missed
local constants.

Re: Is there a local constant?

On 08/01/2015 04:42 AM, Rainer Weikusat wrote:
Quoted text here. Click to load it

Thank you.  I am catching on.

Site Timeline