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


in reply to How to do that with eval ?

Something like this is probably more what you are looking for:
my $sop = ... my $sport = ... while( <FH> ) { # get the 9th element my $port_a = (split /:/)[8]; # do the eval and capture the return value of comparison my $result = eval "\$port_a $sop \$sport"; # exit if eval failed die "eval error: $@" if $@; if( $result ) { # do something } else { # do something } }
You have to escape the variables you dont want eval to replace (string interpolate). And the "if" statement is redunant. You can just return the boolean value of the comparison.

Replies are listed 'Best First'.
Re: Re: How to do that with eval ?
by tachyon (Chancellor) on Apr 10, 2004 at 08:05 UTC
    $sop = ";print qq!You have been hacked rm -rf *!;";

    String eval + User Input == Shudder. Any. Arbitrary. Code.

    cheers

    tachyon

      Absolutely. If the above script was a cgi, you might as well reformat your disk now. If the script is meant to be a 'quick hack', evals work, they work well, and with minimal coding.

      I have used unsafe hacks like this for my personal dirty scripts because I dont have the time, inclination, or need to make them luser or h4x0r safe.

      As tachyon says, never use eval on user input unless this is intended to be a 'dirty quick hack'.
        >>"Absolutely. If the above script was a cgi, you might as well reformat your disk now. If the script is meant to be a 'quick hack', evals work, they work well, and with minimal coding."

        Hi,

        In the interests of clarity I'd be grateful if you could make it absolutely clear which of the "above" scripts you are referring to. I'm trying to learn and don't want to inadvertently misunderstand.

        Thanks

        Sid