Distinguishing Class and Object Methods

First let me start with the definition of class and object method

What is Class Method? – Method which is called without creating an object of class.

What is Object Method? – Method which is called by the reference of object.

In PERL there is no such disquisition, but let me tell you the same can be done by adding just three lines of code in function

sub func1 {
my $self = shift;
unless( ref $self) {                                                                  # Line 1
croak “Should call func1() with an object, not a class”;         # Line 2
}                                                                                          # Line 3
return “1”;
}

 

Advertisements