in reply to Re^2: Generic Parsing class with callback
in thread Generic Parsing class with callback
I'll take a closer look later when I have more time, but I didn't realize you were doing substitution regexes. For that, you either have to use eval or here are two other methods that I know how to get around the eval call.
Force the caller to pass in a code reference with the entire details of the substitution...
my $str = "This is one thing. This."; sub cref_regex { my ($str, $re_cref) = @_; print $re_cref->($str); } my $cref = sub { $_[0] =~ s/This/That/ig; }; cref_regex($str, $cref);
...or, force the caller to send in both a $search and if required, an optional $replace variable...
sub separate_regex { my ($str, $search, $replace) = @_; if (! $replace){ print $str =~ /$search/; } else { print $str =~ s/$search/$replace/g; } } separate_regex($str, 'This', 'That');
Both of those are more work for the caller, so I can't tell which is better in the long run. I've just always heard "don't use string eval", so I try to avoid it where possible.
In a recent module of mine I updated, I opted for the $search, $replace option.
-stevieb
|
|---|