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'.

while (<>) { $num ="2"; $eq = sub { $_[0] == $_[1] }; /(\d+).*/; if ($eq->( $num, $1 ) ) { print "match\n"; } }
You may have to predefine your operators if you are doing something complicated.
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
    thanks for that, however, could you explain the line
    sub { $_[0] == $_[1] }
    what is this doing?

    CODE tags added by Arunbear

      Sure, I could do that for you.

      Also, check out perlsub for a better explanation.

          -Bryan

        thanks for that, much appreciated