%d = ( foo => sub { "[@_]"; }, bar => sub { "(@_)"; }, ); $_ = 'foo fooz bar baz1'; s/(\w+)(?(?{ !$d{$1} })(?!))/$d{$1}->($1)/ge; print "$_\n"; #### [foo] [foo]z (bar) baz1 #### s/ (\w+) # One or more word chars in capture buffer 1. Got it. ( # Embedding a regexp modification character or what? # So everything within this set of parenthesis is # supposed to evaluate to either m, s, i, x, p, g, or c? # I don't get it. # Also, why is all this on the left side of s\\? Why are we # _matching_ this? ?(?{ !$d{$1} }) # This is really just used as a boolean to # indicate that a hash key exists. I get it, # kind of. (?!) # From perldoc, "a zero-width negative look-ahead # assertion. I might understand this if only there was # a pattern after the "?!". ) /$d{$1}->($1)/gex; print "$_\n"; # Here is my walkthrough for 'foo'. # 1. (\w+) matches 'foo'. # 2. $d{$1} is _true_. !_true_ = false. In this case, false is "1". # 3. (?!) says to *not* match a "1" followed by null. Maybe? # 4. (?...) means use the 1 not followed by null as a regexp modifier # to 'foo'? What?!