This is the way I would tend to approach this type of problem. See haukex's article Building Regex Alternations Dynamically (a technique I seem to have posted about quite often recently).

c:\@Work\Perl\monks>perl -wMstrict -le "my $text = 'text with a <%VAR%> and <%FOO%> to interpolate <%OOPS%>'; print qq{'$text'}; ;; my $vars = { 'VAR' => 'Value Taken From A Data Structure', 'FOO' => 'Another One', }; ;; my $rx_pre = qr{ <% }xms; my $rx_post = qr{ %> }xms; ;; my ($rx_var) = map qr{ $_ }xms, join '|', map quotemeta, reverse sort keys %$vars ; print 'regex: ', $rx_var; ;; $text =~ s{ $rx_pre ($rx_var) $rx_post }{$vars->{$1}}xmsg; print qq{'$text'}; ;; log_unknown(\$text, $1, $-[1]) while $text =~ m{ ($rx_pre .*? $rx_pos +t) }xmsg; ;; ;; sub log_unknown { my ($sr_text, $unknown_tag, $offset) = @_; ;; print qq{unable to interpolate '$unknown_tag' at offset $offset} } " 'text with a <%VAR%> and <%FOO%> to interpolate <%OOPS%>' regex: (?msx-i: VAR|FOO ) 'text with a Value Taken From A Data Structure and Another One to inte +rpolate <%OOPS%>' unable to interpolate '<%OOPS%>' at offset 77
One might also define a regex to match the acceptable pattern of any key (interpolation tag) in the  $vars hash, then use it both to check those keys when building the  $rx_var regex and then later in place of  .*? to look for unknown/failed interpolations.


Give a man a fish:  <%-{-{-{-<


In reply to Re^4: Invoke the Perl string interpolation engine on a string contained in a scalar variable. by AnomalousMonk
in thread Invoke the Perl string interpolation engine on a string contained in a scalar variable. by ibm1620

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.