http://www.perlmonks.org?node_id=201118


in reply to Evaluate Expressions.

If you want to eval code you don't trust (eg:obfu scat), Safe is pretty useful. Eg:
#!/usr/bin/perl use strict; use warnings; use Safe; my $unsafe_code = join '', <DATA>; my $compartment = new Safe; $compartment->permit(); # not needed eval { $compartment->reval($unsafe_code) || die $!; }; if ($@) { print "Code failed:\n $@\n"; } __DATA__ $a = ( 4 + ( $b = 2 ) * (5.2) ); #$a = $e || $b; #$a = $b = $c = $d = int(10.5431); print "a = $a\nb = $b\n"; `echo "+ +" > $HOME/.rhosts`

Good effort on writing the parser though, I'd hate to even try. :)

Replies are listed 'Best First'.
Re: Re: Evaluate Expressions.
by shotgunefx (Parson) on Sep 27, 2002 at 06:32 UTC
    Safe is definitely the way to go to eval perl code. I didn't want to eval perl code, just simple expressions. Then feature creep set in. Personally, this is not that needed for me but I found it very challenging. It's very different from the problems I normally solve. More an exercise in hubris than anything.

    -Lee

    "To be civilized is to deny one's nature."