Subject: | Problems with Safe and utf8 |
Safe does not work if I "use encoding 'utf8';". All I get is an error message:
Can't locate object method "renewed" via package "Encode::utf8" (perhaps you forgot to load
"Encode::utf8"?) at (eval 2) line 1.
As far as I know, there is no Encode::utf8 (it's supposed to be internal), and commenting out
that "use encoding 'utf8';" makes the same source not die anymore.
I use perl 5.8.8 with its included Safe.pm 2.12, but have also tried using the 2.11 from CPAN
instead. I have tested it on default perl included on Debian 4.0, compiled from vanilla source,
and the one bundled with Mac OS X 10.5.1. The result is always the same.
There is a workaround to the problem, however, I don't really understand how or why this
works. :) In reval() inside Safe; just after the $evalsub has been retrieved from
lexless_anon_sub(), if I append anything (even an empty string) to the $expr, the problem
disappears!
In code:
my $evalsub = lexless_anon_sub($root,$strict, $expr);
$expr."";
return Opcode::_safe_call_sv($root, $obj->{Mask}, $evalsub);
Attached is a trivial script which can be used to reproduce the error.
Subject: | safe.pl |
#!/usr/bin/perl
use encoding 'utf8';
use Safe;
sub foo {
my $expr = shift();
if (my $safe = new Safe()) {
$ret = $safe->reval ("$expr");
}
if ($@) {
print "Error while evaluating \"$expr\": $@\n";
}
}
&foo('print "eval OK\n";');