Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

using features in a Safe compartment

by djerius (Beadle)
on Mar 21, 2011 at 21:07 UTC ( #894622=perlquestion: print w/ replies, xml ) Need Help??
djerius has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to use features in a Safe compartment but they're not being recognized. Here's an example using Perl 5.12.2.
use feature qw[ say ]; use Safe; # works here say "Hello!"; my $cft = Safe->new; # allow all opcodes $cft->deny_only(); # doesn't work here (expected result) $cft->reval( 'say "Hello Again!"' ); # explicit load of feature, but doesn't work (unexpected result) $cft->reval( 'use feature qw[ say ]; say "Hello Again! 2"' );
And here's what it spits out:
Hello! String found where operator expected at (eval 5) line 1, near "say "He +llo Again!"" (Do you need to predeclare say?) String found where operator expected at (eval 7) line 1, near "say "He +llo Again! 2"" (Do you need to predeclare say?)
Does anyone have any experience with this?

Thanks,
Diab

Update: I should be clearer about the question:

How does one enable features in a Safe compartment? My example code seems to indicate that a simple use feature doesn't do anything within the compartment. Am I mising something?

Comment on using features in a Safe compartment
Select or Download Code
Re: using features in a Safe compartment
by muzakfetch (Scribe) on Mar 21, 2011 at 23:21 UTC
    Hi! First of all, "reval" only evaluates the code and then returns the value. So in this case, the following will work:
    use warnings; use strict; use Safe; use feature qw{say}; say "Hello!"; my $cft = Safe->new; $cft->deny_only(); my $ret = $dft->reval(qq{return "Hello Again!"}, undef); say $ret;
    So using the "say" doesn't work because it evaluates to nothing, and returns nothing. In fact, I was kind of surprised that it didn't at least return "1" since say should return "true". But who knows.

    Update: something else just occurred to me. Safe is not going to evaluate the "use" or in this case what I tried below. It evaluates the entire script, doesn't recognize the "say" keyword and throws the error. It never *executes* the use or the require/import.
    my $unsafe = qq| require feature; feature->import( qw{say switch} ); say "Hope this works"; return "I did what you said"; |; # explicit load of feature, but doesn't work (unexpected result) $ret = $cft->reval( $unsafe, undef );


    HTH,
    Tyler.
      I'm afraid I don't really understand your first point:
      "reval" only evaluates the code and then returns the value.

      So using the "say" doesn't work because it evaluates to nothing, and returns nothing.

      As my example showed, the code is never evaluated because it fails to compile due to say not being recognized. My attempt at enabling the feature in the Safe compartment via use feature doesn't work.

      It works in a standard eval:

      $ perl -e 'eval q[use feature qw[ say ]; say "hello"]' hello
      The code in your update shows the same problem that I'm struggling with, namely that use feature is not enabling the feature in the Safe compartment.

      Perhaps I should rephrase the question: how does one get use feature to work in a Safe compartment?

Reaped: discount louboutin
by NodeReaper (Curate) on Mar 22, 2011 at 03:38 UTC
Re: using features in a Safe compartment
by sundialsvc4 (Abbot) on Mar 23, 2011 at 16:03 UTC

    P.S.   Where’s a good source of documents that describe things like Safe?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://894622]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2014-11-28 14:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (197 votes), past polls