Consider the output of your original regex and these two variations (I don't know where the  'baz1' comes from in your output):
>perl -wMstrict -le "my %d = ( foo => sub { \"[@_]\"; }, bar => sub { \"(@_)\"; }, ); $_ = 'foo fooz bar'; s/ (\w+) (?(?{ !$d{$1} }) (?!)) / $d{$1}->($1) /xge; print; " [foo] [foo]z (bar) >perl -wMstrict -le "my %d = ( foo => sub { \"[@_]\"; }, bar => sub { \"(@_)\"; }, ); $_ = 'foo fooz bar'; s/ (\w+) / $d{$1}->($1) /xge; print; " Use of uninitialized value in subroutine entry at ... Can't use string ("") as a subroutine ref while "strict refs" ... >perl -wMstrict -le "my %d = ( foo => sub { \"[@_]\"; }, bar => sub { \"(@_)\"; }, ); $_ = 'foo fooz bar'; s/ (\w+) / $d{$1} ? $d{$1}->($1) : $1 /xge; print; " [foo] fooz (bar)
Then consider the  (?(condition)yes-pattern) construct in the Extended Patterns section of perlre and the fact that, if the null regex is always true, wouldn't the negative look-ahead to the null regex  (?!) always be false? (Coupled with realizing what the logical value of  !$d{$1} is when  $1 doesn't exist in the hash.)

In reply to Re: Regular expression operators (?{code}), (?!pattern), and (?...) by AnomalousMonk
in thread Regular expression operators (?{code}), (?!pattern), and (?...) by jffry

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.