Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between package, module and class in object oriented Perl?

Tags:

oop

module

perl

What is the difference between package, module and class in object oriented Perl?

like image 799
joe Avatar asked Jul 17 '09 15:07

joe


People also ask

What is the difference between package and module in Perl?

A Perl package is a collection of code which resides in its own namespace. Perl module is a package defined in a file having the same name as that of the package and having extension . pm. Two different modules may contain a variable or a function of the same name.

What is class and object in Perl?

Object Basics The object is stored as a reference in a scalar variable. Because a scalar only contains a reference to the object, the same scalar can hold different objects in different classes. A class within Perl is a package that contains the corresponding methods required to create and manipulate objects.

What is package name in Perl?

It's common in Perl parlance to treat the words “module” and “package” as synonyms, and in practice they almost refer to the same thing. A module name is shorthand for a filepath, but a package name refers to a namespace within the Perl symbol table.


2 Answers

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:

package SomePackage;

sub some_subroutine { ... } # really SomePackage::some_subroutine

You load the module to get access to the package:

use SomePackage; # read and compile the module file

SomePackage::some_subroutine( ... );

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:

package SomeClass;

sub class_method { my( $class, @args ) = @_; ... }
sub instance_method { my( $self, @args ) = @_; ... }

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:

 use SomeClass;

 my $i = SomeClass->class_method( ... ); 

The OO arrow syntax does some special stuff to let the some_method subroutine know that it's being called as a method. Perl puts the referent (the SomeClass in 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:

  SomeClass->new('world'); 

is syntactically the same as if you had called it with the class name as the first parameter:

  SomeClass::new( 'SomeClass' ,'world'); # no inheritance this way

That works the same for objects too. When an object is the referent:

my $i = SomeClass->new(); 
$i->bar( 'world'); 

the object is the first parameter as the method:

SomeClass::bar($i, 'world'); 
like image 182
Kent Fredric Avatar answered Oct 21 '22 09:10

Kent Fredric


Perl doesn't have classes. It has namespaces that you change with package. For the complete details of Perl OOP, see Intermediate Perl or Object Oriented Perl. You can also see the perltoot and perlboot documentation. In short, Perl fakes what people expect "real" classes to be with packages, normal subroutines, and references.

A module is a distributable piece of code contained in a file. See perlmod.


I say more about this in my post for The Effective Perler, Find a module's release managers. I don't get into the OO stuff, but I talk about the other terms around "module".

like image 22
brian d foy Avatar answered Oct 21 '22 08:10

brian d foy