in reply to Regular expression operators (?{code}), (?!pattern), and (?...)
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.)>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)
|
|---|