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.