⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch03_01.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>The Perl Executable (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><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="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><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="part2.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch03_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h1 class="chapter">Chapter 3. The Perl Executable</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4>  <p> <a href="#perlnut2-CHP-3-SECT-1">Command Processing</a><br /><a href="ch03_02.htm">Command-Line Options</a><br /><a href="ch03_03.htm">Environment Variables</a><br /><a href="ch03_04.htm">The Perl Compiler</a><br /><a href="ch03_05.htm">Threads</a><br /></p></div><p>The <em class="emphasis">perl</em><a name="INDEX-136" /></a> executable is normally installed in<em class="emphasis">/usr/bin</em> or <em class="emphasis">/usr/local/bin</em>on your machine. Some people often refer to <em class="emphasis">perl</em>as the Perl interpreter, but this isn't strictlycorrect, as you'll learn shortly.</p><p>Every Perl program must be passed through the Perl executable to beexecuted. <a name="INDEX-137" /></a><a name="INDEX-138" /></a>The first line in many Perlprograms is something like:</p><blockquote><pre class="code">#!/usr/bin/perl</pre></blockquote><p><a name="INDEX-139" /></a><a name="INDEX-140" /></a><a name="INDEX-141" /></a><a name="INDEX-142" /></a><a name="INDEX-143" /></a>For Unix systems, this<tt class="literal">#!</tt> (hash-bang or shebang) line tells the shell tolook for the <em class="emphasis">/usr/bin/perl</em> program and pass therest of the file to that <em class="emphasis">/usr/bin/perl</em> forexecution. Sometimes, you'll see different pathnamesto the Perl executable, such as<em class="emphasis">/usr/local/bin/perl</em>. You might see<em class="emphasis">perl5</em> or <em class="emphasis">perl6</em> instead of<em class="emphasis">perl</em> on sites that still depend on olderversions of Perl.</p><p>Often, you'll see command-line options tacked on theend of <em class="emphasis">perl</em>, such as the notorious<em class="emphasis">-w</em> switch, which produces warning messages. Butalmost all Perl programs on Unix start with some variation of<tt class="literal">#!/usr/bin/perl</tt>.</p><p>If you get a mysterious <a name="INDEX-144" /></a>"Command notfound" error on a Perl program,it's often because the path to the Perl executableis wrong. When you download Perl programs off the Internet, copy themfrom one machine to another, or copy them out of a book (like thisone!). The first thing you should do is make sure that the<tt class="literal">#!</tt> line points to the location of the Perlexecutable on your system. If you're on a Win32platform, where the shebang path is used only to check for Perlswitches, you should make sure that you run<em class="emphasis">pl2bat.bat</em><a name="INDEX-145" /></a><a name="INDEX-146" /></a> on theprogram so you can run it directly from the command line.</p><p>So what does the Perl executable do? It compiles the programinternally into a parse tree and executes it immediately. Because theprogram is not compiled and executed in separate steps, Perl iscommonly known as an interpreted language, but this is not quitetrue.<a href="#FOOTNOTE-5">[5]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-5" /></a><p> [5]Unlike strictly compiled languages, thecompiled form of a Perl program is not stored as a separate file.However, Versions 5.6 and later give you the option of using astandalone Perl compiler that creates bytecode to be executedseparately. We'll say more about the compiler laterin this chapter.</p> </blockquote><p>So do you call something a Perl"script" or a Perl"program"? Typically, the word"program" is used to describesomething that needs to be compiled into assembler or bytecode beforeexecuting, as in the C language. The word"script" is used to describesomething that runs through an executable on your system, such as theBourne shell. For Perl, you can use either phrase and only offendthose Perl programmers who care about semantics more than you do.</p><p>What does all this mean for you? When you write a Perl program, youcan just give it a correct <tt class="literal">#!</tt> line at the top ofthe script, make it executable with <tt class="literal">chmod +x</tt>, andrun it. For 95% of Perl programmers in this world,that's all they care about.</p><div class="sect1"><a name="perlnut2-CHP-3-SECT-1" /></a><h2 class="sect1">3.1. Command Processing</h2><p><a name="INDEX-147" /></a><a name="INDEX-148" /></a><a name="INDEX-149" /></a>In addition to specifyinga <tt class="literal">#!</tt> line, you can specify a short script directlyon the command line. Here are some of the possible ways to run Perl:</p><ul><li><p>Issue the <em class="emphasis">perl</em> command, writing your script lineby line via <em class="emphasis">-e</em> switches on the command line:</p><blockquote><pre class="code">perl -e 'print "Hello, world\n"'    # Unixperl -e "print \"Hello, world\n\""  # Win32 or Unixperl -e "print qq[Hello, world\n]"  # Also Win32</pre></blockquote></li><li><p>Issue the <em class="emphasis">perl</em> command, passing Perl the name ofyour script as the first parameter (after any switches):</p><blockquote><pre class="code">perl testpgm</pre></blockquote></li><li><p><a name="INDEX-150" /></a>On Unix systems that support the<tt class="literal">#!</tt> notation, specify the Perl command on the<tt class="literal">#!</tt> line, make your script executable, and invokeit from the shell (as described above).</p></li><li><p>Pass your script to Perl via standard input. For example, under Unix: </p><blockquote><pre class="code">echo "print 'Hello, world'" | perl -% perlprint "Hello, world\n";^D</pre></blockquote></li><li><p><a name="INDEX-151" /></a>On Win32 systems, you can associatean extension (e.g., <em class="emphasis">.plx</em>) with a file type anddouble-click on the icon for a Perl script with that file type. Or,as mentioned earlier, do this:</p><blockquote><pre class="code">(open a "DOS" window)C:\&gt; (edit your Perl program in your favorite editor)C:\&gt; pl2bat yourprog.plxC:\&gt; .\yourprog.bat(program output here)</pre></blockquote><p>If you are using the ActiveState version of Win32 Perl, the installernormally prompts you to create the association.</p></li><li><p>On Win32 systems, if you double-click on the icon for the Perlexecutable, you'll find yourself in a command-promptwindow with a blinking cursor. You can enter your Perl commands,indicating the end of your input with Ctrl-Z, and Perl will compileand execute your script.</p></li></ul><p>Perl parses the input file from the beginning, unlessyou've specified the <em class="emphasis">-x</em> switch(see <a href="ch03_02.htm#perlnut2-CHP-3-SECT-2">Section 3.2, "Command-Line Options"</a> laterin this chapter). If there is a <tt class="literal">#!</tt> line, it isalways examined for switches as the line is being parsed. Thus,switches behave consistently regardless of how Perl was invoked.</p><p>After locating your script, Perl compiles the entire script into aninternal form. If there are any compilation errors, execution of thescript is not attempted. If the script is syntactically correct, itis executed. If the script runs off the end without hitting an<tt class="literal">exit</tt> or <tt class="literal">die</tt> operator, animplicit <tt class="literal">exit(0)</tt> is provided to indicatesuccessful completion. <a name="INDEX-152" /></a><a name="INDEX-153" /></a> </p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="part2.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch03_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">II. Language Basics</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">3.2. Command-Line Options</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; 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 + -