In Perl, how can I determine if a subroutine was invoked as a method? -
is there way determine whether subroutine invoked method (with @isa probing) or plain subroutine? perhaps sort of extension module super-caller()?
for example, given
package ad::hoc; sub func() { ... }
how can func()
discriminate between following 2 invocations:
ad::hoc->func; # or $obj->func ad::hoc::func('ad::hoc'); # or func($obj)
(i know, desire likely indication of poor design™.)
see if devel::caller helps. changed code invoke func
on object , seems work on mac perl
5.14.3 (and 5.24.0):
called_as_method($level)
called_as_method returns
true if subroutine @$level
called method.
#!/usr/bin/env perl package ad::hoc; use strict; use warnings; use devel::caller qw( called_as_method ); sub func { printf "%s\n", called_as_method(0) ? 'method' : 'function'; return; } package main; use strict; use warnings; ad::hoc->func; ad::hoc::func();
output:
method function
Comments
Post a Comment