Here is complete story.

Thanks -- this was brief and clear enough that you should have put that into the OP, instead of making up a "pseudo/generic" case to confuse us. :)

Anyway, I would tend to prefer taking a two-step approach: apply all the substitutions that work, and after those are all done, if any residual template-variable markers are left behind, then croak. Something like this:

my %hash = ( var1 => 'value1', var2 => 'value2' ); # ... or whatever... while ( $tmpl =~ /@~(.*?)~@/ and exists( $hash{$1} )) { my $var = $1; $tmpl =~ s/@~$var~@/$hash{$var}/g; } croak "Bad template text:\n$tmpl" if ( $tmpl =~ /@~|~@/ );
"Basic" regex substitutions tend to be obscure and cryptic enough by themselves, without adding flow-control logic into the rhs. The code will be easier to maintain if you segregate the regex operations from the flow-control.

In reply to Re^5: die in regexp by graff
in thread die in regexp by powerman

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.