Upload
others
View
11
Download
0
Embed Size (px)
Citation preview
Advanced Perl
Programming
Student Workbook
Page ii Rev 3.1.1 © 2008 ITCourseware, LLC
Advanced Perl Programming
Advanced Perl Programming
Published by ITCourseware, LLC., 7245 South Havana Street, Suite 100, Centennial, CO 80112
Contributing Authors: Brandon Caldwell, Jeff Howell, Jim McNally, Rob Roselius
Editor: Rob Roselius
Special thanks to: Many instructors whose ideas and careful review have contributed to the quality of this
workbook, including Michael Naseef and Richard Raab, and the many students who have offered
comments, suggestions, criticisms, and insights.
Copyright © 2008 by ITCourseware, LLC. All rights reserved. No part of this book may be reproduced
or utilized in any form or by any means, electronic or mechanical, including photo-copying, recording, or by
an information storage retrieval system, without permission in writing from the publisher. Inquiries should be
addressed to ITCourseware, LLC., 7245 South Havana Street, Suite 100, Centennial, Colorado, 80112.
(303) 302-5280.
All brand names, product names, trademarks, and registered trademarks are the property of their respective
owners.
© 2008 ITCourseware, LLC Rev 3.1.1 Page iii
Advanced Perl Programming
Contents
Chapter 1 - Course Introduction ............................................................................................................. 9
Course Objectives .......................................................................................................................... 10
Course Overview ........................................................................................................................... 12
Using the Workbook ...................................................................................................................... 13
Suggested References ..................................................................................................................... 14
Chapter 2 - Debugging ......................................................................................................................... 17
Warnings ........................................................................................................................................ 18
Diagnostic Messages ...................................................................................................................... 20
Carping, Confessing, and Croaking ................................................................................................. 22
Strict Checks .................................................................................................................................. 24
Compiler Pragmas .......................................................................................................................... 26
Debugging Flags ............................................................................................................................. 28
Your Perl Configuration ................................................................................................................... 30
The Devel::Peek Module ................................................................................................................. 32
The Data::Dumper Module .............................................................................................................. 34
Labs ............................................................................................................................................... 36
Chapter 3 - Expert List Manipulation .................................................................................................... 39
The grep Operator .......................................................................................................................... 40
Lists, Arrays, and List Operators ..................................................................................................... 42
Context .......................................................................................................................................... 44
Context and Subroutines ................................................................................................................. 46
Initializing Arrays and Hashes .......................................................................................................... 48
Reference Syntax ............................................................................................................................ 50
Auto-vivification ............................................................................................................................. 52
Defined Values ................................................................................................................................ 54
Other List Operators ...................................................................................................................... 56
Usage of map, grep, and foreach ..................................................................................................... 58
Labs ............................................................................................................................................... 60
Page iv Rev 3.1.1 © 2008 ITCourseware, LLC
Advanced Perl Programming
Chapter 4 - Blocks and Code References ............................................................................................. 63
Blocks ............................................................................................................................................ 64
Subroutines .................................................................................................................................... 66
Subroutine Prototypes .................................................................................................................... 68
Code Refs and Anonymous Subroutines .......................................................................................... 70
Typeglobbing for the Non-Squeamish .............................................................................................. 72
Local (Dynamic) Variables .............................................................................................................. 74
Lexical Variables ............................................................................................................................. 76
Persistent Private Subroutine Variables ............................................................................................ 78
Closures ......................................................................................................................................... 80
The eval Operator .......................................................................................................................... 82
The Block Form of eval .................................................................................................................. 84
The String Form of eval .................................................................................................................. 86
Block Form of eval for Exception Handling ..................................................................................... 88
Labs ............................................................................................................................................... 90
Chapter 5 - Packages ........................................................................................................................... 93
Review of Packages ....................................................................................................................... 94
BEGIN and END Blocks ............................................................................................................... 96
Symbol Tables ................................................................................................................................ 98
Package Variables ........................................................................................................................ 100
Calling Package Subroutines ......................................................................................................... 102
Importing Package Symbols .......................................................................................................... 104
Exporting Package Symbols .......................................................................................................... 106
Using the Exporter Package .......................................................................................................... 108
The use Function .......................................................................................................................... 110
AUTOLOAD and @ISA ............................................................................................................. 112
AutoLoader and SelfLoader ......................................................................................................... 114
Labs ............................................................................................................................................. 116
Chapter 6 - Objects and Classes ........................................................................................................ 119
Object-Oriented Stuff ................................................................................................................... 120
Making Perl Object-Oriented ....................................................................................................... 122
References ................................................................................................................................... 124
The bless Function ........................................................................................................................ 126
So, What's a Blessed Thing Good For? ......................................................................................... 128
Calling Class and Object Methods ................................................................................................ 130
Object Methods ........................................................................................................................... 132
Writing Classes ............................................................................................................................. 134
© 2008 ITCourseware, LLC Rev 3.1.1 Page v
Advanced Perl Programming
Constructors ................................................................................................................................. 136
Inheritance .................................................................................................................................... 138
What Perl Doesn't Do ................................................................................................................... 140
Labs ............................................................................................................................................. 142
Chapter 7 - Tied Variables .................................................................................................................. 145
Why Use tie? ................................................................................................................................ 146
Tying a Scalar ............................................................................................................................... 148
Inside Tied Variables ..................................................................................................................... 150
untie ............................................................................................................................................. 152
Another Tied Scalar Example ........................................................................................................ 154
Tying an Array .............................................................................................................................. 156
A Tied Array Example ................................................................................................................... 158
Tying Hashes ................................................................................................................................ 160
Tie::Hash and Tie::Array ................................................................................................................ 162
Tying Filehandles .......................................................................................................................... 164
What Are DBM, NDBM, GDBM, SDBM, etc? ........................................................................... 166
Using the DBM Modules .............................................................................................................. 168
Labs ............................................................................................................................................. 170
Chapter 8 - Installing and Using Perl Modules ..................................................................................... 173
Laziness, Impatience, and Hubris .................................................................................................. 174
CPAN.......................................................................................................................................... 176
Using Modules ............................................................................................................................. 178
Installing a Perl Module ................................................................................................................. 180
Unpacking the Module Source ...................................................................................................... 182
The Configuration Step ................................................................................................................. 184
The Build Step .............................................................................................................................. 186
The Test Step ............................................................................................................................... 188
The Install Step ............................................................................................................................. 190
Using CPAN.pm .......................................................................................................................... 192
Using Module Documentation ....................................................................................................... 194
Labs ............................................................................................................................................. 196
Chapter 9 - Introduction to DBI/DBD ................................................................................................. 199
The Old Way - DBPerls ............................................................................................................... 200
A Better Way - DBI/DBD ............................................................................................................ 202
Database Programming ................................................................................................................. 204
Handles ........................................................................................................................................ 206
Page vi Rev 3.1.1 © 2008 ITCourseware, LLC
Advanced Perl Programming
Connecting to the Database .......................................................................................................... 208
Creating a SQL Query .................................................................................................................. 210
Getting the Results ........................................................................................................................ 212
Updating Database Data ............................................................................................................... 214
Transaction Management .............................................................................................................. 216
Finishing Up ................................................................................................................................. 218
Labs ............................................................................................................................................. 220
Chapter 10 - DBI/DBD SQL Programming ......................................................................................... 223
Error Checking in DBI .................................................................................................................. 224
Getting Connected ........................................................................................................................ 226
Drivers ......................................................................................................................................... 228
Using Parameterized Statements .................................................................................................... 230
Statement Handle Attributes .......................................................................................................... 232
Other Handle Attributes ................................................................................................................ 234
Column Binding ............................................................................................................................ 236
The do Method ............................................................................................................................ 238
BLOBs and LONGs and Such ..................................................................................................... 240
Installing DBI Drivers .................................................................................................................... 242
Labs ............................................................................................................................................. 244
Chapter 11 - Introduction to Perl/Tk ................................................................................................... 247
Tcl, Tk, Tcl/Tk, Tkperl, Perl/Tk, etc. ............................................................................................. 248
Perl/Tk ......................................................................................................................................... 250
Creating a Perl/Tk Application ...................................................................................................... 252
GUI Programming Overview ......................................................................................................... 254
Adding Widgets ............................................................................................................................ 256
Scrolled Widgets .......................................................................................................................... 258
Configuring Widgets ...................................................................................................................... 260
Menus .......................................................................................................................................... 262
More Fun with Menus .................................................................................................................. 264
Using FileSelect ............................................................................................................................ 266
Labs ............................................................................................................................................. 268
Chapter 12 - Perl/Tk Programming ..................................................................................................... 271
Tk::Error and Tk::ErrorDialog ....................................................................................................... 272
Configuring Widgets ...................................................................................................................... 274
Geometry Management ................................................................................................................. 276
Geometry Management with grid() ................................................................................................ 278
© 2008 ITCourseware, LLC Rev 3.1.1 Page vii
Advanced Perl Programming
The Frame Widget ........................................................................................................................ 280
Defining Widget Callbacks ............................................................................................................ 282
Bindings ....................................................................................................................................... 284
Nonblocking I/O with fileevent() ................................................................................................... 286
Tags ............................................................................................................................................. 288
Other Widgets .............................................................................................................................. 290
Other Tk Commands .................................................................................................................... 292
Getting Tk .................................................................................................................................... 294
Labs ............................................................................................................................................. 296
Chapter 13 - Extending Perl with C/C++ ............................................................................................ 299
Extending the Perl Interpreter ........................................................................................................ 300
Overview of Perl5 XSUBs ............................................................................................................ 302
Get Started with h2xs ................................................................................................................... 304
Set up the Perl Wrapper Class ...................................................................................................... 306
Write the XS Code ....................................................................................................................... 308
The XS File .................................................................................................................................. 310
Write Some Test Code ................................................................................................................. 312
What Do You Want? ..................................................................................................................... 314
Returning Values on the Stack ....................................................................................................... 316
A Walk Through an XSUB ........................................................................................................... 318
Arguments to XSUBs ................................................................................................................... 320
Other h2xs Options ...................................................................................................................... 322
Labs ............................................................................................................................................. 324
Chapter 14 - Embedding the Perl Interpreter ....................................................................................... 327
Why Embed Perl? ........................................................................................................................ 328
Embedding Perl in a C Program .................................................................................................... 330
Compiling the Program ................................................................................................................. 332
perlmain.c ..................................................................................................................................... 334
Perl Data Types ............................................................................................................................ 336
Macros and Functions .................................................................................................................. 338
Manipulating Scalars ..................................................................................................................... 340
Memory Management ................................................................................................................... 342
Script Space ................................................................................................................................. 344
Evaluating Perl Expressions ........................................................................................................... 346
Dynamic Loading .......................................................................................................................... 348
Multiple Perl Interpreters .............................................................................................................. 350
Labs ............................................................................................................................................. 352
Page viii Rev 3.1.1 © 2008 ITCourseware, LLC
Advanced Perl Programming
Chapter 15 - Module Development and Distribution ............................................................................ 355
Distributing Modules ..................................................................................................................... 356
Get Started with h2xs ................................................................................................................... 358
Files Created by h2xs ................................................................................................................... 360
The Build Library (blib) Directory ................................................................................................. 362
Unit Testing and test.pl .................................................................................................................. 364
Versions ....................................................................................................................................... 366
Using blib ..................................................................................................................................... 368
POD ............................................................................................................................................ 370
POD Translators ........................................................................................................................... 372
Cutting a Distribution .................................................................................................................... 374
Other Niceties .............................................................................................................................. 376
Makefile.PL ................................................................................................................................. 378
Labs ............................................................................................................................................. 380
Chapter 16 - Design and Implementation ............................................................................................. 383
Think First .................................................................................................................................... 384
Object-Oriented Design ................................................................................................................ 386
Object-Oriented Development ...................................................................................................... 388
Library Modules ........................................................................................................................... 390
Utility Programs ............................................................................................................................ 392
Filters ........................................................................................................................................... 394
Performance ................................................................................................................................. 396
Timing with Benchmark ................................................................................................................. 398
Labs ............................................................................................................................................. 400
Solutions ............................................................................................................................................ 403
Index .................................................................................................................................................. 453
Course Introduction
© 2008 ITCourseware, LLC Rev 3.1.1 Page 9
Chapter 1
Chapter 1 - Course Introduction
Advanced Perl Programming
Page 10 Rev 3.1.1 © 2008 ITCourseware, LLC
� Expertly manipulate lists, arrays, and hashes.
� Exploit code references and closures.
� Use eval to run dynamically generated Perl code, and to trap exceptions.
� Create and use object-oriented Perl modules.
� Tie Perl variables to subroutines to customize access and assignment.
� Access UNIX DBM files efficiently from Perl.
� Find and effectively use the thousands of freely available Perl modules.
� Access database management systems from Perl programs using DBI.
� Write GUI application components quickly using Perl/Tk.
� Extend Perl with modules that load C/C++ object code.
� Interface Perl with, and embed Perl in, C/C++ applications.
� Describe the internal representation of Perl's various datatypes and data
structures.
� Write reusable Perl modules.
� Avoid coding practices that hurt performance and maintenance.
Course Objectives
Course Introduction
© 2008 ITCourseware, LLC Rev 3.1.1 Page 11
Chapter 1
In this course, it is assumed that you have:
1. Completed training in Perl Programming.
2. Programmed extensively in Perl for at least several months.
Or, if you have not attended Perl Programming training, you have programmed extensively in Perl for at least
a year or two. This is a wide-ranging course which addresses topics including Perl internals, coding
techniques, use of various Perl modules, and interfacing Perl with C and C++. You will write many programs
in this class, large and small.
Some aspects of Perl that you should already be comfortable with include:
� Language syntax fundamentals, including:
- Commonly used special variables and shortcuts
- Perl datatypes: scalars, lists, hashes
- Operators and expressions
- Quoting and interpolation
� References - these are especially important
� Contexts - list, scalar, string, numeric
� Perl regular expressions
� Anonymous arrays and anonymous hashes
� Input and output
� Packages, use, and require
� CPAN
� Very basic C-language features
� User-level or better UNIX skills
Every programmer takes a unique career path, encounters a unique sequence of programming situations,
and has unique strengths and weaknesses; it's possible that you are less than familiar with one or two of
these topics (which is by no means exhaustive). Don't worry, you'll still have a great experience in this class,
and maybe fill some gaps in your Perl repertoire beyond the contents of this workbook! And it's also
possible that you're more than familiar with the material in some chapters. Stay tuned-in though, and don't
be surprised if you learn something in an area you thought you knew inside-out.
If more than a few of the topics mentioned above are new to you, you might want to have a chat with the
instructor about what you can expect to accomplish in this class — which could still be a great deal; we just
want to be sure you don't get frustrated, you learn a lot, and you have fun!
Advanced Perl Programming
Page 12 Rev 3.1.1 © 2008 ITCourseware, LLC
� Audience: Application programmers, system administrators, website authors,
webmasters, and UNIX/NT power users.
� Prerequisites: Perl Programming on Unix and at least several months real
experience programming in Perl. Comprehension of the extending,
embedding, and internals material will require some C or C++ programming
experience.
� Classroom Environment:
� UNIX X-Windows workstation per student.
� Perl5.004+ installed.
� C compilation system.
� DBMS with supporting Perl DBI/DBD modules.
� Perl Tk[48]??.??? module installed and tested.
� Additional modules: Devel::Peek, Data::Dumper.
Course Overview
Course Introduction
© 2008 ITCourseware, LLC Rev 3.1.1 Page 13
Chapter 1
Using the Workbook
Chapter 2 Servlet Basics
© 2002 ITCourseware, LLC Rev 2.0.0 Page 17
Add an init() method to your Today servlet that initializes a bornOn date, then print the bornOn date
along with the current date:
Today.java
...
public class Today extends GenericServlet {
private Date bornOn;
public void service(ServletRequest request,
ServletResponse response) throws ServletException, IOException
{
...
// Write the document
out.println("This servlet was born on " + bornOn.toString());
out.println("It is now " + today.toString());
}
public void init() {
bornOn = new Date();
}
}
Hands On:
The init() method is
called when the servlet is
loaded into the container.
This workbook design is based on a page-pair, consisting of a Topic page and a Support page. When you
lay the workbook open flat, the Topic page is on the left and the Support page is on the right. The Topic
page contains the points to be discussed in class. The Support page has code examples, diagrams, screen
shots and additional information. Hands On sections provide opportunities for practical application of key
concepts. Try It and Investigate sections help direct individual discovery.
In addition, there is an index for quick lookup. Printed lab solutions are in the back of the book as well as
online if you need a little help.
Java Servlets
Page 16 Rev 2.0.0 © 2002 ITCourseware, LLC
� The servlet container controls the life cycle of the servlet.
� When the first request is received, the container loads the servlet class
and calls the init() method.
� For every request, the container uses a separate thread to call
the service() method.
� When the servlet is unloaded, the container calls the destroy()
method.
� As with Java’s finalize() method, don’t count on this being
called.
� Override one of the init() methods for one-time initializations, instead of
using a constructor.
� The simplest form takes no parameters.
public void init() {...}
� If you need to know container-specific configuration information, use
the other version.
public void init(ServletConfig config) {...
� Whenever you use the ServletConfig approach, always call the
superclass method, which performs additional initializations.
super.init(config);
The Servlet Life Cycle
The Topic page provides
the main topics for
classroom discussion.
The Support page has
additional information,
examples, and suggestions.
Code examples are in a
fixed font and shaded. The
online file name is listed
above the shaded area.
Screen shots show
examples of what you
should see in class.
Topics are organized into
first (�), second (�), and
third (�) level points.
Pages are numbered
sequentially throughout
the book, making lookup
easy.
Callout boxes point out
important parts of the
example code.
Advanced Perl Programming
Page 14 Rev 3.1.1 © 2008 ITCourseware, LLC
Christiansen, Tom and Nathan Torkington. 2003. Perl Cookbook, Second Edition. O'Reilly &
Associates, Sebastopol, CA. ISBN 0596003137.
Friedl, Jeffrey E. F. 2006. Mastering Regular Expressions, Third Edition. O'Reilly & Associates,
Sebastopol, CA. ISBN 0596528124.
Hall, Joseph and Randal Schwartz. 1998. Effective Perl Programming. Addison-Wesley, Reading, MA.
ISBN 0201419750.
Cozens, Simon. 2005. Advanced Perl Programming. O'Reilly & Associates, Sebastopol, CA.
ISBN 0596004567.
Wall, Larry, Tom Christiansen, and Jon Orwant. 2000. Programming Perl. Third Edition. O'Reilly &
Associates, Sebastopol, CA. ISBN 0596000278.
http://www.perl.com
http://www.perl.org
news:comp.lang.perl.misc
news:comp.lang.perl.modules
news:comp.lang.perl.tk
news:comp.lang.perl.announce
news:comp.lang.perl.moderated
Suggested References
Course Introduction
© 2008 ITCourseware, LLC Rev 3.1.1 Page 15
Chapter 1
Perl, the archetype for free/open-source software, was supported for years by its original author Larry Wall.
Time was, you'd send an email to Larry, and he'd have a fix or enhancement in the Perl source code
distribution in weeks, days, or overnight. Larry has long since ceded the dirty work of maintaining the Perl
source code to other dedicated hackers and is unlikely to answer your Perl questions by email (though he
might, you never know).
Perl is one of the best documented languages available. Quite apart from the several fine books available,
every Perl installation comes with complete, up-to-date documentation including almost 100 pages of FAQs
(nearly 300 answers to common questions), and over 600 pages of technical reference and tutorials. Find
Perl's Plain Old Documentation (POD) in /usr/local/lib/perl5/pod/.
For many years businesses were reluctant to adopt Perl as a production development and deployment tool
due to concerns of "Where's the support?" and, perhaps, "Who's liable (who do we sue)?" Perlers knew all
along, of course, that Perl's support is higher quality and more responsive than that for just about any other
software you could name. Perl's support department is the Internet, available 24/7, staffed by tens of
thousands of experienced coders.
For a long while, the USENET newsgroup comp.lang.perl (clp to its subscribers) was the point of
discussion. This group has been superseded by subgroups of comp.lang.perl newsgroup hierarchy
(comp.lang.perl.misc or clpm, primarily) as the readership grew and specialized. While some news
servers haven't fully removed the original clp group, you should start at comp.lang.perl.misc these days.
Woe unto you if you post without first checking the FAQ.
For those actively working to keep the latest Perl releases compiling on its many supported platforms,
there's the perl5-porters mailing list. Don't pester these folks with basic questions, though; this list is for the
busy, dedicated folks keeping Perl going for the rest of us.
As the Web emerged, Tom Christiansen started the Perl home site, www.perl.com. This site includes links
to archives of the Perl source code, as well as modules, links, and other resources. He maintained this
extremely popular and busy site for years over a 28,800 modem (using clever Perl networking techniques,
of course) until O'Reilly & Associates offered to sponsor the site. Give it a visit.
While there are now many consultants and small companies providing Perl support services, and
commercial journals are in publication (see archives of The Perl Journal, at www.tpj.com), the Internet is
still the official Perl support department, and users (programmers like us) still get to exercise our
resourcefulness and curiosity as we pursue solutions and understanding. Happy browsing!
Advanced Perl Programming
Page 16 Rev 3.1.1 © 2008 ITCourseware, LLC
DebuggingChapter 2
© 2008 ITCourseware, LLC Rev 3.1.1 Page 17
Chapter 2 - Debugging
Objectives
� Enable, and temporarily disable,
warnings in various scopes.
� Get verbose diagnostics for
debugging.
� Generate appropriate warnings and
exits in your own code.
� Enable strict compiler checking of
variables, references, and barewords.
� Temporarily disable strict checking.
� View data structures, internal values,
and stack traces.
Advanced Perl Programming
Page 18 Rev 3.1.1 © 2008 ITCourseware, LLC
� perl -w prints wisdom that you might otherwise miss, such as:
� if ($x = $y)... instead of if ($x == $y)...
� Non-numeric scalars in a numeric expressions.
� Variables that are only used once (helps catch typos).
� Uninitialized variables.
� Subroutines that recurse more than 100 times.
� And many more, both compile-time and runtime errors.
� -w sets the global $^W variable (that's either $<Ctrl>W or $^W (carat-W))
— you can change, and even localize, $^W at runtime.
$^W = 1; # Turn on warnings
{ ## I want to do some dangerous stuff here...
local $^W = 0; # Set to 0 until end of block
� Set $SIG{__WARN__} to trap warnings and call your own function.
$SIG{__WARN__} = \&callbackFunction;
� Any time a warning is generated, or warn() is used, your callback
function will be invoked.
� The warning message is sent to the callback as its first parameter.
� Do this in a BEGIN block to catch compiler warnings.
Warnings
DebuggingChapter 2
© 2008 ITCourseware, LLC Rev 3.1.1 Page 19
Hands On:
Run this sample program and examine the output. Make sure you understand the reason for every warning:
warntest.plprint "\$^W is $^W\n";
while ($l = <STDIN>) {1;}
#BEGIN {
# $SIG{__WARN__} = \&myWarning;
#}
print "x is '$x'\n";
&scopeIt();
print "z is '$z'\n";
warn "Warning: You should rotate your tires periodically.\n";
sub scopeIt {
local $^W = 0;
print "y is '$y'\n";
}
sub myWarning {
print "Got a warning that starts: '",
substr($_[0],0,40), "...'\n";
}
Now, uncomment just the $SIG{__WARN__} line, and run it again. Which messages are compile-time
warnings and which are runtime warnings? Why?
Finally, uncomment the BEGIN block around the $sig{__WARN__} line and run it again.
Advanced Perl Programming
Page 20 Rev 3.1.1 © 2008 ITCourseware, LLC
� For verbose descriptions of warnings, use diagnostics;.
� This automatically enables the -w flag.
� For each warning, you'll get an additional, descriptive paragraph.
� You may enable or disable these diagnostic messages during runtime by
using diagnostics::enable() and diagnostics::disable().
� The -verbose option prints the perldiag man page introduction, which
describes the various levels of warnings, before any other diagnostics.
� To get diagnostic output without modifying your code, you can use
-Mdiagnostics.
perl -Mdiagnostics prog.pl
perl -M'diagnostics -verbose' prog.pl
� Strive to get your code running cleanly under -w before putting it into
production.
Diagnostic Messages
DebuggingChapter 2
© 2008 ITCourseware, LLC Rev 3.1.1 Page 21
Hands On:
Add a use diagnostics statement to the program from the previous page and run it.
warntest.plprint "\$^W is $^W\n";
use diagnostics;
...
Change it to use diagnostics -verbose and run it again. Peruse the output.
The question of whether to use -w, and other diagnostics-producing options, in the production runtime
environment is the subject of passionate debate (so don't bring it up in comp.lang.perl.misc unless you're
wearing your asbestos suit). Certainly it can cause failure and explosive logfile growth for "trivial" reasons in
CGI scripts, for example (some argue that there are no trivial warnings, of course). New releases of Perl
may add disconcerting warning messages that older scripts didn't count on; it may not be practical to retest
every script before upgrading your Perl. You can, of course, use a $SIG{__WARN__} handler to insulate
end users from warnings, and quietly log (or transmit to you) the diagnostic information. On this somewhat
religious issue, you must consult your own conscience and experience.
Some warnings are themselves considered bugs. Perl5.004 introduced a new warning when you use
constructs like:while ($line = <FH>) { ...
This produces the warning:Value of <HANDLE> construct can be "0"; test with defined()...
(If the last line of input is a 0 with no trailing newline, $line will be false before the true EOF.) This breaks a
great deal of perfectly good existing code and the warning is removed in Perl5.005.
Advanced Perl Programming
Page 22 Rev 3.1.1 © 2008 ITCourseware, LLC
� You probably use warn to print your own warnings to STDERR.
� If you don't provide a message, warn issues a default warning, indicating
something wrong at the line it was invoked.
� If you provide a message to warn and don't put a new line at the end of
it, the line number and current file name are added.
� die behaves just like warn, but exits after printing.
� warn and die report the line number and filename of the file in which they
occur; if that file was included with require or use, it reports the included file,
not the file that included it.
� The carp module provides alternative error routines.
� confess behaves just like die, but prints a stack trace before exiting.
� Your function can carp a message and the code that invokes your function will
have its filename and line number shown.
� carp behaves like warn, but with the caller's file and line number
reported.
carp "function expects filename, using STDIN";
� croak behaves like die, but with the caller's information reported.
croak "function requires filename";
� In module code, carp and croak are preferable to warn and die.
Carping, Confessing, and Croaking
DebuggingChapter 2
© 2008 ITCourseware, LLC Rev 3.1.1 Page 23
Hands On:
Run the carptest.pl program. Note the file and line numbers reported by the warning and error messages.
Now, uncomment the lines in myTest.pm and run the carptest.pl program again. (Of course, only the first
of confess or croak will work, so uncomment them individually.)
mytest.pmpackage myTest;
#use Carp;
sub printIt {
print "myTest::printIt -> $_[0]\n";
warn "This is a warning from myTest::printIt";
die "die called from myTest::printIt";
#carp "This is a carp message generated by myTest::printIt";
#confess"confess called from myTest::printIt";
#croak "croak called from myTest::printIt";
}
1;
carptest.pluse myTest; #using the module
myTest::printIt("Hi MOM!");
Advanced Perl Programming
Page 24 Rev 3.1.1 © 2008 ITCourseware, LLC
� By default, the use strict; pragma tells the compiler to be strict about three
kinds of usage:
� Variables that are not declared, fully qualified, lexical, or imported.
� References that are symbolic.
� Subroutines that are not declared, or barewords that aren't hash keys or
used with the => operator.
� You may provide an argument that will turn on strict checking for only that
usage:
use strict 'vars';
use strict 'refs';
use strict 'subs';
� You can scope the checking to any block or use it for the entire program.
� You will find that using strict checks from the beginning of your development
effort will greatly reduce the number of debugging headaches you might
otherwise encounter.
� It is difficult to retrofit a program with strict checking, so use it from the
start.
� Strive to get your code running cleanly under use strict before putting it into
production.
Strict Checks
DebuggingChapter 2
© 2008 ITCourseware, LLC Rev 3.1.1 Page 25
Hands On:
In the following code, uncomment each use strict line one at a time and run the program.
stricttest.pl#use strict;
&strictVars();
&strictRefs();
&strictSubs();
sub strictVars {
#use strict 'vars';
@a = (6);
print "strictVars: \@a is @a\n"; # Undeclared package var
}
sub strictRefs {
#use strict 'refs';
@names = ('Fido', 'Spot', 'Kiki');
my $symRef = "names";
print "strictRefs: \@\$symRef is @$symRef\n"; # Symref @'names'
}
sub strictSubs {
#use strict 'subs';
my $name = Francis; # Bareword Francis used for quoted string
print "strictSubs: \$name is $name\n";
}
Advanced Perl Programming
Page 26 Rev 3.1.1 © 2008 ITCourseware, LLC
� Using other compiler pragmas will help you write more bulletproof code.
� When performing strict checks on 'vars,' you can do a pseudo-import on
some symbols, which will allow you to use them without fully qualifying
them.
use strict 'vars';
use vars qw($grandtotal @employee);
� You can override built-in functions or call functions without enclosing
their parameter arguments in parentheses.
use subs qw(maxCostSub createEmployee);
� You can use your own Perl modules or those that you've downloaded from
CPAN by using the appropriate library:
use lib "/home/me/my_perl";
� This is preferred over modifying the @INC array in your programs to
include your module paths.
� To turn off a pragma, use no:
no strict 'refs';
no vars;
no integer;
Compiler Pragmas
DebuggingChapter 2
© 2008 ITCourseware, LLC Rev 3.1.1 Page 27
Other pragmas include:
use sigtrap LIST;
# print a stacktrace of the program if a signal
# in the list is generated
use sigtrap;
# print a stacktrace of signals:
# ABRT, BUS, EMT, FPE, ILL, PIPE, QUIT,
# SEGV, SYS, TERM, and TRAP.
use integer;
# force arithmetic to be performed in the more
# efficient integer mode
useinteger.pluse strict;
my $total = &add(2.5,2.5);
print "\$total is $total\n";
sub add {
#use integer; # uncomment to see the effect of use integer;
my(@args) = @_;
my $total;
my $value;
foreach $value (@args) {
$total += $value;
}
return $total;
}
Advanced Perl Programming
Page 28 Rev 3.1.1 © 2008 ITCourseware, LLC
� Perl's -D switch generates many kinds of extra information from the compiler
and interpreter.
� This information is sent to standard error.
� -Dx (or -D1024) prints a syntax tree dump — opcodes, et al.
� -Ds (or -D2) prints a stack snapshot before each opcode.
� -Dt (or -D8) prints an execution trace.
� You may combine these options, either by summing their numeric values
or combining their letter values in a list:
perl -Dst myprog.pl 2>dst.out
perl -D10 myprog.pl 2>dst.out
� Within your program, the numeric sum of the currently set -D options is
available in the special variable $^D.
� The -D switch is available only with Perl interpreters that have been compiled
with the -DDEBUGGING compiler switch.
� The -DDEBUGGING option creates a slower running perl executable,
so you should keep both versions available.
/usr/local/bin/perl
/usr/local/bin/debugperl
Debugging Flags
DebuggingChapter 2
© 2008 ITCourseware, LLC Rev 3.1.1 Page 29
Hands On:
Read the perlrun manpage to see a listing of all the -D flags and their meanings. Run a very simple Perl
script with a debug flag:perl -Dx -e 'print "Hello\n"'
See how much of the output you can figure out. Try it with some other -D options, like -Dt and
-Ds. While using the -Ds option, pass some additional arguments to print.
Test the following program with several of the -D options, one at a time. Be sure to try -Dx,
-Ds, -Dl, -Dt, -Dc, and -Dr. Redirect the standard error output to a file, then examine it
(e.g., perl -Dr dtest.pl 2>dr.out).
Note that the program wants a valid file name as its first argument and, in the regular expression,
searches for lines in the file which begin with u or p and prints them out.
dtest.pluse strict;
my ($filename) = @ARGV;
open (FH, $filename) or die "Can't open $filename";
my $x = "123";
print "$x\n";
$x += 4;
print "$x\n";
while (my $line = <FH>) {
print $line if ($line =~ /^p|^u/);
}
The following abbreviations will appear in some of the output:
SV Scalar Value
HV Hash Value
IV Integer Scalar Value
CV Code Value
PV String Scalar Value
GV Glob Value (typeglob)
NV Double Scalar Value
RV Reference Value
AV Array Value
Advanced Perl Programming
Page 30 Rev 3.1.1 © 2008 ITCourseware, LLC
� If you've ever built and installed Perl yourself, you know how many questions
are asked and answered by the Configure script about your particular system.
� Configure puts this information in config.sh, which is used for generating
Makefiles, etc.
� You may be interested in these details if you're integrating Perl with other
applications or seeking help with debugging.
� Use perl -V to print a summary of the major configuration values.
perl -V
� Methods in the Config.pm module can provide you with all of the configuration
information programmatically.
� Config::myconfig() returns the same information as the -V command-line
option, as a single string value.
� Config::config_sh() returns all of the configuration values, as a single
and very long string.
� Config::config_vars(@varlist) prints the names and values of the
variables named in varlist to STDOUT, suitable for capture by other
programs and scripts.
� The ExtUtils module (used when embedding perl, etc.) can provide similar
information.
Your Perl Configuration
DebuggingChapter 2
© 2008 ITCourseware, LLC Rev 3.1.1 Page 31
Hands On:
Run the following commands to retrieve the configuration variables, compiler options, and link options used
to build your Perl interpreter:perl -MConfig -e 'print Config::config_sh'
perl -MConfig -e 'Config::config_vars(libs)'
perl -MExtUtils::Embed -e ccopts
perl -MExtUtils::Embed -e ldopts
configtest.pluse Config;
#print &Config::myconfig(),"\n"; # Returns "perl -V" info.
#print &Config::config_sh(),"\n"; # Full contents of config.sh!
@cvars=qw(ccflags ldflags perlpath privlib sitearch sitelib);
&Config::config_vars(@cvars);
Advanced Perl Programming
Page 32 Rev 3.1.1 © 2008 ITCourseware, LLC
� Devel::Peek lets you look at internal Perl structures and data values.
� If you understand the contents of the perlguts manpage, you'll
understand the output of this module (and vice versa).
� At its simplest, and perhaps most useful level, using the module involves
two steps:
1. Import the Devel::Peek module.
use Devel::Peek; # or
use Devel::Peek('Dump'); # only dump values
2. Dump the information you're interested in.
Devel::Peek::Dump($somevar);
� Devel::Symdump is a primitive package designed to be subclassed, which
allows you to dump the symbols in a package.
1. Create the Devel::Symdump object.
$sym = new Devel::Symdump('pkg');
$sym = rnew Devel::Symdump('pkg');#recurse
2. Get information from the Devel::Symdump object.
@symbols = $sym->scalars();
print $sym->as_string(),"\n";
print FH $sym->as_HTML();
The Devel::Peek Module
DebuggingChapter 2
© 2008 ITCourseware, LLC Rev 3.1.1 Page 33
peektest.pl#use strict;
use Devel::Peek('Dump');
use Devel::Symdump;
### Build a big, horkin' data structure...
my $topref = {
key1 => [
[ 'key1Elem1_1', 'key1Elem1_2', 'key1Elem1_3', 'key1Elem1_4' ],
[ 'key1Elem2_1', 'key1Elem2_2',
{ key1Elem3_3Key1 => 'key1Elem3_3Key1-Value',
key1Elem3_3Key2 => 'key1Elem3_3Key2-Value',
key1Elem3_3Key3 =>
{ key1Elem3_3Key3_Key1 =>
[
'key1Elem3_3Key3_Key1-Value1',
'key1Elem3_3Key3_Key1-Value2',
'key1Elem3_3Key3_Key1-Value3'
]
},
},
'key1Elem2_4']
],
key2 => 'toprefKey2-value',
key3 => 'toprefKey3-value',
key4sub => sub { my $x = 'hello'; return $x }
};
my $x = "hello";
my $y = \$x;
@pkgB::x_arr = ('pkgB x_arr value 1','pkgB x_arr value 2');
$pkgB::x = "package B x value";
$pkgB::pkgC::z = "pkgC z value";
my $symdumpObj = rnew Devel::Symdump('pkgB');
print $symdumpObj->as_string();
Devel::Peek::Dump($x);
Devel::Peek::Dump($topref);
Advanced Perl Programming
Page 34 Rev 3.1.1 © 2008 ITCourseware, LLC
� Data::Dumper allows you to dump variable values.
1. Import the Data::Dumper module.
use Data::Dumper;
2. Create a Data::Dumper object.
$dumpObj = new Data::Dumper(
[$var1,$var2],['$var1','$var2']);
3. Set up any configuration variables.
$dumpObj->Quotekeys(0);
4. Dump the object.
print $dumpObj->Dump(),"\n";
� Data::Dumper's output is valid Perl code which can be executed (or evaled) to
re-create the dumped data.
� A structure with recursive references can't be created in one step.
� The PURITY flag tells Data::Dumper to output additional statements to
fully define it when it encounters a self-referencing data structure.
The Data::Dumper Module
DebuggingChapter 2
© 2008 ITCourseware, LLC Rev 3.1.1 Page 35
dumptest.pluse Data::Dumper;
### Build a big, horkin' data structure...
my $topref = {
key1 => [
[ 'key1Elem1_1', 'key1Elem1_2', 'key1Elem1_3', 'key1Elem1_4' ],
[ 'key1Elem2_1', 'key1Elem2_2',
{ key1Elem3_3Key1 => 'key1Elem3_3Key1-Value',
key1Elem3_3Key2 => 'key1Elem3_3Key2-Value',
key1Elem3_3Key3 =>
{ key1Elem3_3Key3_Key1 =>
[
'key1Elem3_3Key3_Key1-Value1',
'key1Elem3_3Key3_Key1-Value2',
'key1Elem3_3Key3_Key1-Value3'
]
},
},
'key1Elem2_4']
],
key2 => 'toprefKey2-value',
key3 => 'toprefKey3-value',
key4sub => sub { my $x = 'hello'; return $x }
};
my $a = "hello";
my $b = \$a;
@pkgB::a_arr = ('pkgB a_arr value 1','pkgB a_arr value 2');
$pkgB::a = "package B a value";
$pkgB::pkgC::c = "pkgC c value";
my $dumper = new Data::Dumper([$topref],['$topref']);
$dumper->Indent(1);
$dumper->Quotekeys(1);
$dumper->Purity(1);
print $dumper->Dump();
Advanced Perl Programming
Page 36 Rev 3.1.1 © 2008 ITCourseware, LLC
� Write a program that uses an undeclared variable and one which is used only once. Run your
program with the -w command-line switch.
Run it again without the -w switch, setting the value of $^W to 1 inside the program.
(Solution: warns.pl)
� Copy and modify warns.pl to trap the warnings and call your own subroutine to handle the
printing of the warning. Be sure to include the original warning text as well as your own.
Hint: Use $SIG{__WARN__}
(Solution: warntrap.pl)
� Write a module with a function that issues warnings with warn().
Write a program that uses the function and examine its output.
Change the warn to carp and run the program again. Note the differences in line number and
filename reporting.
Hint: Don't append a "\n" to your warning messages as this disables the output of the filename and
line numbers of warn and carp.
(Solutions: carps.pl, carpmod.pm)
� Write a program which includes the following questionable Perl programming practices:
� An undeclared and unscoped array variable.
� A bareword used as a string in assignments to a scalar.
� A symbolic reference.
Compile and run this program with no warnings.
Once it's working, apply the pragmas use strict 'vars';, use strict 'subs';, use strict 'refs';,
and use strict; one at a time and in that order.
Compile your program each time and note the kind of warnings and errors you receive.
(Solution: strictstuff.pl)
Labs
DebuggingChapter 2
© 2008 ITCourseware, LLC Rev 3.1.1 Page 37
References:
perldoc strict
perldoc perlrun
perldoc perldiag
perldoc perldebug
perldoc Devel::Peek
perldoc Data::Dumper
Resources:
The Perl Journal, Issues #7, #10, #11
� Write a program that declares and initializes a scalar, a simple array, and a simple hash. Create a
separate scalar reference to each of these.
Once your program is running, use the Dump method of Devel::Peek to examine each of these
values.
Hint: You might want to redirect the output to a file to look at.
(Solution: peeker.pl)
Advanced Perl Programming
Page 38 Rev 3.1.1 © 2008 ITCourseware, LLC
Installing and Using Perl ModulesChapter 8
© 2008 ITCourseware, LLC Rev 3.1.1 Page 173
Chapter 8 - Installing and Using Perl
Modules
Objectives
� Find, obtain, install, and use Perl
modules.
Advanced Perl Programming
Page 174 Rev 3.1.1 © 2008 ITCourseware, LLC
� Laziness makes a Perl programmer write labor-saving programs other people
will find useful, and document them so as not to have to answer so many
questions.
� Impatience makes a Perl programmer write computer programs that anticipate
programmers' needs.
� Hubris makes proud Perl programmers write programs they think will withstand
the scrutiny of other Perl programmers who use them.
� Perl5 is distributed with a variety of useful library modules.
� In addition, your nearest CPAN site publishes hundreds of other modules that
might significantly reduce your application development time.
� There is no need to reinvent the wheel!
� For most common problems Perlers routinely face, you can find Perl5
modules encapsulating good solutions.
� Among the many advantages of using a published module:
� It's free.
� It's (usually) well-documented.
� It's been tested.
� Someone else has done the work for you!
Laziness, Impatience, and Hubris
Installing and Using Perl ModulesChapter 8
© 2008 ITCourseware, LLC Rev 3.1.1 Page 175
Among the scores of modules shipped with Perl5.004_04, you'll find:
� AnyDBM_File Provides a framework for multiple DBMs.
� AutoLoader Loads subroutines only on demand.
� AutoSplit Splits a package for autoloading.
� Benchmark Benchmarks running times of code.
� Bundle::CPAN Provides a bundle to play with all the other modules on CPAN.
� CGI Provides the simple Common Gateway Interface Class.
� CPAN Queries, downloads, and builds perl modules from CPAN sites.
� Class::Struct Declares struct-like datatypes as Perl classes.
� Devel::SelfStubber Generates stubs for a SelfLoading module.
� DirHandle Supplies object methods for directory handles.
� English Uses nice English (or awk) names for ugly punctuation variables.
� Env Imports environment variables.
� Exporter Implements default import method for modules.
� ExtUtils::Embed Provides utilities for embedding Perl in C/C++ applications.
� ExtUtils::MakeMaker Creates an extension Makefile.
� File::Compare Compares files or filehandles.
� File::Copy Copies files or filehandles.
� File::DosGlob Provides DOS-like globbing and then some.
� File::Path Creates or removes a series of directories.
� FileCache Keeps more files open than the system permits.
� FindBin Locates the directory of the original perl script.
� Getopt::Long Offers extended processing of command-line options.
� I18N::Collate Compares 8-bit scalar data according to the current locale.
� IPC::Open2 Opens a process for both reading and writing.
� Math::BigFloat Provides an arbitrary length float math package.
� Math::BigInt Provides an arbitrary size integer math package.
� Math::Complex Provides complex numbers and associated mathematical functions.
� Math::Trig Provides trigonometric functions.
� Net::Ping Checks a remote host for reachability.
� Pod::HTML Converts pod files to HTML.
� Search::Dict Searches for key in dictionary file.
� SelectSaver Saves and restores selected file handle.
� SelfLoader Loads functions only on demand.
� Shell Runs shell commands transparently within perl.
� Sys::Hostname Tries every conceivable way to get hostname.
� Term::Cap Provides a perl termcap interface.
� Term::Complete Provides a perl word completion module.
� Term::ReadLine Provides a perl interface to various readline packages.
� Test::Harness Runs perl standard test scripts with statistics.
� Text::ParseWords Parses text into an array of tokens.
� Text::Soundex Implements the Soundex Algorithm as described by Knuth.
� Text::Tabs Expands and unexpands tabs per the unix expand(1) and unexpand(1).
� Text::Wrap Provides line wrapping to form simple paragraphs.
� Tie::Hash, Tie::StdHash Provides base class definitions for tied hashes.
� Time::Local Computes time from local and GMT time.
� UNIVERSAL Provides a base class for ALL classes (blessed references).
Advanced Perl Programming
Page 176 Rev 3.1.1 © 2008 ITCourseware, LLC
� Comprehensive Perl Archive Network (CPAN) is a central (yet distributed)
module repository.
� Browse it at http://www.perl.org/CPAN/ or via FTP.
� Volunteer CPAN sites around the world maintain mirror copies of all published
Perl source code, modules, and scripts.
� When you visit http://www.perl.org/CPAN/, it will attempt to determine
the CPAN site nearest you; it's helpful to memorize the location of your
local CPAN site.
� CPAN lists modules by module, category, and author name.
� Authors who submit modules to CPAN provide a standard format and method
for installing their modules on your system.
� These authors, programmers like yourself, submit their modules, accept
bug reports and suggestions, and release updated versions.
� To submit a module to CPAN, find the instructions for PAUSE (Perl
Authors Upload SErver).
� Become a frequent visitor to your nearest CPAN mirror site.
CPAN
Installing and Using Perl ModulesChapter 8
© 2008 ITCourseware, LLC Rev 3.1.1 Page 177
The CPAN has many categories of modules. Some of them are:02_Perl_Core_Modules
03_Development_Support
04_Operating_System_Interfaces
05_Networking_Devices_Inter_Process
06_Data_Type_Utilities
07_Database_Interfaces
08_User_Interfaces
09_Interfaces_to_Other_Languages
10_File_Names_Systems_Locking
11_String_Processing_Language_Text_Processing
12_Option_Argument_Parameter_Processing
13_Internationalization_and_Locale
14_Authentication_Security_Encryption
15_World_Wide_Web_HTML_HTTP_CGI
16_Server_and_Daemon_Utilities
17_Archiving_and_Compression
18_Images_Pixmap_Bitmap_Manipulation
19_Mail_and_Usenet_News
20_Control_Flow_Utilities
21_File_Handle_Input_Output
22_Microsoft_Windows_Modules
23_Miscellaneous_Modules
Advanced Perl Programming
Page 178 Rev 3.1.1 © 2008 ITCourseware, LLC
� Instead of putting the use module statement in your Perl script, you can use the
-M command-line option to load a module:
perl -MModuleName prog.pl
� If you can't get your system administrator to install the Perl modules you want,
just maintain your own Perl library directory!
� You can specify the library directories in which Perl will search for modules:
� Modify the @INC variable in your script, before calling use or require.
BEGIN {
unshift(@INC, '/home/francis/my_perl',
'/home/jo/perllib');
}
� Or a better alternative is to use the use lib pragma in your script:
use lib '/home/francis/my_perl';
use ModuleName;
� Use the -I command-line switch to push paths onto @INC.
perl -I/home/francis/my_perl prog.pl
� Set the PERL5LIB environment variable as a colon-delimited list of
directory names.
PERL5LIB=/home/francis/my_perl:/home/jo/perllib
Using Modules
Installing and Using Perl ModulesChapter 8
© 2008 ITCourseware, LLC Rev 3.1.1 Page 179
Advanced Perl Programming
Page 180 Rev 3.1.1 © 2008 ITCourseware, LLC
� Installation of a CPAN module is simple:
1. Unpack the module:
gunzip -c ModuleName-1.23.tar|tar xovf -
2. Create a makefile:
perl Makefile.PL
3. Execute the makefile:
make
4. Test the module:
make test
5. Install the module into your Perl directory structure:
make install
� You may run across a module that is just a set of files, with no installation
scripts.
� You'll need to read any READMEs to find out what to do.
� Most likely you just need to copy a .pm file to somewhere where Perl will
be able to find it at runtime.
Installing a Perl Module
Installing and Using Perl ModulesChapter 8
© 2008 ITCourseware, LLC Rev 3.1.1 Page 181
Advanced Perl Programming
Page 182 Rev 3.1.1 © 2008 ITCourseware, LLC
� Modules you download from CPAN are usually in .gzip/.tar format.
� They are typically named in the style:
ModuleName-primaryVersion.subVersion.tar.gz
� You must unzip the file before going further with the module installation:
gunzip -c ModuleName-1.23.tar.gz | tar xovf -
� This creates a directory beneath the current directory called
ModuleName-1.23 and preserves the original archive file.
� You may remove this directory, and the .gz file, once the installation is
complete (not yet, though).
� Don't unpack the module into Perl's standard directory structure.
� Any temporary directory will do.
� Once you've unpacked the module subdirectory, cd into it.
� This would be a very good time to read any files named README or
INSTALL.
Unpacking the Module Source
Installing and Using Perl ModulesChapter 8
© 2008 ITCourseware, LLC Rev 3.1.1 Page 183
For various reasons, the compressed tar files (sometimes called tarballs) that Perl modules are distributed
in are usually compressed with gzip (GNU zip), not the standard UNIX compress command. If your
system doesn't have gzip/gunzip, talk to your system administrator (or download and build it yourself, it's
not too tough to do).
gunzip -c tarfile.gz
leaves the compressed file in place, writing the unzipped data to standard output suitable for input to:| tar xovf -
(x for eXtract, o for assigning your file Ownership to the extracted files, v for Verbose, f - to read tar
archive data from standard input.)
In two steps, this would be:gunzip tarfile.gz
tar xovf tarfile
Occasionally, an author will use the extension .tgz instead of .tar.gz.
Advanced Perl Programming
Page 184 Rev 3.1.1 © 2008 ITCourseware, LLC
� Create the makefile:
perl Makefile.PL
� Makefile.PL is a standard Perl script which has been customized by the author
of the module.
� It checks to see that the module source is complete.
� It may be interactive.
� It may also detect the absence of prerequisite modules; if it does, make
sure to install those modules first, then start over with this module.
� Makefile.PL generates Makefile, a standard UNIX makefile.
� To configure the module to install itself in your own library directory, use the
PREFIX option:
perl Makefile.PL PREFIX=/home/francis/my_perl
� This will replicate the standard Perl library directory structure in a location
of your choice.
� To use modules installed in such a location, you'll have to make sure this
directory is known to your scripts.
The Configuration Step
Installing and Using Perl ModulesChapter 8
© 2008 ITCourseware, LLC Rev 3.1.1 Page 185
Module authors use the standard Perl utility script h2xs and the standard module ExtUtils::MakeMaker
to create Makefile.PL.
Makefile.PL is a Perl script whose job is to generate a Makefile for use with the ubiquitous make
command. Makefile.PL uses information from your local Perl installation to create a Makefile customized
just for your system.
Advanced Perl Programming
Page 186 Rev 3.1.1 © 2008 ITCourseware, LLC
� Run the makefile.
make
� This step compiles any C code necessary for the module.
� You may want to keep the output and any error messages in a log for later
perusal.
make 2>&1 | tee make_log.1
� Most CPAN modules compile cleanly "out-of-the-box."
� If there are any compilation failures, you'll need to solve them before going on.
� Check the README files to see if there are any special options or
modifications you can use in Makefile.PL.
� Look for a README file specific to your system — README.win32,
README.AIX, README.VMS, etc.
� You shouldn't modify Makefile directly; instead:
� Pass parameters to perl Makefile.PL.
� Modify Makefile.PL.
The Build Step
Installing and Using Perl ModulesChapter 8
© 2008 ITCourseware, LLC Rev 3.1.1 Page 187
This step creates a directory named blib/ ("build library") under the module source directory. All module
files are built or copied into this subdirectory for testing.
Advanced Perl Programming
Page 188 Rev 3.1.1 © 2008 ITCourseware, LLC
� Once you've built the module, run any tests provided by the author:
make test
� This command either:
� Runs the script test.pl in the current directory.
� Runs all scripts it finds under a subdirectory named t/, if they have the
filename extension .t.
� You may want to keep the output and any error messages in a log file for later
perusal.
make test 2>&1 | tee make_log.test.1
� Be sure that all the tests succeeded before continuing.
� If they didn't, read the log and README files to see if you can determine
why.
� If you can't, contact the author of the module.
� It may not be necessary for every test to succeed before you can use the
module.
The Test Step
Installing and Using Perl ModulesChapter 8
© 2008 ITCourseware, LLC Rev 3.1.1 Page 189
This uses the module files under blib/ to run the test scripts provided by the module's author.
Advanced Perl Programming
Page 190 Rev 3.1.1 © 2008 ITCourseware, LLC
� You usually need to be logged on with system administrator (root) privileges to
install a module into the existing Perl directory structure.
make install
� When installing into your own library directory, watch for errors; you may
need to create a directory or two, then rerun make install.
� When installation is finished, you can remove the module installation directory
and the archive file.
� If you want to keep the module source code around, to study or to build
again later with different options, you can save space by running make
clean to remove temporary files.
The Install Step
Installing and Using Perl ModulesChapter 8
© 2008 ITCourseware, LLC Rev 3.1.1 Page 191
The make install step copies the tested module files out of the blib/ directory structure into their final
destination — either the standard Perl installation directory, or the location you specified with the PREFIX
option:perl Makefile.PL PREFIX=/my/own/perl/lib.
Advanced Perl Programming
Page 192 Rev 3.1.1 © 2008 ITCourseware, LLC
� The latest versions of Perl5 ship with a module called CPAN, which automates
the module download and installation procedure.
perl -MCPAN -e shell
� When you first use the CPAN.pm module, it will ask you many configuration
questions.
� You'll enter the full URL of your local CPAN site, as in:
ftp://ftp.cs.colorado.edu/pub/perl/CPAN
� It will create a .cpan/ directory under your home directory.
� The MyConfig.pm file under .cpan/CPAN/ contains your configuration
information.
� You can change this file to reflect new preferences.
� Or use the o conf command to set and query options.
� With CPAN.pm you can:
� Search for authors, bundles, distribution files, and modules.
� Display the README of the distribution file.
� Make, test, install, and clean modules or distributions.
� You can determine if you have the latest version.
� Type "h" at the cpan> prompt for a list of commands.
Using CPAN.pm
Installing and Using Perl ModulesChapter 8
© 2008 ITCourseware, LLC Rev 3.1.1 Page 193
cpan> h
command arguments description
a string authors
b or display bundles
d /regex/ info distributions
m or about modules
i none anything of above
r as reinstall recommendations
u above uninstalled distributions
See manpage for autobundle, recompile, force, look, etc.
make make
test modules, make test (implies make)
install dists, bundles, make install (implies test)
clean "r" or "u" make clean
readme display the README file
reload index|cpan load most recent indices/CPAN.pm
h or ? display this menu
o various set and query options
! perl-code eval a perl command
q quit the shell subroutine
cpan> m /odbc/
Module DBD::ODBC (TIMB/DBD-ODBC-0.20.tar.gz)
Module RDBAL::Layer::ODBC (B/BR/BRIAN/RDBAL-1.15.tar.gz)
Module Win32::ODBC (Contact Author DAVEROTH (Dave Roth))
Module html (BJEPS/iodbc-wwwtools-0.20b.zip)
Module iodbc (JMAHAN/iodbc_ext_0_1.tar.gz)
Module wwwodbc (BJEPS/iodbc-wwwtools-0.20b.zip)
cpan> o conf
CPAN::Config options:
commit Commit changes to disk
defaults Reload defaults from disk
init Interactive setting of all options
build_cache 10
build_dir /home/francis/.cpan/build
cpan_home /home/francis/.cpan
ftp /usr/bin/ftp
...
Advanced Perl Programming
Page 194 Rev 3.1.1 © 2008 ITCourseware, LLC
� Most of the modules on CPAN have an associated README, embedded
perldoc documentation, or manpage.
� If there is a README, by all means read it.
� Use the perldoc command to read the embedded POD (Plain Old
Documentation).
perldoc ModuleName
� Use pod2html to generate HTML documentation from the perldoc comments
embedded in a module.
pod2html ModuleName.pm > ModuleName.html
� If the documentation provided with the module doesn't provide you with enough
information to use the module, look at the test programs provided with the
module for hints on how to use it.
Using Module Documentation
Installing and Using Perl ModulesChapter 8
© 2008 ITCourseware, LLC Rev 3.1.1 Page 195
Advanced Perl Programming
Page 196 Rev 3.1.1 © 2008 ITCourseware, LLC
� Create a directory, in your home directory, named perl5lib/. You'll use this directory for modules
you install for personal use, so set up your environment so Perl will search your perl5lib/ directory
for modules at runtime.
� The following modules have been provided for you in this chapter's directory:
Lingua-EN-Inflect-1.84.tar.gz
Games-WordFind-0.02.tar.gz
Roman-1.1.tar.gz
Unpack these archives, read the READMEs, then take the appropriate steps to install these
modules in your own Perl module directory.
� Once you've installed the modules, create HTML documentation for them.
� Based on the documentation, choose one of the modules and write a short program to use it.
Labs
Installing and Using Perl ModulesChapter 8
© 2008 ITCourseware, LLC Rev 3.1.1 Page 197
References:
perldoc perlmodlib
perldoc CPAN
Resources:
http://www.perl.com/CPAN
http://www.perl.com/CPAN/modules
http://www.perl.org/news.html
news:comp.lang.perl.modules
Those* who maintain the integrity of Perl distributions struggle to keep it compact and concise. CPAN has
many modules that most people find useful, but including them all would bloat the perl.tar.gz file beyond its
current 2-3Mb. We may soon see a prepackaged "Perl Developers Kit" containing the most stable versions
of the most useful modules.
*At any given time, only one person on Earth is ordained to apply patches to Perl and to cut releases of the
Perl source distribution from the master source code tree. For obscure reasons, this person is said to be
"holding the patch pumpkin," and is referred to as the "Pumpking." For years it was Larry Wall, but now the
honor, or burden, rotates.
Advanced Perl Programming
Page 198 Rev 3.1.1 © 2008 ITCourseware, LLC
Design and ImplementationChapter 16
© 2008 ITCourseware, LLC Rev 3.1.1 Page 383
Chapter 16 - Design and Implementation
Objectives
� Begin development projects with a
good design and implementation
plan.
� Design classes, library modules,
utilities, and filters.
� Use Getopt to parse command-line
options in utilities.
� Use Perl as a filter.
� Time critical code fragments using
the Benchmark package.
Advanced Perl Programming
Page 384 Rev 3.1.1 © 2008 ITCourseware, LLC
� When setting out to design a module or application, first make sure that you
need to do it at all.
� Is all or part of the work already done in a module from CPAN or
somewhere else?
� Is there an existing, non-Perl tool you can use?
� Pick an approach.
� Object-oriented approach — Abstraction, inheritance, references, etc.
(FileHandle.pm).
� Library approach — Collection of functions and symbols (POSIX.pm).
� All-inclusive — Use as a class, use as a library (CGI.pm).
� UNIX-style utility — Self-contained, command-line options, single task
(h2xs, wwwstat).
� UNIX-style filter — Utility that reads from a file or stdin, and writes to
stdout (pod2html).
Think First
Design and ImplementationChapter 16
© 2008 ITCourseware, LLC Rev 3.1.1 Page 385
Do you think your work will be generally useful, suitable for CPAN?
See if there's a related CPAN package that:
� Does everything you were about to do — if so, you're done!
� Does the same thing, but in a way you don't like — talk to the author about contributing your code or
taking over maintenance.
� Does different tasks in the same area — talk to the author about adding your submodule.
If there's no such module on CPAN, float your idea on news:comp.lang.perl.modules and see what
people think.
Advanced Perl Programming
Page 386 Rev 3.1.1 © 2008 ITCourseware, LLC
� Analyze the problem domain and find the objects.
� What's the thing you're going to represent with an object? A file? An email
message? A process (computer or business)? A database connection?
That's your object.
� Now, packages (modules) are almost always organizational groupings of code; a
module need not be named after the class.
� However, since we say $obj = new ModuleName, it's more OO-ish to
name the module after the class.
� You might use one package as an organizational grouping, and
subpackages as class names.
$carrot = new Motivational::Carrot;
$stick = new Motivational::Stick;
� The subpackage provides a namespace for class variables.
$stick->size($Motivational::Stick::BIG_STICK);
� Subpackage routines are the object (and class) methods.
� Is there any valid reason for not using a hash to represent your object's data at
runtime? Good. Use a hash.
� Okay, seriously, how "big" is each object? What does it need to do? Can
you use a tie? Or a closure? A scalar?
� The reason so many people choose a hash is so the object has named
fields (not numbered ones, as in an array, or only one field, as in a scalar).
Object-Oriented Design
Design and ImplementationChapter 16
© 2008 ITCourseware, LLC Rev 3.1.1 Page 387
Advanced Perl Programming
Page 388 Rev 3.1.1 © 2008 ITCourseware, LLC
� Provide a constructor named new().
� new() should bless a value (a new anonymous hash, usually) into
whatever class is passed as its first argument.
� Don't hard-code the class name, or your class can't be inherited.
� Don't use the one-argument form of bless, which uses the current
package name (same thing, pretty much).
� If new() is passed a reference, get the package name from the reference.
sub new {
$class = shift;
$class = ref($class) || $class;
my $instance = { stick => '==========' };
bless $instance, $class;
}
� Write instance methods to expect an object reference as the first argument.
sub encourage {
my $self = shift;
$self->brandish($self->{stick});
...
� Write class methods to expect a package name.
Object-Oriented Development
Design and ImplementationChapter 16
© 2008 ITCourseware, LLC Rev 3.1.1 Page 389
See CGI.pm for an example of an extremely flexible module. You can use it as an OO class, you can use it
as a library module. Methods work right whether called as object methods, class methods, or even as plain
old subroutines with no object or class argument!
Advanced Perl Programming
Page 390 Rev 3.1.1 © 2008 ITCourseware, LLC
� By far, most CPAN modules are actually library-style modules, even though we
use them with syntax we associate with Perl's OO style.
� They tend to be collections of functionality, not abstract representations
of objects.
� For large libraries, consider using AutoLoader, or SelfLoader, since most
applications will probably use only one or a few of your many routines.
� This can keep down compilation time.
� Again, you can use a package for a general grouping, and subpackages for
specific categories or components.
Library Modules
Design and ImplementationChapter 16
© 2008 ITCourseware, LLC Rev 3.1.1 Page 391
CGI.pm also uses a sophisticated, handmade AUTOLOAD routine, so its methods are only compiled the
first time they're called and not compiled if they aren't called. A simplified example of its approach:package oof;
$AUTOLOADED_ROUTINES = q{
%SUBS = (
'rab' => q!
sub rab { print "Rab\n"; }
!,
'zab' => q!
sub zab { print "Zab\n"; }
!
);
};
sub AUTOLOAD {
my($func) = $AUTOLOAD; # Get name of function called.
my($pack,$func_name) = $func=~/(.+)::([^:]+)$/;
eval "package $pack; $AUTOLOADED_ROUTINES"; # Gives us %SUBS
die $@ if $@;
my($code) = $SUBS{$func_name};
die "Undefined subroutine $AUTOLOAD\n" unless $code;
eval "package oof; $code"; # Gives us sub whatever {}
if ($@) {
$@ =~ s/ at .*\n//; # Remove confusion from error message.
die $@;
}
goto &$func;
}
If this all seems very layered and indirect, you should see the real code in CGI.pm. But the benefits include:
� With over 100 autoloaded routines, users have to compile only those that their programs use.
� CGI.pm can be safely subclassed and the autoload stuff still works.
� It doesn't have the file I/O overhead of AutoLoader, or the limitations of SelfLoader.
Advanced Perl Programming
Page 392 Rev 3.1.1 © 2008 ITCourseware, LLC
� Use Getopt::Std or Getopt::Long to parse command-line options.
� Getopt::Std handles traditional UNIX options (like Perl's options):
cmd -a -b b_arg -c -de
� Getopt::Long handles POSIXish, GNUish options:
cmd --alpha --bravo b_arg --charlie \
--delta --echo
� Maintain an up-to-date $usage or sub usage that prints all valid options.
� Print it when invalid options are used or the user uses -h or --help.
� Definitely embed manpage-style POD.
Utility Programs
Design and ImplementationChapter 16
© 2008 ITCourseware, LLC Rev 3.1.1 Page 393
The Getopt packages automate the parsing of command-line arguments.
use Getopt::Long;
%opt = ();
GetOptions ( \%opt,
"help", # This message.
"f|file|mbox|mailbox=s", # Name of mbox file.
"days=i", # Number of days.
"v|verbose", # More messages.
"q|quiet" # No messages.
);
if ( defined $opt{help} ) {
print "$Usage";
exit(0);
}
$mbox = defined($opt{f}) ? $opt{f} : "/var/mail/$user";
The GetOptions routine parses the command line for options you specify, and those it finds it adds as keys
to the hash whose reference you pass. If the specification includes alternatives for the same option, the first
one will be the key.
Options requiring arguments are followed in the specification by =x, where x is i for an integer, s for a
string, f for a floating-point number. (Getopt validates the argument for you!) If you use :x instead of =x,
the argument is not required for the option. The argument is placed in the hash as the value for that option.
Advanced Perl Programming
Page 394 Rev 3.1.1 © 2008 ITCourseware, LLC
� Make sure your program can take input either from a file or files named on the
command line, or from standard input, with no extra effort by the user.
� How hard is that for Perl?
� Use -T to make sure input is text (if that's important).
� Other than that, design a filter just like a utility program.
� For utilities and filters, carefully consider whether to use nonstandard modules
(don't worry, Getopt is standard).
� Most users expect such programs to run on any stock Perl platform.
Filters
Design and ImplementationChapter 16
© 2008 ITCourseware, LLC Rev 3.1.1 Page 395
Remember that perl itself is a dandy filter (arguably the ultimate filter). If you don't find yourself using perl -p
and perl -n a lot, you need to go back and review these options.
find . -print | perl -p -e 's/[.]// and s:/:\\:g'
find . -print | perl -pe 'tr/[a-z]/[A-Z]/'
find . -print | perl -ne '/solutions/i and print'
perl -pi -e 'tr/\015//d' *.txt
Ever use the -l (dash-ell) option?
perl -e 'for (@INC) { print "$_\n" }'
perl -le 'map {print} @INC' # A tad shorter. Cleaner, too.
perl -le 'map {print} keys %SIG'
This one lists the other modules required to use a module:perl -MFile::Compare -le 'map {print} keys %INC'
perl -MFileHandle -le 'map {print} keys %INC'
perl -MIO -le 'map {print} keys %INC'
Advanced Perl Programming
Page 396 Rev 3.1.1 © 2008 ITCourseware, LLC
� When writing modules and utilities, you certainly don't want your code (rather
than I/O or networking, for example) to be the performance bottleneck for the
end user.
� There are far too many ways of evaluating and improving (and degrading)
performance to list here; at a minimum, though:
� Consider pulling critical code into a compiled C XSUB.
� Master the use of Benchmark for comparing code snippets and regular
expressions to find the fastest.
� Benchmark is especially useful for testing alternative regular
expressions.
Performance
Design and ImplementationChapter 16
© 2008 ITCourseware, LLC Rev 3.1.1 Page 397
timer.pluse Benchmark;
timethese ( 1000000, {
Add => '$i = $i + 1',
Autoinc => '$i++',
OpEquals => '$i += 1',
Silly => 'push @i, $i and $i = @i'
} );
$string = '000245';
timethese ( 100000, {
Match1 => sub { $_ = $string; /00/ },
Match2 => sub { $_ = $string; /^00/ },
Match3 => sub { $_ = $string; /^00[0-9]+$/ },
Subst => sub { $_ = $string; s/00// },
Trans => sub { $_ = $string; tr/0//d }
} );
timer.plBenchmark: timing 1000000 iterations of Add, Autoinc, OpEquals, Silly...
Add: 8 secs ( 7.83 usr 0.10 sys = 7.93 cpu)
Autoinc: 2 secs ( 1.21 usr 0.00 sys = 1.21 cpu)
Opequals: 4 secs ( 4.34 usr 0.06 sys = 4.40 cpu)
Silly: 29 secs (26.70 usr 0.96 sys = 27.66 cpu)
Benchmark: timing 100000 iterations of Match1, Match2, Match3, Subst,
Trans...
Match1: 6 secs ( 5.43 usr 0.14 sys = 5.57 cpu)
Match2: 4 secs ( 4.75 usr 0.10 sys = 4.85 cpu)
Match3: 6 secs ( 6.01 usr 0.10 sys = 6.11 cpu)
Subst: 8 secs ( 6.59 usr 0.10 sys = 6.69 cpu)
Trans: 6 secs ( 4.71 usr 0.07 sys = 4.78 cpu)
Advanced Perl Programming
Page 398 Rev 3.1.1 © 2008 ITCourseware, LLC
� The Benchmark module provides methods for timing code fragments.
� You can provide your code snippet either as a string (suitable for eval) or
as a code reference.
� timethis($count,CODE) runs the CODE fragment $count times and prints the
results to STDOUT.
� timethese($count,{key1 => CODE, key2 => CODE}) runs each code chunk
separately $count times and prints the results for each, labeled with its key.
� The Benchmark module also includes some utility methods you might find
useful:
� timeit($count, CODE) which runs the CODE fragment $count times and
returns the results as a Benchmark object.
� timediff($t2,$t1) which calculates and returns the difference in times
between $t1 and $t2 Benchmark times.
� timestr(Benchmark) which returns a Benchmark time as a string.
� Be sure to time the same code with different sized data sets and varied numbers
of iterations.
� Benchmark warns you if your number of iterations is too small for a
statistically useful result.
Timing with Benchmark
Design and ImplementationChapter 16
© 2008 ITCourseware, LLC Rev 3.1.1 Page 399
datasets.pluse Benchmark;
@ldata = ('-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o',
'--oo--oo--oo--oo--oo--oo--oo--oo--oo--oo--oo--oo--oo--oo--oo--oo',
'--------oooooooo--------oooooooo--------oooooooo--------oooooooo');
@mdata = ('-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o',
'--oo--oo--oo--oo--oo--oo--oo--oo',
'--------oooooooo--------oooooooo');
@sdata = ('-o-o-o-o',
'--oo--oo',
'--------');
for my $data ('sdata', 'mdata', 'ldata') {
for my $x (0..2) {
$code{ $data.' s '."[$x]" } = sub { my $d = $$data[$x]; $d =~ s/o//g };
$code{ $data.' tr '."[$x]" } = sub { my $d = $$data[$x]; $d =~ tr/o//d };
}
};
for (0..2) { print " [$_]: $mdata[$_]\n" }
timethese 10000, \%code, 'all';
timethese 100000, \%code, 'all';
datasets.pl [0]: -o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o
[1]: --oo--oo--oo--oo--oo--oo--oo--oo
[2]: --------oooooooo--------oooooooo
Benchmark: timing 10000 iterations of ldata s [0], ldata s [1], ldata s [2],
ldata tr [0], ldata tr [1], ldata tr [2], mdata s [0], mdata s [1], mdata s [
2], mdata tr [0], mdata tr [1], mdata tr [2], sdata s [0], sdata s [1], sdata
s [2], sdata tr [0], sdata tr [1], sdata tr [2]...
ldata s [0]: 4 secs ( 3.13 usr 0.00 sys + 0.00 cusr 0.00 csys = 3.13 cpu)
ldata s [1]: 3 secs ( 3.03 usr 0.00 sys + 0.00 cusr 0.00 csys = 3.03 cpu)
ldata s [2]: 3 secs ( 2.98 usr 0.00 sys + 0.00 cusr 0.00 csys = 2.98 cpu)
ldata tr [0]: 1 secs ( 0.83 usr 0.00 sys + 0.00 cusr 0.00 csys = 0.83 cpu)
ldata tr [1]: 1 secs ( 0.85 usr 0.00 sys + 0.00 cusr 0.00 csys = 0.85 cpu)
ldata tr [2]: 1 secs ( 0.85 usr 0.00 sys + 0.00 cusr 0.00 csys = 0.85 cpu)
mdata s [0]: 2 secs ( 2.09 usr 0.00 sys + 0.00 cusr 0.00 csys = 2.09 cpu)
...
Advanced Perl Programming
Page 400 Rev 3.1.1 © 2008 ITCourseware, LLC
� Write a program using Getopt::Long to parse a list of command-line options: -a, -h (which prints a
usage message and exits), -n (which accepts an optional integer argument), and -s (which requires a
string argument). It should print out which options were used, and any arguments passed to each
option. The user should be able to get a usage message by using either -h or --help.
(Solution: opts.pl)
� Modify your solution for the previous lab so that it accepts the option -l. This option should accept a
list of arguments, which your program should print out. See the documentation for Getopt::Long.
(Solution: optlist.pl)
� The data:
First string:"714-555-1212 one, (714) 555-1212 another, and (714)-555-1212 another"
Second string:"Two are (714)555-1212 714-555-1212, and the third (714)-555-1212"
Write a program with a single RE that will parse the two strings above and return a three-element list
containing only the phone numbers (in whatever form they appear in the string). Put the two strings
in an array and, in a foreach loop, apply the RE and print the resulting list for each of the strings.
Once this program is working, wrap the foreach loop in a for loop and use the Benchmark
methods to time ten thousand iterations of the outer loop.
Let's see who comes up with the fastest RE.
(Solution: parser.pl)
Labs
Design and ImplementationChapter 16
© 2008 ITCourseware, LLC Rev 3.1.1 Page 401
References:
Programming Perl: 537-546, 546-550, 445-453, 277-325
perldoc perlmodlib
perldoc perlbot
perldoc Getopt::Long
perldoc Getopt::Std
perldoc Benchmark
Resources:
The Perl Journal, Issues #7, #10, #11
Advanced Perl Programming
Page 402 Rev 3.1.1 © 2008 ITCourseware, LLC