in reply to Re^2: How do I test the validity of a CODE reference?
in thread How do I test the validity of a CODE reference?

defined (which already provides this functionality)...

Yes, defined(&$sub) works fine, but the functionality seems different to me. My approach looks at the variable itself, whereas defined appears to be looking at the subroutine referenced by the variable.

Anyway .... my point is very moot .... at best :-)

Cheers,
Rob

Replies are listed 'Best First'.
Re^4: How do I test the validity of a CODE reference?
by ikegami (Patriarch) on Aug 08, 2008 at 05:14 UTC
    Looks like they both operate on the same thing (the CV*) to me. (If not, please explain, since I'm not very familiar with this stuff.) So the question is why does Perl and your function use different tests to determine the same thing?
    #define CvROOT(sv) ((XPVCV*)SvANY(sv))->xcv_root_u.xcv_root #define CvXSUB(sv) ((XPVCV*)SvANY(sv))->xcv_root_u.xcv_xsub #define CvOUTSIDE(sv) ((XPVCV*)SvANY(sv))->xcv_outside
      Looks like they both operate on the same thing (the CV*) to me

      Yes - when you descend to that level they become very similar, indeed :-)

      So the question is why does Perl and your function use different tests to determine the same thing?

      Good question. My approach won't work on perl-5.6.2, so perhaps history and portability might be part of the explanation. But there could, of course, be other reasons. (I couldn't even get correct results on perl-5.6.2 with CvROOT and CvXSUB.)

      Cheers,
      Rob

        Yes - when you descend to that level they become very similar, indeed :-)

        At a high level:
        validate $xdefined &$xref $x && defined &$x
        code refreturns resultreturns resultreturns result
        sub namediesreturns resultreturns false
        other refsdiesdiesdies
        undefined?stringifiedreturns false

        I couldn't even get correct results on perl-5.6.2 with CvROOT and CvXSUB

        That's odd, because defined and sub calls use CvROOT(cv) || CvXSUB(cv) in 5.6.2 as well.