1 writing perl 5 modules: an introduction keith arner
Post on 22-Dec-2015
218 views
TRANSCRIPT
![Page 1: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/1.jpg)
1
Writing Perl 5 Modules:An Introduction
Keith Arner
![Page 2: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/2.jpg)
2
Outline
• What do we mean by Modules and Classes?
• Module Basics– Using Modules, Packages, Special Variables,
Exporting Functions
• Advanced Concepts– Autoloading, More Exporting, Versioning
• Ask questions at any time
![Page 3: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/3.jpg)
3
Topics Not Covered
• Objects
• Modules available on CPAN
• Writing modules for CPAN distribution
• Each of these would make a good topic for a future talk (Any volunteers?)
![Page 4: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/4.jpg)
4
Modules and Classes
• There are two types of modularization under Perl 5– Modules : library functions– Classes : object-oriented programming
![Page 5: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/5.jpg)
5
What is a Module?
• A module is a .pm file that defines a library of related functions
• Modules are conceptually similar to old-fashioned Perl libraries (.pl files), but have a cleaner implementation– selective namespace cluttering– simpler function invocation
![Page 6: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/6.jpg)
6
How to use a Module Time::Local
• use Time::Local;
$time = timelocal($sec, $min, $hours, $mday, $mon,
$year);
print "Seconds since epoch: $time\n";
![Page 7: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/7.jpg)
7
Module Basics
• Learn by Example: MyRand.pm• Refer to attached printout of MyRand.pm• Note that line numbers are for reference
purposes only
![Page 8: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/8.jpg)
8
Package Names and Filenames
• Package name is declared on line 1
• This should be the same as the filename, without the .pm extension
• If it is different, your functions will not be exported correctly
• Should begin with a capital letter to avoid possible conflict with pragmas
![Page 9: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/9.jpg)
9
More on Package Names
• package MyRandis file MyRand.pm
• package Timeis file Time.pm
• package Time::Localis file Time/Local.pm
• They have nothing to do with one another
![Page 10: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/10.jpg)
10
The Package: Your Own Private Namespace
• Each package is a separate namespace for functions and variables
• There is no collision of names between namespaces
• $seed in the package main and $seed in the package MyRand are distinct variables
![Page 11: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/11.jpg)
11
Magic Variables
• @EXPORT, @ISA and $VERSION have special meanings
• @EXPORT tells Perl what functions you want to make available to the public
• @ISA tells Perl how to export the functions (I won't go into the details)
• $VERSION is used to indicate compatibility
![Page 12: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/12.jpg)
12
Exporting Functions
• Line 11 sets @EXPORT to contain a list of the names of functions to be exported
• Note that we use qw() to generate the list– This is just syntactic sugar for:@EXPORT = ('srand', 'rand');
![Page 13: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/13.jpg)
13
What does it mean to Export?
• Your functions are defined in your own package
• Exporting makes the functions available in another package as if they were defined in the other package
• This is done by fiddling with symbol tables (which is a topic for another talk)
![Page 14: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/14.jpg)
14
A Function is a Functionis a Function
• On lines 19 and 23, functions are declared and written exactly as they would be anywhere else
![Page 15: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/15.jpg)
15
Module Specific State
• Line 15 declares a variable local to the module that can maintain it's state between function calls
• $seed is visible to all functions in this file• $seed does not conflict with $seed in
any other module, nor in a mainline program
![Page 16: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/16.jpg)
16
Declaring Victory
• All Perl modules must return a true value to indicate that they completed successfully
• Line 35 shows the canonical return• Any true value will do:
– 'true'– 3.1415926535– $^O & $/– (3..99)
![Page 17: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/17.jpg)
17
How do you use it?
• Make sure your module is in your include path, @INC
• This can be augmented with -I or your PERL5LIB environment variable
• use MyRand;srand(5);print rand;
![Page 18: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/18.jpg)
18
Advanced Topics
• Autoloading
• Selective Exporting
• Versioning
• Much, much more
![Page 19: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/19.jpg)
19
Autoloading
• The autoloader is used to improve performance of large modules (such as POSIX.pm)
• Functions are split into separate files and compiled only as needed
• AutoSplit.pm is used to split modules
![Page 20: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/20.jpg)
20
More Exporting
• @EXPORT can export variables in addition to functions:– @EXPORT = qw(rand srand $seed)
• @EXPORT_OK and %EXPORT_TAGS can be used to export selectively
![Page 21: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/21.jpg)
21
Selective Exporting
• It is not always desirable to export everything, so @EXPORT_OK is used
• @EXPORT = qw (rand srand);@EXPORT_OK = qw ($seed);
• use MyRand qw(rand $seed);
![Page 22: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/22.jpg)
22
Fancy Exporting
• Typing all the names of symbols to import can become a hassle, so %EXPORT_TAGS is used
• @EXPORT = qw (rand srand);@EXPORT_OK = qw ($seed);%EXPORT_TAGS = (
funcs => [qw(rand srand)],vars => [qw($seed)] );
• use MyRand qw(:funcs :vars);
![Page 23: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/23.jpg)
23
Versioning
• $VERSION is used to indicate the version of the module
• use MyRand 2.0;– will fail if $MyRand::VERSION < 2.0
• Can be used with RCS Keywords:– $VERSION = ('$REVISION 1.0$' =~
m/Revision ([^\$]+)/);
![Page 24: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/24.jpg)
24
Much, much more
• Overriding builtin functions
• Cascading inheritance with @ISA• Automated Cleanup
• Object Oriented Programming
![Page 25: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/25.jpg)
25
References• Programming Perl, Second Edition.
Wall, Christiansen & Schwartz– Chapters 5 and 7
• Man pages– perlmod(1)
• Perl Cookbook. Christiansen & Torkington– Chapter 12
![Page 26: 1 Writing Perl 5 Modules: An Introduction Keith Arner](https://reader036.vdocument.in/reader036/viewer/2022062516/56649d805503460f94a64606/html5/thumbnails/26.jpg)
26
Summary
• Modules are libraries of functions
• A simple module just exports a set of functions
• Perl modules can be expanded in many directions for arbitrarily sophisticated libraries