ch01_04.htm

来自「by Randal L. Schwartz and Tom Phoenix I」· HTM 代码 · 共 361 行 · 第 1/2 页

HTM
361
字号
<html><head><title>How Do I Make a Perl Program? (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 &amp; 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_03.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_05.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">1.4. How Do I Make a Perl Program?</h2><p><a name="INDEX-61" />It'sabout time you asked (even if you didn't). Perl programs aretext files; you can create and edit them with your favorite texteditor. (You don't need any special development environment,although there are some commercial ones available from variousvendors. We've never used any of these enough to recommendthem.)</p><p>You should generally use a programmers'<a name="INDEX-62" /><a name="INDEX-63" />text editor, rather than an ordinaryeditor. What's the difference? Well, a programmers' texteditor will let you do things that programmers need, like to indentor unindent a block of code, or to find the matching closing curlybrace for a given opening curly brace. On Unix systems, the two mostpopular programmers' editors are<i class="command">emacs</i><a name="INDEX-64" /> and<i class="command">vi</i><a name="INDEX-65" /> (and their variants and clones). Both ofthese have been ported to several non-Unix systems, and many systemstoday offer a graphical editor (which uses a pointing device like amouse). In fact, there are even versions of <i class="command">vi</i> and<i class="command">emacs</i> that offer a graphical interface. Ask yourlocal expert about text editors on your system.</p><p>For the simple programs you'll be writing for the exercises inthis book, none of which will need to be more than about twenty orthirty lines of code, any text editor will be fine.</p><p>A few beginners try to use a <a name="INDEX-66" />word processorinstead of a text editor. We recommend against this -- it'sinconvenient at best and impossible at worst. But we won't tryto stop you. Be sure to tell the word processor to save your file as"text only"; the word processor's own format willalmost certainly be unusable.</p><p>In some cases, you may need to compose the program on one machine,then transfer it to another to be run. If you do this, be sure thatthe transfer uses "<a name="INDEX-67" />text" or"<a name="INDEX-68" />ASCII" mode, and not"<a name="INDEX-69" />binary" mode. This step is neededbecause of the different text formats on different machines. Withoutthat, you may get inconsistent results -- some versions of Perlactually abort when they detect a mismatch in the line endings.</p><a name="lperl3-CHP-1-SECT-4.1" /><div class="sect2"><h3 class="sect2">1.4.1. A Simple Program</h3><p>According to the oldest rule in the book, any book about a computerlanguage that has Unix-like roots has to start with showing the<a name="INDEX-70" />"Hello, world" program. So,here it is in Perl:</p><blockquote><pre class="code">#!/usr/bin/perlprint "Hello, world!\n";</pre></blockquote><p>Let's imagine that you've typed that into your texteditor. (Don't worry yet about what the parts mean and how itworks. We'll see about those in a moment.) You can generallysave that program under any name you wish. Perl doesn't requireany special kind of <a name="INDEX-71" />filename or extension, and it'sbetter to use no <a name="INDEX-72" />extension at all.<a href="#FOOTNOTE-31">[31]</a> But some non-Unix systems may require anextension like <em class="emphasis">.plx</em><a name="INDEX-73" /> (meaning PerL eXecutable); see yoursystem's release notes for more information.</p><blockquote class="footnote"> <a name="FOOTNOTE-31" /><p>[31]Whyis it better to have no extension? Imagine that you've writtena program to calculate bowling scores and you've told all ofyour friends that it's called <i class="command">bowling.plx</i>.One day you decide to rewrite it in C. Do you still call it by thesame name, implying that it's still written in Perl? Or do youtell everyone that it has a new name? (And don't call it<i class="command">bowling.c</i>, please!) The answer is that it'snone of their business what language it's written in, ifthey're merely <em class="emphasis">using</em> it. So it should havesimply been called <i class="command">bowling</i> in the firstplace.</p> </blockquote><p>You will also need to do something so that your system knows thatit's an executable program (that is, a command). Whatyou'll do depends upon your system; maybe you won't haveto do anything more than to save the program in a certain place.(Your current directory will generally be fine.) On Unix systems, youmark a program as being executable by using the<i class="command">chmod</i><a name="INDEX-74" /> command, perhaps like this:</p><blockquote><pre class="code">$ <tt class="userinput"><b>chmod a+x my_program</b></tt></pre></blockquote><p>The dollar sign (and space) at the start of the line represents theshell prompt, which will probably look different on your system. Ifyou're used to using <i class="command">chmod</i> with a numberlike <tt class="literal">755</tt> instead of a symbolic parameter like<tt class="literal">a+x</tt>, that's fine too, of course. Either way,it tells the system that this file is now a program.</p><p>Now you're ready to run it:</p><blockquote><pre class="code">$ <tt class="userinput"><b>./my_program</b></tt></pre></blockquote><p>The <tt class="userinput"></tt><a name="INDEX-75" /> <a name="INDEX-76" /> <a name="INDEX-77" />dot and slash at the start of this commandmean to find the program in the current working directory.That's not needed in all cases, but you should use it at thestart of each command invocation until you fully understand whatit's doing.<a href="#FOOTNOTE-32">[32]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-32" /><p>[32]In short, it's preventingyour shell from running another program (or shell builtin) of thesame name. A common mistake among beginners is to name their firstprogram <tt class="literal">test</tt>. Many systems already have a program(or shell builtin) with that name; that's what the beginnersrun instead of their program.</p> </blockquote><p>If everything worked, it's a miracle. More often, you'llfind that your program has a bug. Edit and try again -- but youdon't need to use <i class="command">chmod</i> each time, sincethat should "stick" to the file. (Of course, if the bugis that you didn't use <i class="command">chmod</i> correctly,you'll probably get a "permission denied" messagefrom your shell.)</p></div><a name="lperl3-CHP-1-SECT-4.2" /><div class="sect2"><h3 class="sect2">1.4.2. What's Inside That Program?</h3><p>Like other "free-form" languages, Perl generally lets youuse insignificant<a name="INDEX-78" />whitespace (likespaces, tabs, and newlines) at will to make your program easier toread. Most Perl programs use a fairly standard format, though, muchlike most of what we show here. We strongly encourage you to properlyindent your programs, since that makes your program easier to read; agood text editor will do most of the work for you. Good<a name="INDEX-79" />comments also makea program easier to read. In Perl, comments run from a<a name="INDEX-80" /><a name="INDEX-81" />pound sign (<tt class="literal">#</tt>) tothe end of the line. (There are no "<a name="INDEX-82" />block comments" inPerl.<a href="#FOOTNOTE-33">[33]</a>) We don't use many comments in the programs in thisbook, because the surrounding text explains their workings, but youshould use comments as needed in your own programs.</p><blockquote class="footnote"> <a name="FOOTNOTE-33" /><p>[33]But there are a number of ways to fake them.See the FAQ (accessible with <em class="emphasis">perldoc</em><em class="emphasis">perlfaq</em> on most installations).</p></blockquote><p>So another way (a very strange way, it must be said) to write thatsame "Hello, world" program might be like this:</p><blockquote><pre class="code">#!/usr/bin/perl    print    # This is a comment"Hello, world!\n"  ;    # Don't write your Perl code like this!</pre></blockquote><p>That first line is actually a very special comment. On Unixsystems,<a href="#FOOTNOTE-34">[34]</a> if the very first twocharacters on the first line of a text file are"<tt class="literal">#!</tt><a name="INDEX-83" />", then what follows is the name ofthe program that actually executes the rest of the file. In thiscase, the program is stored in the file<em class="filename">/usr/bin/perl</em>.</p><blockquote class="footnote"> <a name="FOOTNOTE-34" /><p>[34]Most modern ones, anyway. The

⌨️ 快捷键说明

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