Modules
The
packagekeyword starts a new namespace (which lasts until another package declaration or until end of block). All user-defined global identifiers (variables, subroutines, filehandles) belong to this package. Lexical variables do not belong to any package.package Employee; # Put in file Employee.pm @employees = ("John", "Fred", "Mary", "Sue"); sub list_employee { print @employees; } 1; # Last executing statement in file must be # non-zero, to indicate successful loadingTo load module Employee:
use Employee; #or require Employee;
Specify the load path with the
-Icommand-line option, PERL5LIB environment variable, or@INC.Access foreign package’s variables and subroutines with fully qualified names:
print @Employee::employees; Employee::print();
Privacy is not enforced.
If a subroutine is not found in that package, a default subroutine
AUTOLOAD()is called, if present.$AUTOLOADis set to the fully qualified name of the missing subroutine.To inherit module C from modules A and B, prime C’s
@ISAarray with the names of its superclass modules:package A; sub foo{ print "A::foo called \n";} package C; @ISA = ("A", "B"); C->foo(); # Calls A::foo, because B does not
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access