tags:

views:

919

answers:

2

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

+12  A: 

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 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');
Kent Fredric
That's a pretty roundabout way of saying that Perl < 6 doesn't have classes ;-)
innaM
Although, that said, I kinda *like* that behaviour. Primarily got short-shafted by PHP to get that way of thinking though, I mean, it doesn't have, ( and will never have , ( *puts fingers in ears* ) ) working namespace support.
Kent Fredric
Packages aren't scopes. They are lexically scoped, but don't define a scope.
brian d foy
Perl doesn't have classes. Perl's OOP model uses references, subroutines, and packages to do what people expect classes to do, but there's no thing that is a class. You don't have class data for that reason, and have to fake it with one package per module with lexical variables in the file-scope.
brian d foy
Perl classes don't need to have a constructor. You only need that to create instances, which you don't have to do.
brian d foy
@brian d foy: it's clearer to say: the notion of current package is lexically scoped, but packages are not.
ysth
@ysth: no, it's clearer my way. All packages are scoped. It doesn't matter if it is the current package or not.
brian d foy
Not sure I agree with brian; Perl 5 has explicit support for class-based OO (consider UNIVERSAL and @ISA). Classes are not themselves instance of classes, but Perl has many non-metacircular aspects. There is clearly a call stack, but there is no stack object; it has lexical environments, but there is no lexical environment object. (Incidentally, all three of these things were simple enough to hack in.)
jrockway
@jon: you can do class-based OO stuff, but there's no thing called a class. You're still just using namespaces. I'm comparing this to languages which actually have thingys that are classes as a first-order object.
brian d foy
MooseX::Declare does blur the lines a bit conceptually, but it still works with the same stuff under the hood.
Kent Fredric
+9  A: 

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.

brian d foy