📄 0494-0497.html
字号:
<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="0491-0493.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0498-0498.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-494"><P>Page 494</P></A>
<P>Listing 24.2. continued
</P>
<!-- CODE //-->
<PRE>
9: if (-e "/home/ftp/incoming/$file")
10: {`cp /home/ftp/incoming/$file /home/ftp/private/$file`;
11: chmod 0644, "/home/ftp/private/$file";
12: `rm -f /home/ftp/incoming/$file`;
13: if (uc($file) ne $file) {
14: $ucfile = uc($file);
15: `ln /home/ftp/private/$file /home/ftp/private/$ucfile`;
16: }
17: if (lc($file) ne $file) {
18: $lcfile = lc($file);
19: `ln /home/ftp/private/$file /home/ftp/private/$lcfile`;
20: }
21:
22: # Send mail
23: open (MAIL, "| /usr/sbin/sendmail -t ftpadmin,$user");
24: print MAIL <<EndMail;
25: To: ftpadmin,$user
26: From: ftpadmin
27: Subject: File ($file) moved
28:
29: The file $file has been moved
30: The file is now available as
31: ftp://ftp.databeam.com/private/$file
32:
33: ftpadmin\@databeam.com
34: =================================
35: EndMail
36: close MAIL;
37: }
38:
39: else { # File does not exist
40: print "File does not exist!\n";
41: } # End else (-e $file)
42:
43: } # End else ($user eq "")
44:
45: sub usage {
46: print "move <filename> <username>\n";
47: print "where <username> is the user that you are moving this for.\n\n";
48: }
</PRE>
<!-- END CODE //-->
<P>Without going through Listing 24.2 line-by-line, the following paragraphs take a look at
some of the high points that demonstrate the power and syntax of Perl.
</P>
<P>In lines 4_5, the array @ARGV contains all command-line arguments. The place where one
argument ends and another begins is taken to be every space, unless arguments are given in quotes.
</P>
<P>In line 9, the -e file test tests for the existence of a file. If the file does
not exist, perhaps the user gave me the wrong filename, or one of the other server administrators beat me to it.
</P>
<A NAME="PAGENUM-495"><P>Page 495</P></A>
<P>Perl enables you to open a pipe to some other process and print data to it. This allows Perl
to "use" any other program that has an interactive user interface, such as
sendmail, or an FTP session. That's the purpose of line 23.
</P>
<P>The << syntax allows you to print multiple lines of text until the
EOF string is encountered. This eliminates the necessity to have multiple
print commands following one another—for example,
</P>
<!-- CODE SNIP //-->
<PRE>
24: print MAIL <<EndMail;
...
35: EndMail
</PRE>
<!-- END CODE SNIP //-->
<P>The subroutine syntax allows modularization of code into functions. Subroutines are
declared with the syntax shown in lines 45_48, and called with the
& notation, as shown in line 7:
</P>
<!-- CODE SNIP //-->
<PRE>
7: ... {&usage}
...
45: sub usage {
...
48: }
</PRE>
<!-- END CODE SNIP //-->
<H4><A NAME="ch24_ 15">
Purging Logs
</A></H4>
<P>Many programs maintain some variety of logs. Often, much of the information in the logs
is redundant or just useless. The program shown in Listing 24.3 removes all lines from a file
that contain a particular word or phrase, so lines that you know are not important can be purged.
</P>
<P>Listing 24.3. Purging log files.
</P>
<!-- CODE //-->
<PRE>
1: #!/usr/bin/perl
2: #
3: # Be careful using this program!!
4: # This will remove all lines that contain a given word
5: #
6: # Usage: remove <word> <file>
7: ###########
8: $word=@ARGV[0];
9: $file=@ARGV[1];
10:
11: unless ($file) {
12: print "Usage: remove <word> <file>\n"; }
13:
14: else {
15: open (FILE, "$file");
16: @lines=<FILE>;
17: close FILE;
18:
19: # remove the offending lines
20: @lines = grep (!/$word/, @lines);
21:
22: # Write it back
23: open (NEWFILE, ">$file");
24: for (@lines) { print NEWFILE }
25: close NEWFILE;
26: } # End else
</PRE>
<!-- END CODE //-->
<A NAME="PAGENUM-496"><P>Page 496</P></A>
<P>Listing 24.3 is fairly self-explanatory. It reads in the file and then removes the offending
lines using Perl's grep command, which is similar to the standard UNIX
grep. If you save this as a file called remove and place it in your path, you will have a swift way to purge server logs
of unwanted messages.
</P>
<H4><A NAME="ch24_ 16">
Posting to Usenet
</A></H4>
<P>If some portion of your job requires periodic postings to Usenet—a FAQ listing, for
example—the following Perl program can automate the process for you. In the sample code, the text
that is posted is read in from a text file, but your input can come from anywhere.
</P>
<P>The program shown in Listing 24.4 uses the
Net::NNTP module, which is a standard part of the Perl distribution.
</P>
<P>Listing 24.4. Posting an article to Usenet.
</P>
<!-- CODE //-->
<PRE>
1: #!/usr/bin/perl
2: open (POST, "post.file");
3: @post = <POST>;
4: close POST;
5: use Net::NNTP;
6:
7: $NNTPhost = `news';
8:
9: $nntp = Net::NNTP->new($NNTPhost)
10: or die "Cannot contact $NNTPhost: $!";
11:
12: # $nntp->debug(1);
13: $nntp->post()
14: or die "Could not post article: $!";
15: $nntp->datasend("Newsgroups: news.announce\n");
16: $nntp->datasend("Subject: FAQ - Frequently Asked Questions\n");
17: $nntp->datasend("From: ADMIN <root\@rcbowen.com>\n");
18: $nntp->datasend("\n\n");
19: for (@post) {
20: $nntp->datasend($_);
21: }
22:
23: $nntp->quit;
</PRE>
<!-- END CODE //-->
<H3><A NAME="ch24_ 17">
For More Information
</A></H3>
<P>The Perl community is large and growing. Since the advent of the WWW, Perl has
become the most popular language for Common Gateway Interface (CGI) programming. There is
a wealth of sources of information on Perl. Some of the better ones are listed here. The
following books are good resources:
</P>
<UL>
<LI> Programming
Perl, Second Edition, by Larry Wall, Randall Schwartz, and
Tom Christiansen (O'Reilly & Associates)
</UL>
<A NAME="PAGENUM-497"><P>Page 497</P></A>
<UL>
<LI> Learning
Perl by Randall Schwartz (O'Reilly & Associates)
<LI> Mastering Regular Expressions
by Jeffrey Friedl (O'Reilly & Associates)
<LI> CGI Programming with Perl, Visual Basic, and
C by Rich Bowen, et al. (Web Publishing & Programming Resource
Kit, Volume 4) (Sams.net Publishing)
</UL>
<P>On Usenet, check out the following:
</P>
<UL>
<LI> comp.lang.perl.misc
Discusses various aspects of the Perl programming language. Make sure that
your questions are Perl-specific, not generic CGI questions, because the regulars tend
to flame folks who can't tell the difference.
<LI> comp.infosystems.www.authoring.cgi
Discusses authoring of CGI programs, so much of the discussion is
Perl-specific. Make sure that your questions are related to CGI, not just Perl. The regulars are
very particular about staying on topic.
</UL>
<P>Check these sites on the World Wide Web:
</P>
<UL>
<LI> <a href="http://www.perl.com/">http://www.perl.com/</A>
The Perl Language home page, maintained by Tom Christiansen. This is the place
to find all sorts of information about Perl, from its history to culture to helpful tips.
This is also the place to download the Perl interpreter for your system.
<LI> <a href="http://www.perl.com/cpan">http://www.perl.com/CPAN</A>
Yes, this is part of the site just mentioned, but it merits its own mention. CPAN
(the Comprehensive Perl Archive Network) is the place for you to find modules
and programs in Perl. Also, if you end up writing something in Perl that you think
is particularly useful, this would be the place to make it available to the Perl community.
<LI> <a href="http://www.perl.org/">http://www.perl.org/</A>
The Perl Institute. A nonprofit organization dedicated to the advancement of
Perl.
</UL>
<H3><A NAME="ch24_ 18">
Summary
</A></H3>
<P>Perl, in the words of its creator, Larry Wall, "combines the best elements of C,
sed, awk, and sh," but is also a great language for folks who have no experience
with these languages.
</P>
<P>Perl's powerful regex library and ease of use have made it one of the preferred scripting
languages in use today, particularly in the realm of CGI programming. Many people even
think of Perl as exclusively a CGI language, when, in fact, it is capable of so much more.
</P>
<P>Although this book is focused on Red Hat Linux, Perl is also available for many other
platforms, and scripts that you write in Perl on one platform will run without changes on another.
</P>
<P><CENTER>
<a href="0491-0493.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0498-0498.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -