in reply to How to instrument a "fake" subroutine?

Interesting... Fcntl::AUTOLOAD never even gets called, and for some reason whatever's in the saved coderef just calls us again. My wild guess is that Perl puts some sort of magic reference to the autoloading code in that symbol table entry when it gets exported. When this code gets called, it looks and sees that the sub has been defined in the meantime (by you), and happily calls that, rather than looking for an autoload.

In any case, temporarily putting the old glob back in place fixes it, and AUTOLOAD is only called the first time, as things should be.

#!/usr/bin/perl -w use Fcntl qw(LOCK_EX); # instrument LOCK_EX my $lock_ex_coderef = \&Fcntl::LOCK_EX; my $newglob = *Fcntl::LOCK_EX = sub { print "LOCK_EX called\n"; no warnings 'redefine'; *Fcntl::LOCK_EX = $lock_ex_coderef; if (wantarray) { my @ret = &$lock_ex_coderef; *Fcntl::LOCK_EX = $newglob; @ret; } else { my $ret = &$lock_ex_coderef; *Fcntl::LOCK_EX = $newglob; $ret; } }; # call LOCK_EX print $_ = LOCK_EX; print "\n";
/s

Replies are listed 'Best First'.
Re: Re: How to instrument a "fake" subroutine?
by samtregar (Abbot) on May 22, 2002 at 21:09 UTC
    Interesting solution. I have some vague worries about recursive calls to instrumented subs... I think maybe only the first call would get caught since by the time the second call comes the old sub is restored. But I'll have to work up a test case to be sure.

    Thanks,
    -sam