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

Peter Hutnick pm at hutnick.com
Thu May 6 12:59:45 CDT 2004


Rob Nagler wrote:

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

This gives me:

Backslash found where operator expected at (eval 1) line 1, near "a \"
         (Do you need to predeclare a?)

The first rule is "s/\\emph{(.*?)}/\<em\>$1\<\/em\>/g;" which works if I 
just paste it in.

> 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>/',

Since TeX commands are in the form \bla I am confused about why you put 
\\\\bla, not \\bla.

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

I really need to isolate as much of the complexity as possible into the 
code.

-Peter



More information about the Boulder-pm mailing list