Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: How to do that with eval ?

by perlmonkey (Hermit)
on Apr 10, 2004 at 07:43 UTC ( #344099=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: How to do that with eval ?
Download Code
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://344099]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2014-09-20 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (160 votes), past polls