Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Oh wise ones,

I am trying to write a function that returns a string that is assembled from a regex, but the order that the captured substrings are assembled in is determined by one of the passed arguments. Here is an overly simplified example of what I'm trying to do:

my $newstring = munge_string( 'one_two_three', '312' ); sub munge_string { my ( $string, $patternkey ) = @_; # patterns can also be 'text$1$2$3', etc my %patterns = ( 123 => '$1$2$3', 312 => '$3$1$2' ); # in reality the regex is also dynamic (based on $patternkey) $string =~ m/(\w+)_(\w+)_(\w+)/; return $patterns{$patternkey}; # want 'threeonetwo', got '$3$1$2' }
How can I get the function to return a double-interpolated value (once to get the hash value, then again to get the captured substrings)?

I tried using eval (to no avail), and then s///e instead of m//, but I only got single interpolation:

$string =~ s/(\w+)_(\w+)_(\w+)/$patterns{$patternkey}/e; print $string; # '$3$1$2'
If I put the replacement pattern in directly, though, it works fine:
$string =~ s/(\w+)_(\w+)_(\w+)/$3$1$2/; print $string; # 'threeonetwo'
$string =~ s/(\w+)_(\w+)_(\w+)/join( '', $3, $1, $2 )/e; print $string; # 'threeonetwo'

Is there a way to do this cleanly, or am I going about this the wrong way? Thanks in advance.

In reply to Double interpolation of captured substrings by bobf

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (5)
    As of 2020-10-20 09:43 GMT
    Find Nodes?
      Voting Booth?
      My favourite web site is:

      Results (209 votes). Check out past polls.