Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Packages, modules, classes and objects


Published on

This is the tenth set of slightly updated slides from a Perl programming course that I held some years ago.
I want to share it with everyone looking for intransitive Perl-knowledge.
A table of content for all presentations can be found at
The source code for the examples and the presentations in ODP format are on

Published in: Technology
  • Be the first to comment

Packages, modules, classes and objects

  1. 1. Perl Programming Course Packages, modules, classes and objectsKrassimir
  2. 2. Contents1. What is a module?2. What is a package?3. What is a class?4. What is an object?5. Working with objects • bless and ref • tie and tied • use and require6. Using Modules7. Creating a module
  3. 3. What is a module?• A module is just a set of related functions and variables in a library file • ...a Perl package with the same name as the file.• It is specifically designed to be reusable by other modules or programs• May provide mechanism for exporting some of its symbols into the symbol table of any package using it• May function as a class definition and make its semantics available implicitly through method calls on the class and its objects• See Exporter and perlmodlib
  4. 4. What is a package?• package NAMESPACE • declares the compilation unit as being in the given namespace • The scope of the package declaration is from the declaration itself through the end of the enclosing block, file, or eval (the same as the my operator) • Refer to things in other packages by prefixing the identifier with the package name and a double colon: $Package::Variable • See perlfunc/package, perlmod/Packages package Human; our $legs = 2; our $hands = 2;
  5. 5. What is a class?• There is no special class syntax in Perl• A package acts as a class if it provides subroutines to act as methods• A class may be thought as a user-defined type.• use base to both load the base classes (packages) and inherit from them• A class should provide one or more ways to generate objects package Dog; use base qw(Mammal); sub new { my $class = shift; my $self = {}; bless $self, $class; }
  6. 6. What is an object?• An Object is Simply a Reference with user-defined type• Perl doesnt provide any special syntax for constructors• A Method is simply a Subroutine• A method expects its first argument to be the object (reference) or package (string) it is being invoked on• A constructor is a subroutine that returns a reference to something "blessed" into a class• Constructors are often class methods• You could think of the method call as just another form of dereferencing
  7. 7. Working with objects• bless REF,CLASSNAME bless REF • tells the thingy referenced by REF that it is now an object in the CLASSNAME package and returns the reference • If CLASSNAME is omitted, the current package is used • NOTE: Always use the two-argument version to enable inheritance Make sure that CLASSNAME is a true value
  8. 8. Working with objects• bless - Example: { package Dog; sub new { my $class = shift; my $self = { name =>Puffy, nationality =>bg_BG, }; bless $self, $class; } sub name { return $_[0]->{name} } #... }
  9. 9. Working with objects• ref EXPR ref Returns a non-empty string if EXPR is a reference, the empty string otherwise. • If EXPR is not specified, $_ will be used. • The value returned depends on the type of thing the reference is a reference to. • Builtin types include: SCALAR, ARRAY, HASH, CODE, REF, GLOB, LVALUE, FORMAT, IO, Regexp • If the referenced object has been blessed into a package, then that package name is returned instead. • You can think of ref as a typeof operator. • See for MORE.
  10. 10. Working with objects• tie VARIABLE,CLASSNAME,LIST • binds a variable to a package class that will provide the implementation for the variable • VARIABLE is the name of the variable to be tied • CLASSNAME is the name of a class implementing objects of correct type • Any additional arguments are passed to the new method of the class (meaning TIESCALAR , TIEHANDLE , TIEARRAY , or TIEHASH ) • See perlfunc/tied, perltie, Tie::Hash, Tie::Array, Tie::Scalar, and Tie::Handle • See DB_File or the Config module for interesting tie implementations. See also for EXAMPLE
  11. 11. Working with objects• tied VARIABLE • Returns a reference to the object underlying VARIABLE • The same value was originally returned by the tie call that bound the variable to a package. • Returns the undefined value if VARIABLE isnt tied to a package. • See and for examples
  12. 12. Working with objects• use Module VERSION LIST use Module use VERSION • It is exactly equivalent to BEGIN { require Module; Module->import( LIST ); } • The BEGIN forces the require and import to happen at compile time. • import imports the list of features into the current package • If no import method can be found, the call is skipped • In use VERSION form VERSION has to be a numeric argument such as 5.016, which will be compared to $]
  13. 13. Working with objects• require VERSION require EXPR require • demands that a library file be included if it hasnt already been included • The file is included via the do-FILE mechanism, • Lexical variables in the invoking script will be invisible to the included code. • The file must return true as the last statement to indicate successful execution of any initialization code • If EXPR is a bareword, the require assumes a ".pm" extension and replaces "::" with "/" in the filename
  14. 14. Using Modules• See If you have what you need in CORE modules or get the module you need using ppm berov@berov:~> ppm
  15. 15. Using Modules• Example use strict; use warnings; use utf8; use FindBin; BEGIN { $ENV{APP_ROOT} = $FindBin::Bin ./..; } use lib($ENV{APP_ROOT}./lib); use Data::Table;#patched... See TODO in module use Data::Dumper; ...
  16. 16. Packages, modules, classes and objects• Ressources • Beginning Perl (Chapter 11 – Object-Oriented Perl) • perlboot - Beginners Object-Oriented Tutorial • perlobj - Perl objects • perltoot - Toms object-oriented tutorial for perl • perltooc - Toms OO Tutorial for Class Data in Perl • perlbot - Bago Object Tricks (the BOT)
  17. 17. Packages, modules, classesand objectsQuestions?