📄 ch01_05.htm
字号:
<html><head><title>A Whirlwind Tour of Perl (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Learning Perl, 3rd Edition" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch01_04.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="ch01_06.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">1.5. A Whirlwind Tour of Perl</h2><p>So, you want to see a real Perl<a name="INDEX-102" />program with some meat? (If youdon't, just play along for now.) Here you are:</p><blockquote><pre class="code">#!/usr/bin/perl@lines = `perldoc -u -f atan2`;foreach (@lines) { s/\w<([^>]+)>/\U$1/g; print;}</pre></blockquote><p>Now, the first time you see Perl code like this, it can seem prettystrange. (In fact, every time you see Perl code like this, it canseem pretty strange.) But let's take it line by line, and seewhat this example does. (These explanations are very brief; this is awhirlwind tour, after all. We'll see all of thisprogram's features in more detail during the rest of this book.You're not really supposed to understand the whole thing untillater.)</p><p>The first line is the <tt class="literal">#!</tt><a name="INDEX-103" /> line, as we saw before. You might need tochange that line for your system, as we discussed earlier.</p><p>The second line runs an external command, named within<a name="INDEX-104" /><a name="INDEX-105" />backquotes ("<tt class="literal">``</tt>"). (The backquote key is often found next to thenumber 1 on full-sized American keyboards. Be sure not to confuse thebackquote with the single quote, "<tt class="literal">'</tt>".)The command we're using is <i class="command">perldoc -u -fatan2</i>; try typing that at your command line to see what itsoutput looks like. The<i class="command">perldoc</i><a name="INDEX-106" /> command is used on most systems to readand display the documentation for Perl and its associated extensionsand utilities, so it should normally be available.<a href="#FOOTNOTE-39">[39]</a>This command tells you something about the trigonometric function<tt class="literal">atan2</tt>; we're using it here just as anexample of an external command whose output we wish to process.</p><blockquote class="footnote"> <a name="FOOTNOTE-39" /><p>[39]If<i class="command">perldoc </i>is not available, that probably means thatyour system doesn't have a command-line interface, and yourPerl can't run commands (like <i class="command">perldoc</i>) inbackticks or via the piped-open, which we'll see in <a href="ch14_01.htm">Chapter 14, "Process Management"</a>. In that case, you should simply skip theexercises that use <i class="command">perldoc</i>.</p> </blockquote><p>The output of that command in the backticks is saved in an arrayvariable called <tt class="literal">@lines</tt><a name="INDEX-107" />. The next line of code starts aloop that will process each one of those lines. Inside the loop, thestatements are indented. Although Perl doesn't require this,good programmers do.</p><p>The first line inside the loop body is the scariest one; it says<tt class="literal">s/\w<([^>]+)>/\U$1/g;</tt>. Without going intotoo much detail, we'll just say that this can change any linethat has a special marker made with <a name="INDEX-108" /> <a name="INDEX-109" />angle brackets (<tt class="literal"><></tt>), and there should be at least one of those in theoutput of the <i class="command">perldoc</i> command.</p><p>The next line, in a surprise move, prints out each (possiblymodified) line. The resulting output should be similar to what<i class="command">perldoc -u -f atan2</i> would do on its own, but therewill be a change where any of those markers appears.</p><p>Thus, in the span of a few lines, we've run another program,saved its output in memory, updated the memory items, and printedthem out. This kind of program is a fairly common use of Perl, whereone type of data is converted to another.<a name="INDEX-110" /></p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch01_04.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch01_06.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">1.4. How Do I Make a Perl Program?</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">1.6. Exercises</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -