in reply to How to instrument a "fake" subroutine?
Can anyone explain why a calling code-ref to a "fake" sub causes this recursion?Because it's just like a normal sub calling itself since the coderef is pointing to the sub that is currently being called.
A way around calling the fake sub would be to see if the coderef was pointing to an actual sub living in a symbol table somewhere
This works because LOCK_EX (which is what $lock_ex_coderef points to) isn't defined at that point.#!/usr/bin/perl -w use Fcntl qw(LOCK_EX); my $lock_ex_coderef= \&Fcntl::LOCK_EX;; # instrument LOCK_EX *Fcntl::LOCK_EX = sub { print "LOCK_EX called\n"; return &$lock_ex_coderef if defined &$lock_ex_coderef; }; # call LOCK_EX $_ = LOCK_EX;
Another (albeit more hackish) way to see if a dummy sub is being called is to see if the coderef and the symbol table entry have the same reference value
This is very icky (it won't pass string any time soon ;-), but somewhat effective as it halts recursion by seeing that it would be calling itself. Also having written the code I learned that $lock_ex_coderef is merely pointing to an entry in the symbol table whereas &Fcntl::LOCK_EX is pointing to a coderef (which is why the $lock_ex_coderef assignment had to go after the &Fcntl::LOCK_EX assignment).use Fcntl qw(LOCK_EX); # define here so the &Fcntl::LOCK_EX can see it my $lock_ex_coderef; # instrument LOCK_EX *Fcntl::LOCK_EX = sub { print "LOCK_EX called\n"; return &$lock_ex_coderef unless $lock_ex_coderef eq *{$Fcntl::{LOCK_EX}}{CODE}; }; $lock_ex_coderef = \&Fcntl::LOCK_EX; # call LOCK_EX $_ = LOCK_EX;
_________
broquaint
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: How to instrument a "fake" subroutine?
by samtregar (Abbot) on May 22, 2002 at 21:07 UTC | |
by broquaint (Abbot) on May 22, 2002 at 21:22 UTC | |
by samtregar (Abbot) on May 22, 2002 at 21:24 UTC |