Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Configurable comparisons

by Corion (Patriarch)
on Feb 26, 2002 at 22:20 UTC ( [id://147750]=note: print w/replies, xml ) Need Help??


in reply to Configurable comparisons

Tackling this problem via eval is possible with Perl, but it surely is not wise to attack this problem with this tool.

I guess you want something like the following, a hand-coded and much gentler version of eval, that only evaluates the intended stuff (untested):

use strict; my %variable; my %operation; $operation{eq} = sub { my ($op1, $op2) = @_; return $op1 eq $op2; }; $operation{ne} = sub { my ($op1, $op2) = @_; return $op1 ne $op2; }; sub evaluate { my ($var,$op,$value) = @_; if (defined $operation{$op}) { my $code = $operation{$op}; return $code->( $variable{$var}, $value ); } else { die "Unknown operator : $op\n"; }; }; $variable{foo} = 'FOO'; $variable{bar} = 'BAR'; my ($var,$op,$val); foreach $var (keys %variable) { foreach $op (keys %operation) { foreach $val ('FOO','BAR','neither') { print "$var $op $val :", evaluate( $var, $op, $val ), "\n"; }; }; };

More operators are added easily - you could now turn again to Perl to write these operators for you, but it is better to approach the problem slowly instead of drinking the whole sea at once :-)

Of course the above assumes that all your status variables are collected together in a separate hash, as it's a generally bad idea to use symbolic references (see Dominus' homepage for a discussion). If you really really really think you must get at the variable names from within your program, ask that in a separate question.

perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-20 02:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found