bemfica has asked for the wisdom of the Perl Monks concerning the following question:

Sage monks

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:

> echo 'JHTML `Java in here` code' | perl -p -e 's/`(.*)`/<%=$1%>/go' > > JHTML <%=Java in here%> code
However, when I use it in a script, it does not work:
> echo 'JHTML `Java in here` code' | ./myReplace.pl '`(.*)`' '<%=$1%>' > > About to replace: s/`(.*)`/<%=$1%>/go > JHTML <%=$1%> code
Where "myReplace.pl" is
#!/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;
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.

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

    With the line my $textIn = <STDIN>; you only get one line of input. If that line has no backticks you get no substitution. You probably want to do a while (<STDIN>) {} loop around the substitution and printing.

    If you get a line with two backticked sections, you will get a surprise. The regex qr/`(.*)`/ is greedy and will match everything between the first on the line and the last. Try qr/`(.*?)`/ or qr/`([^`]*)`/ to get what you want.

    After Compline,
    Zaxo

      Thank you very much for the hints. I knew about the "while" bit, and had stripped it only for the posting. I do welcome the reminder, though - I should get used to it (as well as the "greedy" bit) or will eventually be surprised!

      A.

Re: Backreference woes
by Roger (Parson) on Dec 30, 2003 at 05:04 UTC
    I think your immediate problem is not to do with regular expression matching or shell escape. Your immediate problem is because you are trying to replace the matching pattern with '$1' as an exact string, not as a back reference. Try this instead...

    #!/usr/bin/perl -w my $searchExp = $ARGV[0]; my $replaceExp = $ARGV[1]; my $textIn = <STDIN>; my $count = 0; # Do the replacing eval "\$count = (\$textIn =~ s/$searchExp/$replaceExp/go)"; if ($count > 0) { print $textIn; } exit;
    And the output is exactly as expected...
    >> echo 'JHTML `Java in here` code' | perl p11.pl '`(.*)`' '<%=$1%>' JHTML <%=Java in here%> code
      Yes, that does it! Thank you very much for the help, it is much appreciated.

      A.

Re: Backreference woes
by ysth (Canon) on Dec 30, 2003 at 06:17 UTC
    I actually understand how to do this kind of thing, but not when I'm as tired as I am now. I think this works as you want, but I'll leave it to others to explain why:
    $count = ($textIn =~ s/$searchExp/"\"$replaceExp\""/goee);
      Wow! Thanks for the reply, this is excellent. I will do some reading on the meaning of "e" - I though I'd got it, but need to work out the reason for its doubling... Again, many thanks.

      A.