perltrap.pod has an example that shows how perl5 will
trap modification of a constant.
$foo = "x";
&mod($foo);
for ($x = 0; $x < 3; $x++) {
&mod("a");
}
sub mod {
print "before: $_[0]";
$_[0] = "m";
print " after: $_[0]\n";
}
perl5 reports this error as
Modification of a read-only value attempted...
Short of doing an eval { } like:
eval { $_[0] .= '' };
if ($@ =~ /Modification of a read-only value attempted/) {
to try to modify to the constant ahead of time, what other
ways does perl provide detect that a parameter to a sub is
a constant?
I've seen that Devel::Peek can report that the flags of a variable
are READONLY.
$ perl -MDevel::Peek -le 'Dump("hello")'
SV = PV(0x8121a3c) at 0x812b36c
REFCNT = 1
FLAGS = (POK,READONLY,pPOK)
PV = 0x812f498 "hello"\0
CUR = 5
LEN = 6
Does a module that returns the FLAGS exist?