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

#!/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;
This works because LOCK_EX (which is what $lock_ex_coderef points to) isn't defined at that point.

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

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;
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).
HTH

_________
broquaint


In reply to Re: How to instrument a "fake" subroutine? by broquaint
in thread How to instrument a "fake" subroutine? by samtregar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.