in reply to Ambiguous use of X resolved to Y

my $old = \&Pod::HTML::scan_podpath; { local $^W; *Pod::HTML::scan_podpath = sub { &{$old}( @_ ); }; }

Replies are listed 'Best First'.
Re: Re: Ambiguous use of X resolved to Y
by dpmott (Scribe) on Feb 05, 2004 at 23:43 UTC
    I seem to recall trying an incantation very similiar to that, but it ended up causing deep recursion. It seemed that the *{name}{CODE} syntax was required to make it work right.

    I suspect that what happens, is that a reference to the actual function in the module is lost when the glob gets reassigned. The reference held in $old is to a glob, and calling a function on it essentially references *{$old}{CODE}, which isn't really what you wanted (because it's the new one). That causes recursion.

    If I'm understanding that wrong, someone please correct me.

    -Dave
      That could happen if the function didn't already exist before you took a reference from it. Be sure to have loaded the code for that function before you run that snippet. Consider also checking for recursion:
      my $old = \&Pod::HTML::scan_podpath; { local $^W; *Pod::HTML::scan_podpath = sub { if ( $old == \&Pod::HTML::scan_podpath ) { croak "..."; } &{$old}( @_ ); }; }