in reply to so simple

However...

If you really want to check to see if the sub is being invoked as a method or a function, you can use the following:

#!/usr/bin/perl package myClass; use strict; use warnings; ### Pure-cheese useless constructor sub new { bless {}, __PACKAGE__ } ### Bimodal sub sub someFunction { my $self = shift if @_ && UNIVERSAL::isa( $_[0], 'UNIVERSAL' ); my $otherArg = shift; print "Called as ", defined $self ? "method" : "function", " with '$otherArg' argument.\n"; } package main; use strict; use warnings; # Invoked as a function myClass::someFunction( "function thingie" ); # Invoked as a method my $testThingie = new myClass; $testThingie->someFunction( "method thingie" ); # It even works invoked as a static method myClass->someFunction( "static method thingie" );


It basically just uses the mother of all superclasses ('UNIVERSAL') to check for object/class-ness. If a thing can be said to inherit from UNIVERSAL (have UNIVERSAL in its @ISA), then it can be assumed to be either a blessed reference (aka object), or the name of a class. What this function doesn't test is to see if the package that the first arg belongs to is actually your own. This is left as an excercise for the reader. =:)

Like Randal said, though: Once you venture down that path, your interface forever must support such nastiness, which can only lead to grief and ruin.