Is there a way I can construct a package subroutine so that, regardless of whether:
it will do the Same Thing each time?
packageclass method
My answer: I think so. I'm not sure why you would want to do this, but it's the only thing I can see here.
Here's a method that would act that way (although it's not quite what you suggested). (untested):
package thisPack; sub smartish_sub { my $self; # for instance reference, if called that way my $class; # for classname, if called that way if (ref $_[0]) { if (UNIVERSAL::isa($self,'thisPack')) { # ( corrected from earlier ->isa call) # it's an instance method invocation my $self = shift @_; } # else it's not an instance, but it is a ref, so # it must be a fully-qualified invocation } else { # not a reference; ought to be class invocation my $class = shift; die "first argument to smartsub must be hashref\n" unless UNIVERSAL::isa($class, 'thisPack'); } # now any class- or instance- invocations have been # shifted off @_ my ($hash, $string) = @_; # might want error-checking here. print "$hash->{foo}\n$string"; } sub new { bless {}, shift; }
YMMV.
Updated (almost immediately): used UNIVERSAL::isa() everywhere, not ->isa().
second update: clarified package vs. class language.
In reply to Re: Calling Madness
by jkahn
in thread Calling Madness
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |