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.


In reply to Backreference woes by bemfica

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.