develooper Front page | perl.perl5.porters | Postings from January 2008

Re: [perl #48419] Safe and use encoding 'utf8'

From:
Rafael Garcia-Suarez
Date:
January 28, 2008 07:12
Subject:
Re: [perl #48419] Safe and use encoding 'utf8'
Message ID:
b77c1dce0801280712r358b8a8et21563ffb0c03b332@mail.gmail.com
On 10/12/2007, via RT Ville Luolajan-Mikkola <perlbug-followup@perl.org> wrote:
> I have already submitted this bug on CPAN also, but I suppose this is
> the right place?
>
> I have found that Safe does not work (at all) 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 the default perl
> included on Debian 4.0, on one 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.pm; 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);
>
> Sample script for demonstration:
> -----
> #!/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";');
> -----
>
> A fix to this problem would be much appreciated, as the workaround is
> flaky at best...

The real workaround would be to permit the called method to be
imported in the Safe compartment. That is, once $safe is created, and
before the call to reval(), do:

        $safe->share('Encode::Encoding::renewed');

I'm not sure that bug can be safely fixed in Safe.



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About