http://www.perlmonks.org?node_id=804008


in reply to Check exist of anonymous subroutine before calling it within regexp substitute?

Just to add something more on the table, here using the experimental (??{CODE})

%d = ( foo => sub { "[@_]"; }, bar => sub { "(@_)"; }, ); # case 1 $_ = 'foo fooz bar'; s/(\w+)/$d{$1}?$d{$1}->($1):"$1"/ge; print "$_\n"; # case 2 $_ = 'foo fooz bar'; s/(\w+)(??{!$d{$1}})/$d{$1}->($1)/ge; print "$_\n"; __OUT__ [foo] fooz (bar) [foo] [foo]z (bar)