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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s