[Boulder.pm] Trickey (for a newbie) String Replacement

Rob Nagler nagler at bivio.biz
Thu May 6 12:34:50 CDT 2004


Peter Hutnick writes:
>     foreach $rule (@rules) {
>        $text =~ (eval $rule)

You could:

    eval("$text =~ $rule");

This assumes a certain structure to $rule.  Alternatively, you could
use the $_ approach, that is, have the $rule assume a specific
variable name that contains the text, e.g., write the rules like:

    '$text =~ s/\\\\bla/<bla>/',

The advantage of using $_ is that the rules are shorter and easier to
read.

> But the eval doesn't expand the way I'd like.  I've blindly tried 
> various quotes and brackets with no luck.  Is it just impossible to have 
> an eval as a part of a statement?

eval is perl so it can do anything perl can do.  On the flip side,
going with a more structured language for the rules would allow you to
avoid eval, which makes your code easier to debug.  Generally, code
which generates code and executes it is more difficult to understand.

Rob



More information about the Boulder-pm mailing list