http://www.perlmonks.org?node_id=480127


in reply to Returning data from an eval

First, in your rule you are using "=" to do a string comparison. Don't do that. use eq.

I have a similar program which reads rules from a xml-file and applies them to a bunch of data. I do compile those rules into anonymous subroutines and call them later on. That is far faster then evaling the same code over and over again. Like so:
# @rules read from file or somewhere else my @rules = ( 'return( "A", "Cat A" ) if $_[0] < 75000 and $_[1] eq "ICE";', #... ); # convert rules to anon subs @rules = map { eval "\$_ = sub { $_ }"; } @rules; for ( @rules ) { print join ",", &$_(20000, "ICE"); #A, Cat A }


holli, /regexed monk/

Replies are listed 'Best First'.
Re^2: Returning data from an eval
by Tanalis (Curate) on Aug 02, 2005 at 09:42 UTC
    Meh .. dumb typo in the comparison there. Thanks.

    I'd not thought about using anonymous subs - thanks for the tip. It certainly seems to speed things up - I'm doing a fairly large number of iterations over the code.