Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is there a local constant?

41 views
Skip to first unread message

T

unread,
Aug 1, 2015, 1:59:23 AM8/1/15
to
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,
-T

shar...@hotmail.com

unread,
Aug 1, 2015, 2:43:16 AM8/1/15
to
On Saturday, 1 August 2015 11:29:23 UTC+5:30, T wrote:

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

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.

T

unread,
Aug 1, 2015, 2:58:34 AM8/1/15
to
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

-T

Rainer Weikusat

unread,
Aug 1, 2015, 7:26:21 AM8/1/15
to
shar...@hotmail.com writes:
> On Saturday, 1 August 2015 11:29:23 UTC+5:30, T wrote:
>
>>
>> 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?
>>
>
> use Readonly;
>
> Readonly my $BAR => 10;
>
> Then, $BAR is a scalar constant

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.

Rainer Weikusat

unread,
Aug 1, 2015, 7:42:10 AM8/1/15
to
T <T...@invalid.invalid> writes:
> 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?

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.

Rainer Weikusat

unread,
Aug 1, 2015, 10:31:50 AM8/1/15
to
Rainer Weikusat <rwei...@mobileactivedefense.com> writes:
> shar...@hotmail.com writes:
>> On Saturday, 1 August 2015 11:29:23 UTC+5:30, T wrote:
>>
>>>
>>> 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?
>>>
>>
>> use Readonly;
>>
>> Readonly my $BAR => 10;
>>
>> Then, $BAR is a scalar constant
>
> 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).

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;

Dump($x);
-----------

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!"
code.

T

unread,
Aug 1, 2015, 4:57:47 PM8/1/15
to
Thank you. I am catching on.
0 new messages