0491-0493.html

来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 437 行

HTML
437
字号




<HTML>

<HEAD>

<TITLE>Developer.com - Online Reference Library - 0672311739:RED HAT LINUX 2ND EDITION:Perl Programming</TITLE>

<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!-- ISBN=0672311739 //-->

<!-- TITLE=RED HAT LINUX 2ND EDITION //-->

<!-- AUTHOR=DAVID PITTS ET AL //-->

<!-- PUBLISHER=MACMILLAN //-->

<!-- IMPRINT=SAMS PUBLISHING //-->

<!-- PUBLICATION DATE=1998 //-->

<!-- CHAPTER=24 //-->

<!-- PAGES=0487-0498 //-->

<!-- UNASSIGNED1 //-->

<!-- UNASSIGNED2 //-->









<P><CENTER>

<a href="0487-0490.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0494-0497.html">Next</A>

</CENTER></P>



<A NAME="PAGENUM-491"><P>Page 491</P></A>











<H5><A NAME="ch24_ 8">

while

</A></H5>









<P>while performs a block of statements while a particular condition is true:

</P>



<!-- CODE SNIP //-->

<PRE>

while ($x&lt;10) {

     print &quot;$x\n&quot;;

     $x++;

     }

</PRE>

<!-- END CODE SNIP //-->









<H5><A NAME="ch24_ 9">

until

</A></H5>









<P>until is the exact opposite of the while statement. It will perform a block of statements

while a particular condition is false&#151;or, rather, until it becomes true:

</P>



<!-- CODE SNIP //-->

<PRE>

until ($x&gt;10) {

     print &quot;$x\n&quot;;

     $x++;

     }

</PRE>

<!-- END CODE SNIP //-->









<H3><A NAME="ch24_ 10">

Regular Expressions

</A></H3>









<P>Perl's greatest strength is in text and file manipulation. This is accomplished by using the

regular expression (regex) library. Regexes allow complicated pattern matching and replacement

to be done efficiently and easily.

</P>









<P>For example, the following one line of code will replace every occurrence of the string

Bob or the string Mary with Fred in a line of text:

</P>



<!-- CODE SNIP //-->

<PRE>

$string =~ s/bob|mary/fred/gi;

</PRE>

<!-- END CODE SNIP //-->









<P>Without going into too many of the gory details, Table 24.1 explains what the preceding

line says.

</P>









<P>Table 24.1. Explanation of $string =~

s/bob|mary/fred/gi;.

</P>



<HR>

<TABLE WIDTH="360">

<TR><TD>

Element

</TD><TD>

Explanation

</TD></TR>



<TR><TD>

$string =~

</TD><TD>

Performs this pattern match on the text found in the variable

called $string.

</TD></TR>



<TR><TD>

s

</TD><TD>

Substitute.

</TD></TR>



<TR><TD>

/

</TD><TD>

Begins the text to be matched.

</TD></TR>



<TR><TD>

bob|mary

</TD><TD>

Matches the text bob or mary. You should remember that it is

looking for the text mary, not the word mary; that is, it will also match the

text mary in the word maryland.

</TD></TR>



<TR><TD>

/

</TD><TD>

Ends text to be matched, begin text to replace it with.

</TD></TR>



<TR><TD>

fred

</TD><TD>

Replaces anything that was matched with the text

fred.

</TD></TR>

</TABLE>



<!-- CODE SNIP //-->

<PRE>

                                                               continues

</PRE>

<!-- END CODE SNIP //-->





<A NAME="PAGENUM-492"><P>Page 492</P></A>



<BR>



<P>

Table 24.1. continued

</P>



<HR>

<TABLE WIDTH="360">

<TR><TD>

Element

</TD><TD>

Explanation

</TD></TR>



<TR><TD>

/

</TD><TD>

Ends replace text.

</TD></TR>



<TR><TD>

g

</TD><TD>

Does this substitution globally; that is, wherever in the string

you match the match text (and any number of times), replaces it.

</TD></TR>



<TR><TD>

i

</TD><TD>

The search text is case-insensitive. It will match

bob, Bob, or bOB.

</TD><TR>



<TR><TD>

;

</TD><TD>

Indicates the end of the line of code.

</TD></TR>

</TABLE>









<P>If you are interested in the gory details, I recommend the book

Mastering Regular Expressions by Jeffrey Friedl, which explains regular expressions from the ground up, going into all

the theory behind them and explaining the best ways to use them.

</P>









<P>Although replacing one string with another might seem like a rather trivial task, the code

required to do the same thing in another language, for example, C, is rather

daunting.

</P>









<H3><A NAME="ch24_ 11">

Access to the Shell

</A></H3>









<P>Perl is useful for administrative functions because, for one thing, it has access to the shell.

This means that any process that you might ordinarily do by typing commands to the shell, Perl

can do for you. This is done with the `` syntax; for example, the following code will print a

directory listing:

</P>



<!-- CODE //-->

<PRE>

$curr_dir = `pwd`;

@listing = `ls -la`;

print &quot;Listing for $curr_dir\n&quot;;

foreach $file (@listing) {

     print &quot;$file&quot;;

     }



<!-- END CODE //-->

</PRE>



<CENTER>

<TABLE BGCOLOR="#FFFF99">

<TR><TD><B>

NOTE

</B></TD></TR>

<TR><TD>

<BLOCKQUOTE>

The `` notation uses the backtick found above the Tab key, not the single quote.

</BLOCKQUOTE></TD></TR>

</TABLE></CENTER>

</P>



<P>Access to the command line is fairly common in shell scripting languages, but is less

common in higher level programming languages.

</P>









<H3><A NAME="ch24_ 12">

Command-Line Mode

</A></H3>









<P>In addition to writing programs, Perl can be used from the command line like any other

shell scripting language. This enables you to cobble together Perl utilities on-the-fly, rather

than having to create a file and execute it.

</P>





<A NAME="PAGENUM-493"><P>Page 493</P></A>











<P>For example, running the following command line will run through the file

foo and replace every occurrence of the string Joe with

Harry, saving a backup copy of the file at foo.bak:

</P>



<!-- CODE SNIP //-->

<PRE>

perl -p -i.bak -e s/Joe/Harry/g foo

</PRE>

<!-- END CODE SNIP //-->









<P>The -p switch causes Perl to perform the command for all files listed (in this case, just one file).

</P>









<P>The -i switch indicates that the file specified is to be edited in place, and the original

backed up with the extension specified. If no extension is supplied, no backup copy is made.

</P>









<P>The -e switch indicates that what follows is one or more lines of a

script.

</P>









<H3><A NAME="ch24_ 13">

Automation Using Perl

</A></H3>









<P>Perl is great for automating some of the tasks involved in maintaining and administering a

UNIX machine. Because of its text manipulation abilities and its access to the shell, Perl can be

used to do any of the processes that you might ordinarily do by hand.

</P>









<P>The following sections present examples of Perl programs that you might use in the

daily maintenance of your machine.

</P>









<H4><A NAME="ch24_ 14">





Moving Files

</A></H4>









<P>One aspect of my job is administering a secure FTP site. Incoming files are placed in an

&quot;incoming&quot; directory. When they have been checked, they are moved to a &quot;private&quot; directory

for retrieval. Permissions are set in such a way that the file is not shown in a directory listing,

but can be retrieved if the filename is known. The person who placed the file on the server is

informed via e-mail that the file is now available for download.

</P>









<P>I quickly discovered that people were having difficulty retrieving files because they

incorrectly typed the case of filenames. This was solved by making the file available with an

all-uppercase name and an all-lowercase name, in addition to the original filename.

</P>









<P>I wrote the Perl program in Listing 24.2 to perform all those tasks with a single

command. When I have determined that a file is to go onto the FTP site, I simply type

move filename user, where filename is the name of the file to be moved, and

user is the e-mail address of the person to be notified.

</P>









<P>Listing 24.2. Moving files on an FTP site.

</P>



<!-- CODE //-->

<PRE>

 1: #!/usr/bin/perl

 2: #

 3: #  Move a file from /incoming to /private

 4: $file = @ARGV[0];

 5: $user = @ARGV[1];

 6:

 7: if ($user eq &quot;&quot;) {&amp;usage}

 8: else {

</PRE>

<!-- CODE SNIP //-->



<PRE>

                                         continues

</PRE>

<!-- END CODE SNIP //-->





<P><CENTER>

<a href="0487-0490.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0494-0497.html">Next</A>

</CENTER></P>









</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?