bemfica has asked for the wisdom of the Perl Monks concerning the following question:
I need to do a good amount of text replacement in a series of old style JSP pages (they used to call them JHTML...). I wrote a perl script to do it and much of it works fine, except for backreferences. I tried it on the command line and found it works fine:
However, when I use it in a script, it does not work:> echo 'JHTML `Java in here` code' | perl -p -e 's/`(.*)`/<%=$1%>/go' > > JHTML <%=Java in here%> code
Where "myReplace.pl" is> echo 'JHTML `Java in here` code' | ./myReplace.pl '`(.*)`' '<%=$1%>' > > About to replace: s/`(.*)`/<%=$1%>/go > JHTML <%=$1%> code
myReplace.pl above is stripped to show only what doesn't work... I wondered if the shell required some extra escaping in the $1, but the print line appears to show that the script got the $1 fine. I also tried having the expressions and replacements read from a CSV file (my initial plan, actually) but that does not work either.#!/usr/bin/perl -w my $searchExp = $ARGV[0]; my $replaceExp = $ARGV[1]; my $textIn = <STDIN>; my $count = 0; # Do the replacing print("About to replace: s/$searchExp/$replaceExp/go \n"); $count = ($textIn =~ s/$searchExp/$replaceExp/go); if ($count > 0) { print $textIn; } exit;
Sorry if this is an obvious mistake - I cannot find it myself, and would appreciate your help.
A.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Backreference woes
by Zaxo (Archbishop) on Dec 30, 2003 at 04:47 UTC | |
by bemfica (Initiate) on Jan 02, 2004 at 04:42 UTC | |
|
Re: Backreference woes
by Roger (Parson) on Dec 30, 2003 at 05:04 UTC | |
by bemfica (Initiate) on Jan 02, 2004 at 04:44 UTC | |
|
Re: Backreference woes
by ysth (Canon) on Dec 30, 2003 at 06:17 UTC | |
by bemfica (Initiate) on Jan 02, 2004 at 04:47 UTC |