in reply to Re: Re: eval routines for faster regexp: how-to...
in thread eval routines for faster regexp: how-to...

BTW, you can also use eval to make subs (rather than references to subs), which is what I do quite often if I have a lot of matches to find:

sub genFind { my $patt = shift; my $code = "sub find {\n\tmy \$str = shift;\n\t\$str =~/$patt/;\n}; eval $code; die "Error in eval: $@\ncode = $code\n" if $@; } #call genFind ('foo'); $res = find ('barefoot'); #can now call 'find' as function
The nice thing about this is that looks just like a normal function and you don't have to worry about '&' and stuff.

pike

Replies are listed 'Best First'.
Hey, this isn't python :)
by Fletch (Bishop) on Nov 08, 2001 at 18:47 UTC

    You don't really need the "\n" and "\t"s in there. If you're really after readability consider using a heredoc which will let you actually format the code readably.

    my $code = <<EOSUB; sub find { my \$str = shift; \$str =~/$patt/; } EOSUB