use strict; use warnings; { package Foo; use Devel::Caller qw(called_as_method); use Carp; sub new { bless {}, shift }; sub display { my $self; # extract $self if called as a method $self = shift if called_as_method(0); # check we have even number of key/value pairs croak "missing key/value (@_)" unless @_ % 2 == 0; # make a hash of our key/value pairs my %hash = @_; # show how we were called my $type = "subroutine"; $type = (ref($self) ? "object" : "class") . " method" if $self; print "$type called with\n"; while (my ($k, $v) = each %hash) { print "$k = $v\n" }; print "\n"; }; }; Foo->new->display(a => 400, b=>800); Foo->display(a => 400, b=>800); Foo::display(a => 400, b=>800); Foo::display(a => 400, b=>800, 900);