Win32 Perl Programming:
The Standard Extensions, Second Edition
You can purchase this
My goal was to write a book that was concise, informative
and useful. I didnít want the book to become just another stack of paper
sitting on a shelf or propping up someoneís computer monitor. I wanted the book
to matter. Something that is kept within armís reach from
the keyboard. It appears that this was exactly what happened.
In my ideal world all reference books would be just that,
used for reference. I certainly donít have time to read through paragraphs of
some author droning on about stories of his youth. Instead I want the author to
tell me what I need to know. After all, if I wanted a biography, I wouldnít have
been looking in the computer section of the bookstore.
The goal of this second edition is much like the first editionís.
Basically the book has been updated to reflect
the latest version of Perl (version 5.6) and what it is capable of. While
researching what to add to the second edition I was pained in having to decide
what was in and what was out. Just how does one decide if one extension is more
useful than another, anyhow? The criteria I decided to use was
based on the amount of questions that were asked on several different forums.
Email lists, Usenet groups, personal messages people have sent me as well as
discussions at various conferences were among the list of sources I used. I
also considered what extensions are the basis of
others. For example, the Win32::API extension is used by Win32API::Net,
Win32API::File and Win32API::Registry. So it makes sense to explain Win32::API.
I know that including some extensions but excluding others
may appear to seem like there is some sort of agenda involved. I can assure you
that there is none. I concentrate on the ďStandard ExtensionsĒ; those that
either a) come with Win32 Perlís standard library and
are commonly used or b) are so very commonly used that they deserve recognition
(such as Win32::Lanman).
Microsoftís Win32 platforms (such as Windows 95/98/ME and
NT/2000/XP) have become quite popular. The reasons behind this depend upon how
you view the industry. Some believe that Win32 is so common because it is a
good operating system. Yet others believe the sole reason for its popularity is
due to its presence on practically every new PC being shipped. Regardless of
the reason, it is uncontested that indeed more and more people are using the
Along with this surge of Win32 users comes a surge of Win32
Perl programmers. These coders are unique from most others in the Perl
community. They have been long pampered with the Microsoft Windows GUI
environment which provides clever and convenient techniques to retrieve
information. By using icons, graphics, sounds, and a host of other sense
provoking resources, the ability to access programming documentation is
effortless and even enjoyable.
The documentation that exists regarding Perlís
Win32 extensions, however, is far from convenient. Typically it includes a list
of functions, the parameters they accept, and a brief description of what each
function does. But for a Perl programmer new to Win32 or to someone new to
programming in general, such limited documentation can leave him lost, confused,
and frustrated. This becomes self-evident if you peruse the Usenetís Perl
forums where newbies post questions that others
bemoan about being obvious or in some obscure FAQ.
This book takes aim at this issue and documents not only the
Win32 extensionís functions but explains what they do, why they do it, and goes
into detail explaining what to expect as a result of the function. For many functions there are in depth details that give the reader
not only the knowledge of how to use the extension but also when to use them.
Since the Perl Win32 extensions generally are written and
put out on the Internet without funding, it goes to reason that there would be
a lack of documentation. After all, it takes enough time to create, test, and
maintain an extension let alone write comprehensive tutorials. Any
documentation that an author provides is just icing on the cake. Having written
several extensions myself, I can assure you that it sucks up quite a bit of
time maintaining them.
Since an extension is not really useful unless you know how
it is used, it is reasonable to feel that documentation is quite important. The
problem is that the lack of adequate documentation compounded with the need to
study such information creates a knowledge void. This
is far more prevalent with the Win32 extensions than it is with Perl in
general. You can find a plethora of Web pages rehashing Perlís
main pages but almost all of these are based on the non-Win32 specific
extensions. This does not help my clients who are looking for documentation.
They quite often ask me what the best resources are for learning how to use the
Win32 extensions. My reply is that there are four basic avenues to follow:
The Internet is a wealth of information on the Win32
extensions. Chapter 1 and Chapter 12 list Internet resources that are very
valuable to anyone programming the Win32 extensions. Typically these resources
are either very vague and not too helpful or they are
very specific and technical but do not explain how one extension works with
Studying an extensionís source code is the absolute best way
to learn how an extension works. This is where you learn the full details on
how a particular extension implements its features and even uncovers
undocumented features. It also lets you discover bugs that may explain why a
function does not work as advertised.
The biggest drawback of this learning technique is that many
of the Win32 Perl programmers are either not familiar with C/C++ or they donít
understand the Perl extension API. For those who feel comfortable with the C
programming languages, Chapter 10 explains the details of a Win32 extension.
Experimentation is one of the most valuable ways to discover
the uses of an extension. By creating test scripts that call extension
functions in several different contexts, the treasures hidden within a Win32
extension can all fall into place. This particular style of learning, however,
can be time consuming and lead to quite a bit of frustration.
It is books like this that give the reader insight into the
extensions. My bookshelves are filled with reference books ranging from Win32
and C++ to AppleTalk and cryptography. A good reference book is more valuable
than gold to a programmer. It can save hours of calling up friends, accessing
Web search engines and ripping apart header files. It is the aim of this book
to find a spot on your bookshelf with other reference books that you find
It seems that most Win32 Perl books that have come out
lately try to address so much that the details are lost. It does me no good to
have 10 books all lightly covering the same topics; I would rather have 5 books
each addressing different topics in detail.
This book neither tries to teach Perl nor explain the Win32
API. Instead it explains Perlís standard Win32
extensions. Everything from Chapter 1 to Appendix C revolves around these
extensions: how to use them, when to use them, and how to understand them.
Along the way I explain many aspects of the Win32 API, Perl, and other related
topics, but focus on the extensions. This book was written to compliment good
Perl programming and Win32 API books, not to replace them.
This book is not really for the Perl beginner. Someone just
starting to learn the intricacies of Perl may find this resource a bit
confusing. This is not to say that beginners should avoid it but they should
understand that this book assumes at least a rudimentary understanding of Perl.
Concepts such as regular expressions, scalar variables, hashes, arrays,
modules, extensions, and others are routinely tossed around. If a beginning
Perl programmer reads this text without a good resource guide to explain these
concepts it will probably be of little use.
Just as a Perl newbie may find this book to be a bit more
than he bargained for, so will someone with no Win32 background. This book
assumes the user is familiar with Win32 concepts such as domains, user and
group accounts, the Registry, primary and backup domain controllers,
networking, and Remote Access Services (RAS).
Keep in mind that lacking an understanding of these concepts
does not preclude anyone from reading and appreciating this book. It simply
means that the reader may need to consult other resources to understand them.
Some concepts, however, are described because of the nature of the topic. For
example, the topics of COM, OLE, ODBC, and named pipes are directly related to
the way particular extensions function. Therefore a rudimentary description of
these concepts is provided to facilitate learning how to use their respective
So there you have it. This book targets intermediate and
advanced Perl programmers who have at least a basic understanding of Win32
platforms. These readers can be very generally divided into five categories
(for the sake of simplicity and brevity I will address only five):
Users make up the bulk of the Win32 market. Some are forced
to use it at work and others use it at home. Yet others fall into fringe
categories, but none the less use the platform. Many of these people will
eventually have needs to automate processes for a variety of reasons. Maybe
they will want all temporary files deleted from the hard drive upon boot up.
Maybe some will want to automatically download a Web page every hour. Who knows
what would cause a user to want to learn Perl, but indeed users are learning
This book explains how most of the common Perl Win32
extensions work. While relating this information, it also explains many of the
fine points that help clarify why a function may perform in a way that may not
seem reasonable. Tips and notes also pave the way for any user who is learning
either Win32 Perl or the Win32 platform (or both).
Administrators have the greatest reason to benefit from
using Perl. Having been a system administrator for WANs with thousands of
users, I can say without any doubt that Perl has saved my hide too many times
From simple utilities, such as logon scripts to automatic
account creation scripts, Perl is an administratorís most indispensable tool.
My colleagues and I have used Perl scripts as a poor manís Systems Management
Service (SMS)óallowing me to install, update, and manage administrate thousands
of client and server machines across LANs, WANs, internets, and intranets.
Win32 Administrators will find this book particularly
important since it covers the most important administration functions. Topics
such as user, group, and machine management are covered as well as permissions,
sending messages, and file management.
Sure you can program some rather sophisticated applications
using C, Pascal, or Visual Basic but for quick prototyping I will bet that Perl
would beat most any language hands down. The ability to quickly and effectively
slap together a working script for a proof-of-concept project is one of Perlís great abilities. For example, I can write code that
connects to an ODBC data source, queries the database, retrieves and processes
the results faster using Perl than I can open Microsoft Access and create a
query by dragging and dropping icons.
Any programmer will feel at home with Perl by her side. Even
if she uses another language, Perl can still be the reliable tool that saves
the day. Recently I had to walk through all the headers of one of our C++
projects picking out error messages and their associated error code values. The
source tree was 38 megs of source code which equates
to quite a few header files. By taking 10 minutes to write a Perl script I was
able to process all header files in under 15 minutes (including the time to
write the script)!
Programmers will find this book to be a valuable reference.
Since Perlís Win32 extensions make copious use of the
Win32 API any Win32 programmer will have a better understanding of which
extension provides the best interface to achieve a desired goal.
With all the hype about the Internet, Webmasters have found
it necessary to learn how to deal with programming issues such as Active Server
Pages (ASP), Common Gateway Interfaces (CGI), Data
Source Names (DSN), user accounts logging on as services, permissions, and a
variety of other issues. Of course Win32 Perl is a natural in these regards.
This book not only addresses common traps that Webmasters fall into but it
explains how to avoid them.
For our Unix brethren who have
found it necessary to migrate data or machines to the Win32 world, this book
can be a life saver. Perlís Win32 extensions can ease
the migration by providing functions which are needed to make a Unix
userís pilgrimage into the Win32 landscape a bit more
familiar. This book covers topics such as scheduling batch jobs to run (similar
to CRON entries), Inter Process Communication (IPC) such as named pipes,
shortcuts (a kind of symbolic links), and permissions in addition to user and
The way that this book is structured is a bit of a departure
from other books that cover similar topics. It seems that most of them address Perlís Win32 extensions by name. That is to say each
chapter covers a particular extension.
One of the biggest complaints I have received from coders on
the Internet relates to not knowing what extension performs which function. Now
consider this: If you donít know what extension performs which function, how
useful is a book that assumes you know each extensionís functionality? If you
wanted to create a new user group, would you intuitively know to look up the
chapter that covers Win32::NetAdmin? Or if you needed to get a list of the
CD-ROM drives on your computer, would you know to flip to the pages that
discuss the Win32::AdminMisc extension? Most users who have talked with me
about these issues tell me that they would have no idea.
This book addresses this problem by designating each chapter
with a programming topic. These chapters cover a topic that a Win32 Perl
programmer may find useful from computer administration and automation to
accessing database data and interfacing directly with the Win32 API. There are
some extensions, however, that are so specific in their functionality that they
really only apply to one topic, so they end up getting a chapter all to
themselves (such as the chapters on OLE and ODBC).
The chapters break out into the following discussions:
This chapter discusses the history of the Win32 port
of Perl. Exactly what extensions
are and how they are used. This includes an examination of the differences
between methods and functions. A discussion regarding how to handle errors when
using Perlís Win32 extensions is also covered.
Chapter 2 covers the basics of network administration. This
includes discussions on how to discover the machines on your network, resolving
DNS names, managing shared resources, and managing RAS servers.
Details on managing a computer are discussed with emphasis
on user and group accounts, user RAS privileges, INI files, the Registry and
This chapter is all about files. Any user
who is looking for information on how to manage file attributes would want to
crack open this chapter. Win32 shortcuts are also covered, including how
to create, manage, update, and assign hotkeys to them. Finally the art of
monitoring a directory for changes are explained.
The ability to automate programs by using OLE is discussed
in Chapter 5. Here you will learn not only what automation is but how it works
and how you can use it to get the most out of Windows programs.
Chapter 6 covers the details surrounding Win32 communication
techniques of message sending and named pipes.
This chapter dedicates itself to accessing databases using
the Win32::ODBC extension. What ODBC is and how your script can interact with
it are what you can expect. Since SLQ is the query language of choice for ODBC
it, too, is discussed.
Process management is covered with a
emphasis on process creation. You will learn how to spawn a new process using
This chapter covers some of the miscellaneous tidbits that
donít seem to find a home in the other chapters. Here a detailed discussion
about controlling consoles (a.k.a. DOS boxes), playing sound files (in the .WAV
format) and interacting with the Win32 API. Additionally, the miscellaneous
functions found in the Win32.pm module (and extension) are discussed.
Whether you are writing an extension or reading the source
code for an existing extension, this chapter explains the details that make it
all make sense. Explanations of scalar variables, arrays, hashes, and
references are provided. For anyone who has tried to read an extensionís source
code, this chapter describes the entire process.
One of the most powerful aspects of Windows NT/2000/XP
machines is that they support true security. This allows an administrator to
set permissions on particular objects such as files, directories, printers,
network shares and others. However, whereas this is a powerful tool it is also
very difficult to understand and program. This chapter describes how Win32
security works and how you can use Perl to discover and modify security
This chapter describes some of the more common problems that
a programmer will run into when using Perlís Win32
extensions. This includes not only extension specific problems but also CGI and
ASP issues are taken to task.
This appendix is a detailed reference, which illustrates the
all of the functions in the extensions which are covered in this book. Each
functionís syntax is shows along with a brief description of what it does.
There have been so many requests for Win32::ODBC constant
and function details that I am providing this information in Appendix B. Here
you will find a wealth of information regarding what functions ODBC supports
and what constants unlock the treasures that await an ODBC user.
With the so many possible Win32 network errors, this
appendix provides a programmer a way to help figure out what a network related
error really means.
Almost all programmers have a programming style that they
use. Perl programmers are no exception. Unfortunately many of the styles used
are not designed for clarity. This can be problematic for someone who is either new to the language or who has to read through
pages upon pages of source code.
I have heard many coders tell me that they donít prefer my
style of coding for a variety of reasons, but then again I have been told that
my style is relatively easy to read. Since it is true that what one coder
dislikes another coder adores, I think that it is important for you to
understand my style.
It has been suggested that my coding style is not a proper
Perl coding style. If you were to read the popular Perl coding books you will
see an obvious distinction between my style and other styles. Whereas my style
may not be a proper Perl style, I have to remind critics of a long held notion
that Perl programmers keep sacred to their heart, ďThere is more than one way
to do it!Ē
Generally speaking I make use of the so-called Hungarian
Notation programming style. This is where the name of a variable begins with a
prefix that indicates the type of data that the variable represents. For
$iTotal = 0;
$szName = "my test text
Here the $iTotal variable has a
prefix of "I" indicating that the variable represents an integer
value. The other variable $szName would represent a
character string (in C parlance the "sz" is
indicative of a zero (or NULL) terminated string).
Now all of the hard core Perl readers are thinking, "Sure
that is fine for C coding, but this is Perl!" Since Perl variables have the
ability to morph themselves into whatever format they need to be in, prefixing
the variable with a type indicator is not necessary. This is true. True, that
is, until either a) your script becomes so large that it is considered a
program or b) other (possibly non-Perl) programmers will have to read and
modify your code.
It ends up that for many of my clients this style is
absolutely invaluable. Some scripts that I have authored are large and are used
in production environments by C++ coders. When they have to modify the Perl
code it is helpful to understand what the variableís intention was: a boolean flag, a character string,
a floating point number, a reference (a.k.a. a pointer), and so on.
Another part of the Hungarian style is the way case plays a
role in a variable. Basically speaking the beginning character of a word is
capitalized as in:
$iTotalSortedEntries = 33;
If you were to not use mixed case that variable would look
$itotalsortedentries = 33;
I find it much easier to read the first example.
Perl has been a fun language to learn. With all of its
obfuscation and hidden tricks that keep you guessing. But it is also a fast
language to code in. I was once in a room with two engineers who took over 20
minutes trying to convince Microsoft Excel to properly format a histogram
chart. I finally stopped what I was doing and wrote a simple script that not
only formatted the data correctly but controlled Excel to manually create the
chart. I was able to do this in under 5 minutes.
Literal, both of the engineers jaws dropped and they
both asked for copies of the script.
Perl is fast. Perl is quick. Perl is open source. Perl is
not well documented. This book is here to help. I hope that this book serves
you well and that you enjoy reading it as much as I have enjoyed writing it.