|
|
Intermediate Perl - Day 2
Goals
In this session you will:
- use packages to control namespace
- encapsulate packages in Modules
- export variables from packages
POST
- log into the machine and go to your
~/[initials] directory.
- start a new script called
day1post.pl. Comment it with the author's name, date, contact info, etc.
- define a hash that contains a list of your animals. The key will be the type of animal (cat, frog, squid or whatever) and the value will be the animals name. Define at least three animals in this hash.
- write a function receives the hash by value, prints out "passed by value", then prints out the key/value pairs.
- write a function receives the hash by reference, prints out "passed by reference", then prints out the key/value pairs.
- what is the essential difference between these two functions?
- Use the debugger to run through the script twice, once using step-into (
s) and once using step-over (n). What is the difference? If you're feeling adventurous, set a watch on a variable and see how it changes.
packages
- packages are the perl way to delimit namespace (not scope per se)
- You have already been using a package called
main::. If you have a variable called $counter it is really $main::counter.
- namespace package can change within the course of one file, but usually doesn't.
- a package is usually in effect for an entire sourcefile.
- a package is self-contained. You can only access variables (properties?) and functions (methods?) by:
exporting the variables from the package to your current (main::?) package. (preferred)
- calling the variable explicitly like
$packagename::varname (generally frowned upon)
Modules
- a module is the basic unit of perl re-usability
- the module is a stand-alone file whose package is the same name as the filename (see the example below)
- the module is
MyPackage.pm
- the package is
MyPackage
- you will usually
use the module unless the documentation says otherwise.
- perl looks in @INC array for module directories; you may have to extend this array (with
use lib to get perl to see your module.
A simple example
| callModule.pl |
MyModule.pl |
#!/path/to/perl
# author, contact, date info
# may have to add current dir to @INC
use lib '/path/to/module/dir';
# leave off the .pm
use MyModule;
print "\$name is $name\n";
|
# MyModule.pm
# first time module
# administrative overhead
require Exporter;
@ISA = qw/Exporter/;
@EXPORT = qw/$name/;
$name = "Jabba the Hut";
# returns positive value
1;
|
more complex exporting
use SomeModule; default exports
use SomeModule(); NO exports
use SomeModule qw($somevar); optional exports
@EXPORT_OK = qw/$somevar somefunction/;
use SomeModule qw(:DEFAULT :textstuff); using tags
%EXPORT_TAGS = (textstuff => [ qw/$var $var2/ ], tag2 => [ qw/$var2 $var3/ ]);
practicum
- start two new files,
firstModule.pl and FirstModule.pm. The .pl file will be executed. The .pm file is the module and will be used.
- name your package in the the module.
- declare a variable like
$currentAge. Populate it with your current age (surprise!)
use your module from the perl script.
- Attempt to access the
$currentAge variable.
- Attempt to access the variable with it's full name ($package::var).
- After you get the previous step to work, comment out that line. It is rude to reach into a module and grab a variable that way!
- Now export the variable (by default via the @EXPORT array) and try to access it again.
- add two more variables to your module. Export one via the @EXPORT_OK array (remember to ask for it by name when you use the module. Try to access all three. What happens?
Homework
http://www.mousetrap.net/syllabus/interperl/day2.html
$Id: day2.orb,v 1.7 2002/08/18 05:06:45 mouse Exp $
Remember, your login is based on your machine's hostname, not on any other number.
~/[initials] refers to the subdirectory under your homedir, named after your initials. Everything except for .dotfiles will be stored in your ~/[initials] directory.
|
© 1995-2001 jason carr
Distributed under the terms of the GNU Free Documentation License.
|
|