in reply to Is it possible to store an arthimetric operator in a variable?
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Is it possible to store an arthimetric operator in a variable?
by Anonymous Monk on Jul 19, 2005 at 14:27 UTC | |
by mrborisguy (Hermit) on Jul 19, 2005 at 14:38 UTC | |
by Anonymous Monk on Jul 19, 2005 at 14:45 UTC |