📄 ch29.htm
字号:
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>Internet address <TT>128.91.200.37</TT>
</BLOCKQUOTE>
<P>
</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"></TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>Directory <TT>/perl5</TT>
</BLOCKQUOTE>
<P>
</TD>
</TR>
</TABLE>
<BR>
<FONT SIZE="4"><B>Europe</B></FONT>
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P><I>Site</I>
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P><I>Location</I>
</BLOCKQUOTE>
<P>
</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>ftp.cs.ruu.nl</TT></TD>
<TD ALIGN="LEFT">Internet address <TT>131.211.80.17</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"></TD>
<TD ALIGN="LEFT">Directory <TT>/pub/PERL/perl5.0/src</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>ftp.funet.fi</TT></TD>
<TD ALIGN="LEFT">Internet address <TT>128.214.248.6</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"></TD>
<TD ALIGN="LEFT">Directory <TT>/pub/languages/perl/ports/perl5</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>ftp.zrz.tu-berlin.de</TT></TD>
<TD ALIGN="LEFT">Internet address <TT>130.149.4.40</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"></TD>
<TD ALIGN="LEFT">Directory <TT>/pub/unix/perl</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">
<BLOCKQUOTE>
<P><TT>src.doc.ic.ac.uk</TT>
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>Internet address <TT>146.169.17.5</TT>Directory <TT>/packages/perl5</TT>
</BLOCKQUOTE>
<P>
</TD>
</TR>
</TABLE>
<BR>
<BR>
<FONT SIZE="4"><B>Australia</B></FONT>
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P><I>Site</I>
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P><I>Location</I>
</BLOCKQUOTE>
<P>
</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P><TT>sungear.mame.mu.oz.au</TT>
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>Internet address <TT>128.250.209.2</TT>
</BLOCKQUOTE>
<P>
</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"></TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>Directory <TT>/pub/perl/src/5.0</TT>
</BLOCKQUOTE>
<P>
</TD>
</TR>
</TABLE>
<BR>
<FONT SIZE="4"><B>South America </B></FONT>
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P><I>Site</I>
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P><I>Location</I>
</BLOCKQUOTE>
<P>
</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P><TT>ftp.inf.utfsm.cl</TT>
</BLOCKQUOTE>
<P>
</TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>Internet address <TT>146.83.198.3</TT>
</BLOCKQUOTE>
<P>
</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"></TD>
<TD ALIGN="LEFT">
<BLOCKQUOTE>
<P>Directory <TT>/pub/gnu</TT>
</BLOCKQUOTE>
<P>
</TD>
</TR>
</TABLE>
</P>
</CENTER>
<P>You also can obtain Perl from most sites that store GNU source code, or from any
site that archives the Usenet newsgroup <TT>comp.sources.unix</TT>.
<CENTER>
<H3><A NAME="Heading7<FONT COLOR="#000077">A Sample Perl Program</FONT></H3>
</CENTER>
<P>Now that Perl is available on your system, it's time to show you a very simple
program that illustrates how easy it is to use Perl. The program shown in Listing
29.1 asks for a line of input and writes it.
<CENTER>
<H3><A NAME="Heading8<FONT COLOR="#000077">Listing 29.1. A simple Perl program
that reads and writes a line of input.</FONT></H3>
</CENTER>
<PRE><FONT COLOR="#0066FF">1: #!/usr/local/bin/perl
2: $inputline = <STDIN>;
3: print( $inputline );
</FONT></PRE>
<P>Here is the output from this listing:</P>
<P>Line 1 is the header comment. Line 2 reads a line of input. Line 3 writes the
line of input back to your screen.</P>
<P>The following sections describe how to create and run this program, and they describe
it in more detail.
<CENTER>
<H3><A NAME="Heading9<FONT COLOR="#000077">Running a Perl Program</FONT></H3>
</CENTER>
<P>To run the program shown in Listing 29.1, carry out the following actions:
<DL>
<DD><B>1.</B> Using your favorite editor, type the program and save it in a file
called <TT>program29_1</TT>.<BR>
<BR>
<B>2.</B> Tell the system that this file contains executable statements. To do this
in the UNIX environment, enter the following command:
</DL>
<PRE><FONT COLOR="#0066FF">$ chmod +x program29_1
</FONT></PRE>
<DL>
<DD><B>3.</B> Run the program by entering this command:
</DL>
<PRE><FONT COLOR="#0066FF">$ program129_1
</FONT></PRE>
<P>When you run <TT>program29_1</TT>, it waits for you to enter a line of input.
After you enter the line of input, <TT>program29_1</TT> prints what you entered,
as shown here:</P>
<PRE><FONT COLOR="#0066FF">$ program29_1
This is my line of input.
This is my line of input.
$
</FONT></PRE>
<CENTER>
<H4><A NAME="Heading10<FONT COLOR="#000077">If Something Goes Wrong</FONT></H4>
</CENTER>
<P>If Listing 29.1 is stored in the file <TT>program29_1</TT> and run according to
the preceding steps, the program should run successfully. If the program doesn't
run, one of two things has likely happened:
<UL>
<LI>The system can't find the file <TT>program29_1</TT>.
<P>
<LI>The system can't find Perl.
</UL>
<P>If you receive the error message</P>
<PRE><FONT COLOR="#0066FF">program29_1 not found
</FONT></PRE>
<P>or something similar, your system couldn't find the file <TT>program29_1</TT>.
To tell the system where <TT>program29_1</TT> is located, you can do one of two things
in a UNIX environment:
<UL>
<LI>Enter the command <TT>./program29_1</TT>, which gives the system the pathname
of <TT>program29_1</TT> relative to the current directory.
<P>
<LI>Add the current directory <TT>.</TT> to your <TT>PATH</TT> environment variable.
This tells the system to search in the current directory when looking for executable
programs such as <TT>program29_1</TT>.
</UL>
<P>If you receive the message</P>
<PRE><FONT COLOR="#0066FF">/usr/local/bin/perl not found
</FONT></PRE>
<P>or something similar, Perl is not installed properly on your machine. Refer to
the section "How Do I Find Perl?" earlier in this chapter, for more details.</P>
<P>If you don't understand these instructions or are still having trouble running
Listing 29.1, talk to your system administrator.
<CENTER>
<H3><A NAME="Heading11<FONT COLOR="#000077">Line 1 of Your Program: How Comments
Work</FONT></H3>
</CENTER>
<P>Now that you've run your first Perl program, let's look at each line of Listing
29.1 and figure out what it does.</P>
<P>Line 1 of this program is a special line that tells the system that this is a
Perl program:</P>
<PRE><FONT COLOR="#0066FF">#!/usr/local/bin/perl
</FONT></PRE>
<P>Let's break this line down, one part at a time:
<UL>
<LI>The first character in the line, the <TT>#</TT> character, is the Perl comment
character. It tells the system that this line is not an executable instruction.
<P>
<LI>The <TT>!</TT> character is a special character; it indicates what type of program
this is. (You don't need to worry about the details of what the <TT>!</TT> character
does. All you have to do is remember to include it.)
<P>
<LI>The path <TT>/usr/local/bin/perl</TT> is the location of the Perl executable
on your system. This executable interprets your program; in other words, it figures
out what you want to do and then does it. Because the Perl executable has the job
of interpreting Perl instructions, it usually is called the Perl interpreter.
</UL>
<P>If, after reading this, you still don't understand the meaning of the line <TT>#!/usr/local/bin/perl</TT>,
don't worry. The actual specifics of what it does are not important for our purposes
in this book. Just remember to include it as the first line of your program, and
Perl will take it from there.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading12<FONT COLOR="#000077"><B>NOTE:</B> </FONT>If you are running
Perl on a system other than UNIX, you might need to replace the line <TT>#!/usr/local/bin/perl</TT>
with some other line indicating the location of the Perl interpreter on your system.
Ask your system administrator for details on what you need to include here. After
you have found out what the proper first line is in your environment, include that
line as the first line of every Perl program you write, and you're all set.
<HR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -