If you want to know how to create a class-method or object-method that can also work as an independent function, there are two ways I would feel comfortable doing it:
- If the function take a constant number of arguments, use that to determine how it was called:
# $obj->fooify(1,2,3) or fooify(10,20,30)
# always expects 3 "real" args
sub fooify {
my $self;
$self = shift if @_ == 4; # if 4 args, the first is the object/clas
+s
my ($x, $y, $z) = @_;
# ...
}
- If you can be quite sure the arguments will never be the name of a subclass or an object in a subclass of the class you're in, you can do:
sub fooify {
my $self;
$self = shift if UNIVERSAL::isa($_[0], __PACKAGE__);
my ($x, $y, $z) = @_;
# ...
}