Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Parsing conditional expressions

by Lorand (Beadle)
on Mar 23, 2004 at 13:26 UTC ( [id://338983]=perlquestion: print w/replies, xml ) Need Help??

Lorand has asked for the wisdom of the Perl Monks concerning the following question:

I would like to interpolate a string like "$attribute == value" into an if statement, but actually more complex, using and, or and not operators. Is that possible in some simple way, or do I have to use perl-byacc for that (I don't have yacc knowledge, so that would be kinda complicated)?

Thanks, Lori

PS.: What I actually want to implement is the Perl equivalent of the ethereal-filter(4) mechanism.

Replies are listed 'Best First'.
Re: Parsing conditional expressions
by halley (Prior) on Mar 23, 2004 at 13:34 UTC
    my $attribute = 3; my $condition = '$attribute == 3 or $attribute == 4'; print +((eval $condition)? "TRUE" : "FALSE", ($@)? " (warning: $@)" : '', $/);
    Do be aware that there are security considerations to using eval to arbitrarily evaluate a user-supplied chunk of Perl syntax.

    --
    [ e d @ h a l l e y . c c ]

Re: Parsing conditional expressions
by delirium (Chaplain) on Mar 23, 2004 at 13:33 UTC
    You can do that with an eval command, or with something tricky like...

    if ( $variable eq ($attribute.' == value') ) { &do_stuff }
Re: Parsing conditional expressions
by Wonko the sane (Deacon) on Mar 23, 2004 at 13:38 UTC
    Hello

    I would be very interested in knowing why one would need a conditional conditional statement.
    This seems like a very complex widget to need.

    Wonko

      I don't know what the OP is requesting, but two major applications of this sort of technique would be database queries and message filtering, both according to user-supplied (or larger application configurable) criteria.

      [For caveats re: 'user' and 'database', see my reply to dragonchild below.]

      For example, SpamAssassin is basically a big user-criteria engine: it consults a list of user-supplied criteria and tallies a score total of matching ones, for each e-mail it inspects.

      --
      [ e d @ h a l l e y . c c ]

        Database queries?? I don't think so. If you're letting your user tell you what query you're going to run, you're asking for bankruptcy. I would never allow my users to construct queries through a Perl app. If they need to be constructing queries, give them SQL*Plus access. It has had 9 major revisions to get security and other issues correct. Plus, they'll be executing based on the roles they were granted, not the roles the application was granted.

        Personally, I tell my users "Please choose from the following options" which I have supplied to them. I then combine those options according to a very flexible (but rigid) set of rules to construct the query on the fly.

        As for message filtering ... you don't need to use eval, there, either. You use either dispatch or switch statements. Unless, of course, you like playing on the edge. I do something exactly like this in PDF::Template, in my <if> node. You get to indicate the LHS, RHS, and the operator. If I don't recognize the operator, then it's a numeric equality check. If you don't like that, tough. And, it's the same with the mathematical operators. You get the four basic ones. Anything else and I complain. Loudly.

        You can never NEVER trust your users, even if it's just to keep them from typing 'rm -rf /' accidentally.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

      I need this to match some packets stored in XML (actually PDML) format. I use XML::Parser to parse the document, and when I detect an end tag </packet> I want to see if that packet meets some conditions or not.

      I need this to determine some phases of the packet flow, so I will have about 6 packet types that have to be matched, and that has to be extensible and easily modifyable, so I don't want to hardcode it. Security is not a concern, it's for a personal project.

      Thanks for the answers so far.

        Create a class called Packet and subclasses called Packet::TypeA, Packet::TypeB, etc. Then, using a factory, create the appropriate subclass. Have each subclass know how to validate itself. No need for eval.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://338983]
Approved by Limbic~Region
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2024-04-18 17:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found