in reply to Re^2: Check exist of anonymous subroutine before calling it within regexp substitute?
in thread Check exist of anonymous subroutine before calling it within regexp substitute?

ty, i confess that at a first look i didn't understand why, could you elaborate plz?
  • Comment on Re^3: Check exist of anonymous subroutine before calling it within regexp substitute?

Replies are listed 'Best First'.
Re^4: Check exist of anonymous subroutine before calling it within regexp substitute?
by ikegami (Patriarch) on Oct 30, 2009 at 14:53 UTC
    use strict; use warnings; my %d = ( foo => sub { "[@_]"; }, bar => sub { "(@_)"; }, ); $_ = 'baz1'; s/(\w+)(??{!$d{$1}})/$d{$1}->($1)/ge; print "$_\n";
    Use of uninitialized value in subroutine entry at line 10. Can't use string ("") as a subroutine ref while "strict refs" in use a +t line 10.

    $1 is is "baz", so $d{$1} is undef, so !$d{$1} is 1, so the match tries to match "1". It does, so the replacement expression is executed.