I agree it's very annoying that on the callee side, i.e. in the sub, it's virtually impossible to distinguish between being called as a function or as a method.
{ package Foo; use Devel::Caller qw(called_as_method); sub new { bless {}, shift } sub foo { my $arg = shift; if (called_as_method(0)) { ref($arg) ? "object method" : "class method"; } else { "subroutine"; } } } use Test::More tests => 3; my $foo = Foo->new; is( Foo->foo, "class method" ); is( $foo->foo, "object method" ); is( Foo::foo($foo), "subroutine" );
(although wanting to do this in real code is nearly always a sign that you need to rethink your design :-)
In reply to Re^5: Perl module symbol import versus direct invocation
by adrianh
in thread Perl module symbol import versus direct invocation
by JayBonci
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |