<<

. 52
( 69 .)



>>

learning the language, as they can help you to
spot problems that prevent your script from
running, or represent poor scripting style.




– Pico opens with your file. – Perl runs your script.
° Save your file, and exit · Type ./sam.pl and press
Pico. Return.
‡ Type the line
– Pico saves your modified
#!/usr/bin/perl at the top of
your script, and press Return. script.



253
UNIX FOR MAC




MANIPULATE TEXT WITH PERL
include as characters you want to match with an escape

Y
ou can use the ability of Perl to recognize patterns to
character. The escape character tells Perl to take the
help you manipulate text. If you are able to describe
following character literally and not to interpret it as part of
a pattern that you are looking for in a stream of text,
the Perl syntax. Thus, \/ represents / in your search pattern.
then you can represent it as a Perl regular expression with
which you can easily extract and manipulate it.
Perl has many special character sequences, such as \d,
to help you to locate and manipulate text. Users refer to
For example, if you are looking for dates that look like
these character sequences as metacharacters or escape
05/01/03, you can tell Perl that you want strings that match
sequences. Many metacharacters have both a positive and
the pattern \d{2}\/\d{2}\/\d{2}. While this expression
negative form. For example, while \d represents a digit, \D
may look complicated, a quick breakdown makes it easier
represents a non-digit ” any character that is not a digit.
to understand. The string \d matches a digit, and when
While \s+ represents any amount of whitespace, \S+
changed to \d{2}, it matches a two-digit number. The date
represents any string of characters that does not contain any
above includes three of these strings. The two extra forward
whitespace. The biggest challenge to most Perl beginners is
slashes in the search pattern indicate that the strings you
to remember what each of the metacharacters represents.
are searching for contain slashes. Because slashes are a part
of the Perl syntax, you must precede any slashes that you



MANIPULATE TEXT WITH PERL




– Pico opens so that you can ‹ Type while ( <> ) { and
SEARCH A FILE FOR DATES
edit your file. press Return, then type if (
„ Type pico finddates.pl and
/\d{2}\/\d{2}\/\d{2}/ ) { and
¤ Type #!/usr/bin/perl “w
press Return.
press Return.
and press Return twice.
› Type print $_; and press
Return, type } and press
Return, and then type }
and press Return.
254
15
WORK WITH PERL



Perl recognizes many different metacharacters, shows some of the most common metacharacters
allowing you to closely describe text that you are and what they represent. You can use the + and *
looking for and to manipulate it. The table below qualifiers with any of the metacharacters.

METACHARACTER MATCHES METACHARACTER MATCHES
A digit. Any amount of whitespace.
\d \s+
One or more digits.
\d+ Some whitespace or no
\S*
whitespace.
Zero or more digits.
\d*
A tab.
\t
A non-digit.
\D
An uppercase character.
\u
The escape character.
\e
A form feed.
\f A word character ” a letter,
\w
digit, or underscore.
A lowercase letter.
\l
A non-word character.
\W
A newline.
\n
An octal character.
A carriage return. \O?
\r
A whitespace character.
\s A hexadecimal character.
\x?




ˇ Save your file, and exit Á Type chmod a+x ° Type the name of a file
Pico. finddates.pl and press Return. containing dates, and press
Return.
‡ Type ./finddates.pl
– Your Perl script displays all
followed by a space.
lines that contain dates in the
specified form.


255
UNIX FOR MAC




READ THE COMMAND LINE
$tothrs=0;

Y
ou can build Perl scripts that read data from the
foreach day ( Mon,Tue,Wed,Thu,Fri ) {
command line. Like other scripting languages, Perl
print "hours worked on $day> ";
allows you to prompt the user to enter information.
$hrs = <STDIN>;
Perl does this by issuing a print statement containing the
$tothrs = $tothrs + $hrs;
prompt and then reading the answer from standard input.
}
For example, you can include a prompt in the script with
print "You only worked $tothrs hours!";
the line print "Enter your favorite number> ";
and then collect the answer with $favnum=<STDIN>;. This script prompts the user to enter the number of
When the script runs, Perl prompts you for the number hours that they work each weekday, and computes a total.
and stores it in $favnum. However, this script generates an error message if the user
enters anything other than a number, because the addition
You can also place your prompt-and-read statement into
fails. You can insert a while statement inside the foreach
a loop. A while statement continues looping until the
statement to repeat the prompt until the user enters a
specified conditions are met. A foreach loop is good to use
number.
if you want to collect a specific number of answers from the
user. For example, you can enter the following commands:



READ THE COMMAND LINE




„ Type pico favnum.pl and ¤ Type #!/usr/bin/perl “w › Type print "What a
press Return. and press Return twice, then coincidence! $favnum is my
type print "Enter your favorite favorite number too.\n"; and
number> "; and press Return. press Return.
‹ Type $favnum = <STDIN>; ˇ Type print "Maybe we
and press Return. should go out.\n";.


256
15
WORK WITH PERL


There are times when you do not want to use data exactly as a user
enters it. For example, when you tell the user that their favorite number
just happens to be your favorite number, too, as in the favnum.pl script,
your message displays on two lines instead of three. This unintended line
break results when Perl reads the input line, including the linefeed, and
retains the linefeed. As a result, when the output displays to the user, the
linefeed is still present in the $favnum variable.
You can remove these unwanted linefeeds by using the chomp command.
The chomp command simply removes the linefeed from the end of the
line. The format for the chomp command is chomp($variable). Thus,
you can add the line chomp($favnum); to the script immediately after
the line that reads the response from the user. When the chomp
command removes the linefeed, the output changes to reflect this.
AFTER:
BEFORE:

What a coincidence!
What a coincidence! 111
111 is my favorite
is my favorite number too. number too.
Maybe we should go out. Maybe we should go
out.




Á Save your script, and exit ‡ Type chmod a+x followed · Type ./favnum.pl and press
Pico. by a space. Return.
– The script runs, asking you
° Type favnum.pl and press
Return. for your favorite number.




257
UNIX FOR MAC




READ FILES WITH PERL
is good practice if you want to ensure that the files that you

Y
ou can read any number of files from within a Perl
read are preserved, as it opens the file only for reading, and
script. There are also a number of ways to read files in
disallows any write operations.
Perl, the most straightforward being to use the open
command with the name of the file as an argument. For
Just as the < symbol indicates that a command is opening a
example, to open the file myfile.txt, you can use the
file for reading, the > symbol indicates that a command is
command open(INFILE,"myfile.txt");. This command
opening a file for writing. Using the > symbol indicates that
opens the file and associates it with the file handle INFILE.
a command is opening a file for appending. In other words,
You can also assign the name of the file to a variable
you write from the end of the file, preserving the current
using a command such as $myfile="/Users/user/
contents of the file.
myfile.txt"; and then open the file with an open
command such as open(INFILE,$myfile);. If you assign To be sure that your scripts do not malfunction when input
the filename at the beginning of the script, your script is files do not exist or are unreadable, you can add an or
easier to modify later. die clause to your open command. The command
open(INFILE,"<myfile.txt") or die "Cannot
You can also open a file explicitly for reading by using a <
open myfile.txt for reading"; displays the
symbol within your open command. For example, you can
cautionary message if the input file is unreadable, and exits
use the command open(INFILE,"<myfile.txt");. This
the script.

READ FILES WITH PERL




„ Type pico read1 and press ¤ Type #!/usr/bin/perl “w › Type while (<INFILE>) {
Return. and press Return twice. and press Return.

<<

. 52
( 69 .)



>>