问题
Looking into the Const::Fast source I noticed that it used the built-in function Internals::SvREADONLY
internally. Is it safe to use that function directly in my Perl script? It seems to be present in core from Perl 5.8.
my $PI = 4 * atan2 1, 1;
Internals::SvREADONLY($PI => 1);
$PI = 2.718; # Modification of a read-only value attempted at ..
回答1:
C:\>perldoc Internals
No documentation found for "Internals".
No.
More specifically, the package is named "Internals" for a reason. It is not intended for use outside the core. It could change without notice.
回答2:
This is not quite answering your question, but I think it is worth mentioning so others don't experience the same pain as I have: don't use any readonly value if you're running on a version of Perl earlier than 5.10.1. Consider this little example:
{
package Foo;
sub foo { print "I'm in foo!\n"; }
}
use strict;
use warnings;
use Readonly;
Readonly my @classes => qw(Foo);
foreach my $class (@classes)
{
# this dies with "Can't call method "foo" without a package or object reference"
$class->foo;
}
Since my XS-fu is not very high, I can't explain what is going on here very coherently (but Devel::Peek shows some interesting things in the $class
variable).
来源:https://stackoverflow.com/questions/4098816/should-i-use-internalssvreadonly-for-creating-readonly-variables-in-perl