|
|
|
Cross-Platform Perl, Second Edition
by Eric Foster-Johnson
IDG Books Worldwide, 2000
ISBN 0-7645-4729-1 (second edition)
|
Practical, hands-on perl
|
|
When I first started learning Perl, I actually thought Perl was an evil
plot. The syntax tends to the obscure and obtuse and is just plain difficult
to master. You'll find lots of bizarre syntax, such as the ubiquitous
$_, which does more to prove theories of alien abductions
than anything else. Perl is a very flexible language that allows for many
ways to get any job done. In the Perl community, this is considered a good
thing. The key is finding a successful way to get your job done.
And, that's why I wrote this book.
"Before you think UNIX is family-oriented, note that all children must die."
First edition rated with 4-camels by
Tom Christiansen's Review of Perl books!
|
|
Goals
|
|
|
My goal in writing Cross-Platform Perl
is to free you from experiencing the same problems I did.
Let's face it;
Perl is hard. So, this book provides an easy,
straightforward introduction to Perl with scads and scads of examples. This
book goes far beyond the Perl basics, tackling the tough issues that
you'll need to solve to get your work done. I tell you where Perl
really is evil and how you can get around it. I also provide a lot of
handy Perl routines you can reuse repeatedly, not only as examples to
learn from but in your actual work. And, I try to shed some light on
the confusion that is Perl. Perl doesn't have to be difficult
if you avoid the confusing constructs.
If you've ever been intimidated by Perl, this is the book for you.
Cross-Platform Perl was the first book to cover Perl on both
UNIX and Windows as well as using Perl/Tk for creating graphical
user interfaces. The newly-expanded second edition includes new
chapters, more coverage of Windows and Linux, and a fully-updated text.
You'll see a lot more on networking, Windows-specific modules, Linux, and
XML.
|
|
|
Table of contents
|
|
|
|
Second edition errata
|
|
|
|
First edition errata
|
|
|
The following typos have been found in the first edition of the book.
Note: All of these problems are fixed in the second edition.
Thanks to Glenn Carr,
Tom Grydeland, Bruce Murray, Patrick Riley, and Kathy Smart
for pointing them out. If you have found any problems, please
send me a message. Thanks.
- Page 9 Contacting the Author
- I've moved my Web pages to:
http://foster-johnson.com
- Page 39-40 Code examples for Ending Modifiers
- The code examples shouldn't have the semi colons right
after the statement. These code examples from pages
39 (the end) and 40 (the top) should read:
statement if condition;
statement unless condition;
statement while condition;
statement until condition;
- Page 56 correction for typo at the end of the page
- The last text paragraph should read:
To access individual values in an associative array, you use a
different syntax. Any value in an associative array can be accessed
through its key name, using the following syntax:
$assoc{keyname}
- Page 57 in
keys.pl
- Kathy Smart reports that the
keys.pl script
won't work with Windows NT 4.0 with IIS 3.0 unless you rename it
to something like keys2.pl. if anyone has
any more information on why keys.pl would be
a reserved script name, please send me a message. Thanks.
- Page 106 Table 4.2
-
/[^aA]/ returns true if any character is
found that is not a or A.
- Page 107 Table 4.4
-
/a\b/ Match a at end of word.
/a\B/ Match a not at end of word.
- Page 107 Warning Icon
-
/[^a]/ will return true if there is any character
not a anywhere in the word.
- Pages 109, 111, 116 in
pattern.pl and
path.pl
- While you can use
| as a delimiter for both
m and s,
it is a special character for regular expressions, so
you probably don't want to do this. A better approach would
be to use open and close delimiters like the following:
m<^/usr/local>
or:
$path =~ s</usr/bin> {/usr/local/bin};
- Page 117 in
tr code example.
- With the following statement:
$newname =~ tr/[A-Z]/[a-z]/;
You do not need the brackets in tr expressions,
so the code should look like:
$newname =~ tr/A-Z/a-z/;
You should make similar changes to tolower.pl on page
and toupper.pl on page 121.
- Page 112 in Table 4.6
- A better explanation for the m and s options comes from
Tom Grydeland:
|
/pattern/m
|
Allows ^ to match after every newline and $
to match before every newline in the string being
searched. This makes "beginning of line" and "end
of line" meaningful, even in multi-line strings.
|
|
/pattern/s
|
Allows . to match newline. This is useful for
matching a pattern with . in a multi-line string as if
it were a single line.
|
- Page 141 in
time1.pl
- Comment out the line that increments the variable hour,
as shown below (with the changed line in bold):
#
# Using time and localtime.
#
$t = time();
($sec, $min, $hour, $dom, $mon,
$year,$wday, $yday, $isdst) =
localtime($t);
# Convert data to normal values.
# Comment out the next line.
#$hour++; # from 0..23
$year += 1900;
# Provide English equivalents.
@months = ("January", "February",
"March", "April", "May",
"June", "July", "August",
"September", "October",
"November", "December");
@week = ("Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday",
"Friday", "Saturday");
# Print data.
printf ("Time is: %2.2d:%2.2d:%2.2d\n",
$hour, $min, $sec);
printf("Date is: %s, %d-%s-%d\n",
$week[$wday], $dom, $months[$mon],
$year);
printf("%d days since 1 January\n",
$yday);
if ($isdst) {
print "Is daylight savings time.\n";
} else {
print "No daylight savings time.\n";
}
# time1.pl
- Page 157 in
backtick.pl
- You need to remove the extra newline returned
from the pwd command. Change the
backtick.pl
script to the following:
#
# Using back-ticks to
# capture program output.
#
$dir = `pwd`;
chomp($dir);
print "Directory: $dir\n";
# backtick.pl
- Pages 158-160, with
fork
- The following line is wrong and will actually
undefine the variable (dumb typo on my part):
} elsif (undef $pid) {
Change this (on all the pages mentioned above) to:
} elsif (! defined $pid) {
- Pages 162-163
- I should insert a note that it's better to use
the UNIX forward slash for most Windows path names.
The double backslash is a bit tedious, but I want to stress
that users will expect to enter the backslash directory
separator and Perl coders need to deal with this.
- Pages 162-163, in
winapp.pl
- You need to add
use Win32::WinError; and
call Win32::WinError::GetLastError instead of
Win32::GetLastError, as shown below (with the
changed lines in bold):
Note: I just tried this with Perl for Win32 build 306
and the original script worked. So, try the script first. If you
have problems, try the changes as listed.
#
# Launch a Windows process from Win32 module.
# See online docs for more on this subject.
#
use Win32;
use Win32::Process;
use Win32::WinError;
#Create the process object.
Win32::Process::Create($ProcessObj,
"C:\\WINNT35.0\\NOTEPAD.EXE",
"NOTEPAD",
0, # Don't inherit.
DETACHED_PROCESS,
".") || # current dir.
die &print_error;
#Wait for the process to end. No timeout.
$ProcessObj->Wait(INFINITE) ||
warn &print_error;
$ProcessObj->GetExitCode($ExitCode) ||
warn &print_error;
print "Notepad exited with $ExitCode\n";
print Win32::FormatMessage( Win32::WinError::GetLastError() );
sub print_error {
print Win32::FormatMessage( Win32::WinError::GetLastError() );
}
# winapp.pl
- Page 169, in
config.pl
- This should also check for Solaris and SunOS, two
common flavors of UNIX.
- Page 189, The file
msexcel.pl is missing from the
CD-ROM
- Sorry about that! Thanks to Bruce Murray for pointing
this out. Here is the file:
#
# Use OLE to control MS Excel.
#
use OLE;
# Create an instance of Excel.
$app = CreateObject OLE "Excel.Application" ||
die "Unable to open Excel.";
# Make application visible.
$app->{'Visible'} = 1;
# Create a new workbook.
$app->Workbooks->Add();
# Set values in a "range".
$app->Range("A1")->{'Value'} = "Developer";
$app->Range("B1")->{'Value'} = "Bugs";
$app->Range("B2")->{'Value'} = "Assigned";
$app->Range("C2")->{'Value'} = "Fixed";
$app->Range("A3")->{'Value'} = "Eric F-J.";
$app->Range("B3")->{'Value'} = 10;
$app->Range("C3")->{'Value'} = 10;
# Leave Excel running. Use $app->Quit() to exit.
# msexcel.pl
- Page 264 Missing part of a sentence.
- If you're using an integrated environment, such
as Microsoft Visual C++ or SunSoft Workshop, you still need to
initiate the environment to rebuild the application.
- Page 337, the home page for the Win32 port of Perl
- This has moved to:
http://www.activestate.com
|
|
|
|
|