Perl provides a method for breaking up your program's namespace into chunks called "packages".
Each package gets it's own namespace, it's own symbol table. Package a's variable $x is distinct from package b's variable $x.
In a simple one-file, no-package script, everything is contained in package "main".
All the "magic" variables stay in package main, only identifiers defined by your program are package-specific.
You can define more than one package per file, or use more than one file to define a single package, but more often than not the one-file-per-package rule will work and keep life simpler.
Use C++ style scope operator to access variables in packages:
print $x; # prints $x from current package print $main::x; # prints $x from "main" package print $foo::x; # prints $x from "foo" package foo::bar(); # call bar subroutine in package foo ::bar(); # call bar subroutine from package main
A Perl package bundled into a file named with the package name and having a ".pm" extension is a module.
Typically name module/package files with capitolized word or acronym: Exporter, CGI, HTML, etc.
Package simply used to break up namespace of program. Module used to organize code for importing via "use" or to setup a class for OO programming.
"use" imports code from a module at compile time, "require" at run time. There are more details, but for now, just use "use".
Perl looks in directories specified in the @INC array (created when Perl was compiled) for where to find packages when told to "require" or "use" them. Can add things to it, typically inside of BEGIN, must be if doing a "use" at compile time.
unshift(@INC, "../myperllib/"); # add my personal directory
"domath" script:
#!/usr/bin/perl -w use mymath; # use the mymath module $pi = 7.3; print "my pi = $pi\n"; print "mymath's pi = $mymath::pi\n"; print "area of circle with radius $pi = ", mymath::area($pi), "\n";
the "mymath.pm" file:
package mymath;
$pi = 3.14159;
sub area {
my($radius) = shift;
return($pi * $radius * $radius);
}
1; # return true for require or use
Modified to use the Exporter module for easier naming, just a few steps removed from making a package a Class.
Note this jumps ahead and does some OO stuff.
#!/usr/bin/perl -w
use mymath2 ('area'); # import a specific name as well
$pi = 7.3;
print "my pi = $pi\n";
print "mymath's pi = $mymath2::pi\n";
print "area of circle with radius $pi = ", area($pi), "\n";
The Module in mymath2.pm:
package mymath2;
use Exporter; # setup exporting
@ISA = ('Exporter');
@EXPORT_OK = ('pi', 'area');
$pi = 3.14159;
sub area {
my($radius) = shift;
return($pi * $radius * $radius);
}
1; # return true for require or use
Scripts/Packages can have special functions similar to object contructor/destructors called BEGIN and END. They are dealt with at compile time when script is being first read in. sub BEGIN is called as soon as it is read during parsing of the package. END is called when the package goes out of scope or die's or whatever causes it to exit.
The "sub" keyword is optional on BEGIN and END and is rarely used.
#!/usr/bin/perl -w
die "two\n"; # second, pkg code executing
END {
print "three\n"; # third, pkg exiting
}
sub BEGIN {
print "one\n"; # first, pkg init
}
Useful to define path to packages/libraries:
BEGIN {
unshift(@INC, "/path/to/my/lib/");
}
use mylib; # now we can see it
Quite a few modules distributed with the Perl distro, built and installed during installation. Popular CPAN modules might, over time, be added as well. Seems to be a few more with each revision of Perl.
"perldoc Module" to get module docs on installed module (such as Getopt::Std and others).
diagnostics - go ahead and print out lots of extra info about warnings (related to the splain script that comes with Perl). Faster than finding it yourself in "man perldiag".
#!/usr/local/bin/perl -w @a = (1, 2, 3); print "value of first elmt of a is: ", @a[0], "\n"; output: Scalar value @a[0] better written as $a[0] at ./diag line 7. value of first elmt of a is: 1Modified:
#!/usr/local/bin/perl -w
use diagnostics;
@a = (1, 2, 3);
print "value of first elmt of a is: ", @a[0], "\n";
output:
Scalar value @a[0] better written as $a[0] at ./diag line 7 (#1)
(W) You've used an array slice (indicated by @) to select a single element of
an array. Generally it's better to ask for a scalar value (indicated by $).
The difference is that $foo[&bar] always behaves like a scalar, both when
assigning to it and when evaluating its argument, while @foo[&bar] behaves
like a list when you assign to it, and provides a list context to its
subscript, which can do weird things if you're expecting only one subscript.
On the other hand, if you were actually hoping to treat the array
element as a list, you need to look into how references work, because
Perl will not magically convert between scalars and lists for you. See
perlref.
value of first elmt of a is: 1
File::Copy - Copy or move a file, using simple functions, and works over different types of systems.
use File::Copy;
copy("file1", "file1" . $suffix);
Others:
http://www.cpan.org/ is the home of the CPAN: Comprehensive Perl Archive Network.
Repository of Perl-related documents, modules, source, etc. Especially user-contributed modules for doing all kinds of interesting things.
Various mirrors accessed automatically through the CPAN Multiplexor, www.perl.com will redirect most requests for CPAN to other mirrors based on what it perceives your location to be. You can also manually select from the mirrors.