diotalevi has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a small debugger for myself to generate Test::MockObject scripts. I'm able to trap method calls and function calls but I don't know how to distinguish between the two. Can any of you help?

BEGIN { $ENV{PERL5DB} = ''; $^P = 0x01; # Debug subroutine enter/exit. } $obj = bless do { \ my $x }, "Deb"; $obj->meth; Deb::func( $obj ); sub Deb::meth { } sub Deb::func { } package DB; sub sub { print "$sub( " . join( ', ', @_ ) . " )\n"; }

Results in:

Deb::meth( Deb=SCALAR(0x97f3c4c) ) Deb::func( Deb=SCALAR(0x97f3c4c) )

Replies are listed 'Best First'.
Re: Distinguish method/function call during debugger?
by ikegami (Patriarch) on Feb 23, 2006 at 17:12 UTC

    Is it even possible? What about

    sub Deb::func { print("Deb!\n"); } $obj = bless {}, 'Bar'; $obj->Deb::func();
    and even
    sub Foo::func { print("Foo!\n"); } @Deb::ISA = 'Foo'; $obj = bless {}, 'Bar'; $obj->Deb::func();

      Yeah, I don't actually expect to see that one in practice. Not with the code i'm instrumenting. If you want to go to that level, then there's also $obj->$code_ref and $obj->$string.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        My point was that Perl has so many call syntaxes that I doubt it knows how a function was called.

        By the way, I'm suprised you don't use it. In just about every inheriting class I write, I use $class->SUPER::new().

Re: Distinguish method/function call during debugger?
by adrianh (Chancellor) on Feb 24, 2006 at 11:23 UTC
    I'm able to trap method calls and function calls but I don't know how to distinguish between the two. Can any of you help?

    See called_as_method in Devel::Caller.

      Thanks! It turns out that Devel::Caller doesn't work for perl-5.8.4+ but it's the right idea.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        Thanks

        Y'welcome

        It turns out that Devel::Caller doesn't work for perl-5.8.4+

        Ah. Bugger :-/