I have hundreds of Perl scripts that
I use. Many of these scripts
process some kind of data, such as
URLs, email addresses, or paths. Historically, I would write Perl scripts that
would obtain this data from an input
file or from the keyboard. For
example, I would run an application, copy
and paste the application's resulting
data into an input file, then run a Perl
script that read from the input file.
One day, however, all this changed. I
started using the Win32::Clipboard
extension. I can't tell you why it took
so long for me to realize a better way
of providing input for my Perl scripts. I suppose all that copying and pasting
never really bothered me enough to
do something about it.
With the Win32::Clipboard
extension, the way I provide input to scripts
has changed. For example, I have one
script that formats network packets
captured by the Ethereal network
tracing program. When network traffic is
captured, all I need to do is copy a
particular packet to the Windows
Clipboard. The script continuously
monitors the Clipboard for changes.
When a change is detected, the script
retrieves the packet of data from the
Clipboard, decodes it, and formats it
so that it's legible to humans. The
script then writes the formatted data
back to the Clipboard. Therefore, with
this script, all I need to do is run
Ethereal, copy a packet to the Clipboard,
then paste the formatted results into
an email or a report.
Using the Win32::Clipboard
extension has saved me countless hours of
work. If you also want to save some
time by using the Win32::Clipboard
extension, you need to know how the
extension works and how to use it in
a script.
How the Extension Works
In Windows, every time you select and
copy something in a program, it's
stored in the Clipboard, which is a
temporary memory buffer. Any
subsequent use of the Paste command
attempts to transfer a copy of the
Clipboard's contents into the selected
application.
To access the Clipboard, Perl
scripts can use the Win32::Clipboard
extension, which is part of the
ActivePerl standard distribution
(http://www.activestate.com). Aldo
Calpini, one of the unsung Win32 Perl
heroes, wrote the extension.
Using the extension is very simple.
Because there's only one shared
Clipboard for the entire Windows OS, the
Win32::Clipboard extension doesn't
require you to create a Win32::Clipboard object. However, to follow common Perl practice, I encourage you to
do so. Therefore, to start using the
extension, a Perl script should first
generate a Clipboard object. With this
object, the script can perform a
couple of basic tasks: determine the type
of content on the Clipboard, retrieve
the content on the Clipboard, and
copy content to the Clipboard.
Win32::Clipboard recognizes only a few different data formats. The formats are text data, bitmap graphic data, and lists of filenames and directory paths that have been copied to the Clipboard (such as from Windows Explorer). You can determine the data format by calling the Clipboard object's IsText(), IsBitmap(), and IsFiles() methods. Each method will return a Boolean value of TRUE (which has a numeric value of 1) when the method reflects the format of the data currently on the Clipboard.
After determining the kind of data on the Clipboard, the script can use the Get() method to retrieve the data. This method returns the data as a text string when the data is in text format, as a standard binary bitmap when the data is in graphic format, or as an array of paths when the data is in filelist format. The Get() method doesn't modify or erase data from the Clipboard, so the script can call it as many times as necessary. After retrieving the data from the Clipboard, the script can process that data any way it chooses. For example, the script can save the data to a file or display the data on screen.
If the data is in text format, the script can even modify the data, then copy it back to the Clipboard for storage. (This won't work with data in the graphic or file-list format. This is a limitation of the Win32::Clipboard extension.) To copy text data back to the Clipboard, the script needs to pass the data to the Set() method. The data passed in will overwrite any existing data on the Clipboard. To erase the Clipboard's contents, the script can call the Empty() method.
There's one last Win32::Clipboard method that's worth noting: WaitForChange(). This method causes a Perl script to fall asleep until it detects a change to the Clipboard or until a specified timeout value is exceeded. When a timeout value isn't passed to the method, the script will wait forever until a Clipboard change is detected. When a timeout value (in milliseconds) is passed to the method, the script will wait until the specified time, waking up earlier if a change is detected. WaitForChange() returns a Boolean value of TRUE (1) when a change is detected. Any other value indicates that the script slept for the specified time or there was an error.
How to Use the Extension in a Script
The ClipboardMonitor.pl script, which Listing 1, shows, demonstrates how you can write scripts that interact with the Clipboard. This script uses the Win32::Clipboard extension to continuously monitor the Clipboard for changes, then performs an action when a change is detected.