What is the difference between package, module and class in object oriented Perl?
What is the difference between package, module and class in object oriented Perl?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Modules are a single file, a .pm file that provides code. That could be no packages, a single package, or more than one package. A module doesn’t really care what is in it, so it can be code that inserts itself into the same namespace, a more-traditional set of subroutines in a library or define Perl’s idea of a class.
A package, also known as a namespace, contains its own variables and subroutines. It’s a way of segregating different parts of your program. You create the package and put your code into it:
You load the module to get access to the package:
A Perl class is a package and its associated behavior. The methods in a class are just normal subroutines, although when we treat the subroutines as methods, the first parameter is the thing (a package name or object, also known as the referent) that called method:
Since the class is just a package like any other package and probably lives in a module, you access it the same way with
use:The OO arrow syntax does some special stuff to let the
some_methodsubroutine know that it’s being called as a method. Perl puts the referent (theSomeClassin this case) as the first argument. Additionally, when using the OO syntax, Perl knows to use its inheritance features.Methods called with ‘->’ get the referent as the first parameter to the method, so this call:
is syntactically the same as if you had called it with the class name as the first parameter:
That works the same for objects too. When an object is the referent:
the object is the first parameter as the method: