I share some confusion about what you want, but perhaps this a closer approximation to it:

c:\@Work\Perl>perl -wMstrict -le "my %xlate_map = qw/ ( l_paren ) r_paren { l_curly } r_curly /; my $xlate_targets = join '', map quotemeta, keys %xlate_map; ;; sub xlate { my ($s, $targets, $hr_map) = @_; ;; (my $t = $s) =~ s{ ([$targets]) }{$hr_map->{$1}}xmsg; return $t; } ;; my $str = 'a { b } c ( d ) e'; my $xlt = xlate($str, $xlate_targets, \%xlate_map); print qq{'$xlt'}; " 'a l_curly b r_curly c l_paren d r_paren e'

Update 1: This can be generalized (and slowed down) a bit, and also, if you have Perl version 5.14+, simplified (and perhaps un-slowed) a bit (note use of  /r regex modifier in  s///r substitution):

c:\@Work\Perl>perl -wMstrict -le "use 5.014; ;; my %xlate_map = qw/ ( l_paren ) r_paren { l_curly } r_curly /; ;; sub xlate { my ($s, $hr_map) = @_; ;; my $targets = join '', map quotemeta, keys %$hr_map; return $s =~ s{ ([$targets]) }{$hr_map->{$1}}xmsgr; } ;; my $str = 'a { b } c ( d ) e {{F}} ((G))'; my $xlt = xlate($str, \%xlate_map); print qq{'$xlt'}; " 'a l_curly b r_curly c l_paren d r_paren e l_curlyl_curlyFr_curlyr_cur +ly l_parenl_parenGr_parenr_paren'

Update 2: Another approach: complete encapsulation; works with any version; perhaps a bit faster (note  /o regex modifier in  s///o substitution):

c:\@Work\Perl>perl -wMstrict -le "print qq{Perl version $] }; ;; BEGIN { my %xlate_map = qw/ ( l_paren ) r_paren { l_curly } r_curly /; my $xlate_targets = join '', map quotemeta, keys %xlate_map; ;; sub xlate { my ($s) = @_; ;; (my $t = $s) =~ s{ ([$xlate_targets]) }{$xlate_map{$1}}xmsog; return $t; } } ;; my $str = 'a { b } c ( d ) e {{F}} ((G))'; my $xlt = xlate($str); print qq{'$xlt'}; " Perl version 5.008009 'a l_curly b r_curly c l_paren d r_paren e l_curlyl_curlyFr_curlyr_cur +ly l_parenl_parenGr_parenr_paren'

Any of these approaches can be further simplified to operate directly upon  $_ if that's what you really need.


In reply to Re: Regex - Is there any way to control when the contents of a variable are interpolated? (Using "$1" and '$1' in regex replacements) by AnomalousMonk
in thread Regex - Is there any way to control when the contents of a variable are interpolated? (Using "$1" and '$1' in regex replacements) by JDoolin

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.