use strict; use warnings; # usually this is defined inside a perl module # at ./Whatever/Package.pm package Whatever::Package; # this is the constructor. generally called new but is not a rule sub new{ # we will receive the class name as first argument my $class = shift; # and just need to return a reference (an anonymous hash {} in this case) blessed into $class return bless {}, $class; } sub first { my $self = shift; print "We are inside package ",__PACKAGE__, " we received \$self because it is blessed into ",ref $self,"\n"; } # usually this is defined inside a perl module # at ./Another/Example/Package.pm package Another::Example::Package; # note the same NAME for the sub sub first { my $self = shift; print "This method will no be called unless the object is blessed into Another::Example::Package"; } # usually the script will use Whatever::Package (defined inside a perl module ./Whatever/Package.pm) # but we can merely switch back to the default package 'main' package main; # again a sub with the same name sub first{ print "We are inside package ",__PACKAGE__," I'm not a method but a bare lone sub\n"; } # test the local sub first(); # let 'instanciate' an object of the Whatever::Package class my $obj = Whatever::Package->new(); print "\$obj is now a blessed reference. It is blessed into ",ref $obj,"\n"; # above we have 3 subs named first. Which one will be called? $obj->first();