📄 ch1.htm
字号:
<B>For Windows 95/Windows NT</B>-The home page of hip communications,iNC. (<B>http://www.perl.hip.com</B>) contains a link to downloadthe <TT>i86 Release Binary</TT>. Thislink lets you download a zip file that contains the Perl filesin compressed form.</BLOCKQUOTE><P>Instructions for compiling Perl or for installing on each operatingsystem are iNCluded with the distribution files. Follow the instructionsprovided and you should having a working Perl installation ratherquickly. If you have trouble installing Perl, skip ahead to Chapter22, "Internet Resources," connect to the #perl IRC channel,and ask for help. Don't be shy!<H2><A NAME="YourFirstPerlProgram"><FONT SIZE=5 COLOR=#FF0000>Your First Perl Program</FONT></A></H2><P>Your first Perl program will show how to display a line of texton your monitor. First, you create a text file to hold the Perlprogram. Then you run or execute the Perl program file.<H3><A NAME="CreatingtheProgram">Creating the Program</A></H3><P>A Perl program consists of an ordinary text file containing aseries of Perl statements. Statements are written in what lookslike an amalgam of C, UNIX shell script, and English. In fact,that's pretty much what it is.<P>Perl code can be quite free-flowing. The broad syntactic rulesgoverning where a statement starts and ends are:<UL><LI>Leading spaces on a line are ignored. You can start a Perlstatement anywhere you want: at the beginning of the line, indentedfor clarity (recommended) or even right-justified (definitelyfrowned on because the code would be difficult to understand)if you like.<LI>Statements are terminated with a semicolon.<LI>Spaces, tabs, and blank lines outside of strings are irrelevant-onespace is as good as a hundred. That means you can split statementsover several lines for clarity. A string is basically a seriesof characters eNClosed in quotes. <A HREF="ch2.htm" >Chapter 2</A> "Numeric andString Literals," contains a better definition for strings.<LI>Anything after a hash sign (#) is ignored except in strings.Use this fact to pepper your code with useful comments.</UL><P>Here's a Perl statement inspired by Kurt Vonnegut:<BLOCKQUOTE><PRE>print("My name is Yon Yonson\n");</PRE></BLOCKQUOTE><P>No prizes for guessing what happens when Perl runs this code-itprints out <TT>My name is Yon Yonson</TT>.If the "\n" doesn't look familiar, don't worry-it simplymeans that Perl should print a newline character after the text,or in other words, go to the start of the next line.<P>Printing more text is a matter of either stringing together statementslike this, or giving multiple arguments to the <TT>print()</TT>fuNCtion:<BLOCKQUOTE><PRE>print("My name is Yon Yonson,\n");print("I live in Wisconsin,\n", "I work in a lumbermill there.\n");</PRE></BLOCKQUOTE><P>So what does a complete Perl program look like? Here's a smallexample, complete with the invocation line at the top and a fewcomments:<BLOCKQUOTE><PRE>#!/usr/local/bin/perl -wprint("My name is Yon Yonson,\n");print("I live in Wisconsin,\n", "I work in a lumbermill there.\n");</PRE></BLOCKQUOTE><P>That's not at all typical of a Perl program, though; it's justa linear sequeNCe of commands with no complexity.<P>You can create your Perl program by starting any text processor:<BLOCKQUOTE><B>In UNIX</B>-you can use <TT>emacs</TT>or <TT>vi</TT>.<BR><B>In Windows 95/Windows NT</B>-you can use <TT>notepad</TT>or <TT>edit</TT>.<BR><B>In OS/2</B>-you can use <TT>e</TT>or <TT>epm</TT>.</BLOCKQUOTE><P>Create a file called <TT>test.pl</TT>that contains the preceding three lines.<H3><A NAME="Invocation">Invocation</A></H3><P>Assuming that Perl is correctly installed and working on yoursystem, the simplest way to run a Perl program is to type thefollowing:<BLOCKQUOTE><PRE>perl <I>filename</I>.pl</PRE></BLOCKQUOTE><P>The <TT><I>filename</I></TT> shouldbe replaced by the name of the program that you are trying torun or execute. If you created a <TT>test.pl</TT>file while reading the previous section, you can run it like this:<BLOCKQUOTE><PRE>perl test.pl</PRE></BLOCKQUOTE><P>This example assumes that perl is in the execution path; if not,you will need to supply the full path to perl, too. For example,on UNIX the command might be:<BLOCKQUOTE><PRE>/usr/local/bin/perl test.pl</PRE></BLOCKQUOTE><P>Whereas on Windows NT, you might need to use:<BLOCKQUOTE><PRE>c:\perl5\bin\perl test.pl</PRE></BLOCKQUOTE><P>UNIX systems have another way to invoke a program. However, youneed to do two things. The first is to place a line like<BLOCKQUOTE><PRE>#!/usr/local/bin/perl</PRE></BLOCKQUOTE><P>at the start of the Perl file. This tells UNIX that the rest ofthis script file is to be run by <B>/usr/local/bin/perl</B>. Thesecond step is to make the program file itself executable by changingits mode:<BLOCKQUOTE><PRE>chmod +x test.pl</PRE></BLOCKQUOTE><P>Now you can execute the program file directly and let the programfile tell the operating system what interpreter to use while runningit. The new command line is simply:<BLOCKQUOTE><PRE>test</PRE></BLOCKQUOTE><H2><A NAME="CommentsinYourProgram"><FONT SIZE=5 COLOR=#FF0000>Comments in Your Program</FONT></A></H2><P>It is very important to place comments into your Perl programs.Comments will enable you to figure out the intent behind the mechanicsof your program. For example, it is very easy to understand thatyour program adds 66 to another value. But, in two years, youmay forget how you derived the number 66 in the first place.<P>Comments are placed inside a program file using the # character.Everything after the # is ignored. For example:<BLOCKQUOTE><PRE># This whole line is ignored.print("Perl is easy.\n"); # Here's a half-line comment.</PRE></BLOCKQUOTE><H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>Summary</FONT></A></H2><P>You've finished the first chapter of the book and already writtenand executed a Perl program. Believe it or not, you've now donemore than most people that I talk to on the web. Let's quicklyreview what you've read so far.<P>Perl was created to solve a need, not to match the ideals of computerscieNCe. It has evolved from being a simple hack to a full-fledgedmodern programming language. Perl's syntax is similar to the Cprogramming language. However, it has a lot of features that wereborrowed from UNIX tools.<P>Perl is very cost-effective in a lot of situations because itis free. There are legal restrictions that you need to follow.However, any restrictions are listed in the documentation thatcomes with Perl, and you don't need that information repeated.<P>You can get Perl by reading the <B>http://www.perl.com/perl/info/software.html</B>Web page. It has links to both the source code and the executablesfor Windows 95 and Windows NT.<P>Perl programs are simply text files. They are created in any texteditor. As long as you give the file an extension of .pl, runningthe file will be easy.<P>Most systems will run Perl program file called <TT>test.pl</TT>with the following command:<BLOCKQUOTE><PRE>perl test.pl</PRE></BLOCKQUOTE><P>You can add comments to your Perl program using the # character.Anything after the # character is ignored.<P>I hope the journey has been very smooth so far. The only difficultymay have been if you did not have Perl installed. The next partof the journey will be to learn some basic building blocks inthe form of numeric and string literals. But literals will haveto wait until the next chapter.<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#FF0000>Review Questions</FONT></A></H2><P>Answers to Review Questions are in Appendix A.<OL><LI>What is the address of Perl's home page?<LI>Who was the creator of Perl?<LI>How much does Perl cost?<LI>Why are comments important to programming?</OL><H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#FF0000>Review Exercises</FONT></A></H2><OL><LI>Connect to the Perl Home Page and spend a few minutes lookingat the links.<LI>Create and run a Perl program that prints <TT>"Hello,World"</TT> on the monitor.</OL><HR><CENTER><P><A HREF="#CONTENTS"><IMG SRC="cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="index.htm"><IMG SRC="hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="ch2.htm"><IMG SRC="nc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><HR WIDTH="100%"></P></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -