Depending on what you want to do, you could just make a sub that contains your operator. That way, you can avoid an 'eval'.
You may have to predefine your operators if you are doing something complicated.while (<>) { $num ="2"; $eq = sub { $_[0] == $_[1] }; /(\d+).*/; if ($eq->( $num, $1 ) ) { print "match\n"; } }
my $eq = sub { $_[0] == $_[1] }; my $lt = sub { $_[0] < $_[1] }; # ... while (<>) { $num ="2"; if ( ...some logic... ) { $op = $eq; } else { $op = $lt; } $eq = sub { $_[0] == $_[1] }; /(\d+).*/; if ($op->( $num, $1 ) ) { print "match\n"; } }
-Bryan
In reply to Re: Is it possible to store an arthimetric operator in a variable?
by mrborisguy
in thread Is it possible to store an arthimetric operator in a variable?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |