To Joey The Saint -- sadly, I do not think the answer lies in the direction you suggest.

The problem is that $pat and $repl come as shown to the routine in question from elsewhere. (The code above is, of course, a reduced demo to focus on the problem.) The contents of $1, $2, etc. (if any) are not known until $pat is invoked here, so I do not see an obvious and practical way to *pre*-load the regex vars in $repl using double-quotish behavior. The substitution must be done *after* $repl comes to this routine.

I did travel a little way down the following path:

#!/usr/bin/perl -w use strict; my $str = 'abcadefaghi'; my $pat = '(a.)'; my $repl = '\u$1 '; $str =~ s/$pat/ my @save = (undef, $1, $2, $3, $4, $5, $6); $repl =~ s{\$$_}{$save[$_]}g for 1..6; $repl; /eg; print "$str\n";
...and that works for the '$1'-type vars without opening up the eval danger. The problem is that this approach then leaves me with having to write more similar code to handle all other special chars (e.g. '\u' as shown) with similar hand-coded replacements. Safe but yucky.

In reply to Re: Re: $1 in variable regex replacement string by dvergin
in thread $1 in variable regex replacement string by dvergin

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.