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); #### object method called with a = 400 b = 800 class method called with a = 400 b = 800 subroutine called with a = 400 b = 800 missing key/value (a 400 b 800 900) at /Users/adrianh/Desktop/foo.pl line 35 #### ref($_[0]) and ( ref($_[0]) eq (caller(0))[0] || ( caller(0) )[0] eq __PACKAGE__ ) #### use vars qw( $VERSION @ISA @EXPORT_OK %EXPORT_TAGS ); use Exporter; $VERSION = 0.01_0; # 12/28/02, 1:54 am @ISA = qw( Exporter ); @EXPORT_OK = qw( shave_opts coerce_array OOorNO myargs myself ); %EXPORT_TAGS = ( 'all' => [@EXPORT_OK] ); #### use base qw(Exporter); our $VERSION = 0.01_0; our @EXPORT_OK = qw( shave_opts coerce_array OOorNO myargs myself ); our %EXPORT_TAGS = ( 'all' => [@EXPORT_OK] ); #### # -------------------------------------------------------- # Constructor # -------------------------------------------------------- sub new { bless( {}, shift (@_) ) } #### # -------------------------------------------------------- # Class::OOorNO::Class::OOorNO() # -------------------------------------------------------- sub OOorNO { return ( $_[0] ) if defined( $_[0] ) and UNIVERSAL::can( $_[0], 'can' ); } #### sub DESTROY { } sub AUTOLOAD { } #### use strict; use warnings; use Test::More tests => 3; use Test::Exception; BEGIN {use_ok 'Class::OOorNO'}; my $o = Class::OOorNO->new; isa_ok $o, 'Class::OOorNO'; dies_ok {$o->this_method_does_not_exist} 'unknown method dies'; #### ok 1 - use Class::OOorNO; ok 2 - The object isa Class::OOorNO not ok 3 - unknown method dies # Failed test (t/foo.t at line 13) # Looks like you failed 1 tests of 3. #### ok 1 - use Class::OOorNO; ok 2 - The object isa Class::OOorNO ok 3 - unknown method dies ok #### sub OOorNO { return ( $_[0] ) if defined( $_[0] ) and UNIVERSAL::can( $_[0], 'can' ); } #### sub OOorNO { return $_[0] if UNIVERSAL::can( $_[0], 'can' ); } #### sub OOorNO { UNIVERSAL::can( $_[0], 'can' ); } #### sub new { bless({ }, shift(@_)) } #### sub new { bless {}, shift }