<<

. 53
( 69 .)



>>

‹ Type open(INFILE, ˇ Press the Spacebar four
"<myfile.txt");, replacing times, type print $_; and
myfile.txt with the name of press Return, and then type }.
your file, and press Return
twice.

258
15
WORK WITH PERL




Another way to read files in Perl is to use backticks. For example,
if you want to read the contents of a file into an array, you can
use a command such as @lines=`cat myfile.txt`;. This
command runs the cat command, collects the output, and stores
each line in that output as an element in the array @lines. While
this operation is as simple as the more formal open commands, it
also involves more overhead ” especially for very large files ” as
it stores the data it reads into memory. When the operation reads
a file one record at a time, it stores one record at a time.
Backticks are a good way to get information from your system.
For example, if your script needs to use the current date, you can
assign the date to a variable using a line such as $date=`date
+%m%d%y`;. This command stores a date of the form 05/01/03 to
the $date variable. You can then use this variable to name output
files or to add records with a date stamp to a file you are creating.
For example, you can open an output file with a command such
as open(OUTFILE,">myfile.$date");.




Á Save your script, and exit ‡ Type chmod a+x followed · Type ./read1 and press
Pico. by a space. Return.
– Your script runs,
° Type read1 and press
Return. displaying each line in
the file to your screen.



259
UNIX FOR MAC


WRITE FILES WITH PERL
If you write text that does not contain linefeeds, you will

Y
ou can write files with Perl as easily as you can
not have separate lines in your output file. The print
read files with Perl. In fact, the command for opening
operation does not add linefeeds whether it is writing to
a file for writing is almost the same as that for
the screen or to a file.
opening a file for reading. The command open(OUTFILE,
">myfile.out") or die "Cannot open myfile.out
When you are finished writing to a file or reading from a file,
for writing"; attempts to open the specified file for
you can close the file. The close command is very similar
writing and exits the script if this cannot be done. Changing
to the open command except that you only need to use the
the > symbol to the >> symbol appends any subsequent
file handle, and you do not need to specify the filename. For
output to the file instead of overwriting it.
example, if you want to close the file associated with the file
handle OUTFILE, you type the command close OUTFILE;
After a file is open for writing, you can use the print
and the file closes and is no longer available for reading.
command to write to it. While a command such as print
"Maybe we should go out\n"; prints to the screen,
If you are creating a temporary file and you want to remove
the same line with an additional argument ” print
it when you finish with it, you can use the unlink
OUTFILE "Maybe we should go out\n"; ” writes
command in place of the close command. The unlink
the line to the output file.
command removes the file from the file system.
USE A WHILE LOOP




„ Type pico write1 and press › Type while (<INFILE>) { Á Type chmod a+x followed ° Type ./write1 and press
Return. and press Return, type by a space. Return.
$counter++; and press
– Your script runs, creating
¤ Type #!/usr/bin/perl “w ‡ Type write1 and press
Return again, then type print
and press Return twice. Return. the output file.
OUTFILE "$counter: $_"; and
‹ Type open(INFILE, "<my press Return, and then type }.
file.txt"); and press Return, ˇ Save your script, and exit
then type open(OUTFILE, Pico.
">myfile.out"); and press
Return twice, then type
$counter=0; and press
Return twice.

260
15
WORK WITH PERL




For readability, your file handles should be
One of the advantages of using Perl
meaningful. If you use a temporary file, calling it
over other scripting languages is that
TMP or TMP1 makes it clear to anyone reading
it allows you to read and write any
your code that you do not intend to preserve the
number of files at the same time.
file. If you use temporary files, you must always
For each file that you want to use,
remember to unlink them when you are done;
you type an open command, specify
otherwise, your script may not run the next time
the filename, indicate whether you
you try to use it because it may not be able to
are opening the file for reading,
open a file that already exists. In addition, scripts
writing, appending, or both reading
that leave unnecessary files behind when they
and writing, assign a file handle, and
finish running create clutter in your file system.
write records to the file. When you
Giving temporary files random names, such as
are done, you type a close or an
myfile$$, reduces conflict over filenames.
unlink command.




USE A FOREACH LOOP




„ Type pico hrs2 and press ‹ Type foreach $day ( ˇ Type open(LOG,">>$log); ‡ Type a number, and press
Return. Mon,Tue,Wed,Thu,Fri ) { and and press Return, then type Return for each prompt.
press Return, and then type print LOG "$date: $tothrs
¤ Type $log="time.log"; ° Type cat time.log and
hours\n"; and press Return
print "hours worked on $day>
and press Return, then type press Return.
"; and press Return again. again, and then type close
$date=`date +%y%m%d`;
– Your script runs, appending
LOG; and save your file.
› Type $hrs = <STDIN>;
chomp($date); and press
the output to the output file.
Á Type perl hrs2 and press
Return, and then type and press Return, then type
– Your log file appears.
$tothrs=0; and press $tothrs = $tothrs + $hrs; and Return.
Return twice. press Return again, and then
type } and press Return again.


261
UNIX FOR MAC



INSTALL PERL MODULES
and examples of how the module can be used. After you

P
erl modules are collections of reusable code that can
click on the link that takes you to the page describing the
make your Perl programs more powerful and easier
module you want, you can scroll down the page to find a
to maintain by downloading and installing Perl
description of the module along with information on usage,
modules. Perl modules are meant to be used in other
debugging, parameters, and more.
programs using statements such as use Net::Telnet; that
make these procedures accessible in your own Perl scripts.
There are many Perl libraries and modules already installed
Perl modules are well designed and well documented and
on your system. You can list the installed modules with the
can save you a lot of programming effort.
command find/System/LibraryPerl “name "*.pm"
“print. This directory even includes a module for making
The best place to begin your search for a Perl module that
the process of downloading other Perl modules from CPAN
meets your needs is search.cpan.org. When you go to this
even easier. You can look at CPAM.pm or read about this on
site, you can enter a name into the search field if you know
the CPAN Web site to find out more.
the name of the module that you want or you can search by
category.

In the process of downloading your module, you should
take advantage of the online documentation available for
your module. This documentation will provide instructions



INSTALL PERL MODULES




„ In a browser Address ¤ Enter the name of a Perl
window, enter search.cpan.org module, such as Net::Telnet,
and press Return. in the search window and
click CPAN Search.




262
15
WORK WITH PERL




You must use the package command to
Before you can use a Perl module in your script,
create Perl modules, and you must organize
you must load it with the use command. The
them as subroutines, and name them with the
command use diagnostics; loads the module
file extension .pm. Construction of a module
diagnostics.pm from your /System/Library/Perl
also requires the use of commands to export
directory. The command use Net::Telnet;
items that you create in the module. For more
loads /System/Library/Perl/Net/Telnet.pm.
information about creating Perl modules, you
You can also build your own Perl modules. If you
can consult Perl: Your visual blueprint for
develop Perl code that may be used in many
building Perl scripts, by Paul Whitehead.
scripts or by many users, you may want to turn
the reusable code into a module and install it
into the /System/Library/Perl directory.




– Your search results appear. › Scroll down the page to ˇ Right-click the Source link
Note: You may have to scroll down to
find your match. read about the module, or in the page.
‹ Click the module name if print this page for later
more than one matches your reference.
request.


CONTINUED

263
UNIX FOR MAC



INSTALL PERL MODULES (CONTINUED)
source software from the same kind of file. However,

Y
ou can install Perl modules to increase your
because Perl is not compiled, the make step will not result

<<

. 53
( 69 .)



>>