So are there any more efficient way to do that ?

A better question might be is using eval the most inefficient , least secure and most incomprehensible method? To which the answer is yes.

Now seriously string eval and user input or even file input is a recipe for disaster. As you currently don't seem to understand why or how all I can do is suggest you avoid eval "this" until you understand how dangerous it really is.

If you were to outline what you are actually trying to do (as opposed to how do I take this loaded gun, put it to my head and wait for someone to pull the trigger ;-) I am sure you will find lot's of efficient, secure and fast suggestions.

From the information you give the if/elsif/elsif/else structure you show IS the most efficient structure in terms of speed and security. There are many other ways to do it, typically a dispatch hash, but these are all slower. Just about anything will be more secure than a string eval where you let user input in.

my %dispatch = ( '>' => sub { $_[0] > $_[1] }, '<' => sub { $_[0] < $_[1] }, '=='=> sub { $_[0] == $_[1] }, ); # now in loop if ( $dispatch{$sop} ) { # OK so $sop exists, get the right function from # the dispatch hash and call it with the args we want if ( &{$dispatch{$sop}}( $tmpdata[8], $sport ) ) { # yabada } else { # ! yabada } } else { die "Illegal op $sop. Are you trying to hack me?\n"; } # end loop

cheers

tachyon


In reply to Re: How to do that with eval ? by tachyon
in thread How to do that with eval ? by iwanthome

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.