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

📄 ch1.htm

📁 prrl 5 programs codes in the book
💻 HTM
📖 第 1 页 / 共 2 页
字号:

<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 download

the <TT>i86 Release Binary</TT>. This

link lets you download a zip file that contains the Perl files

in compressed form.

</BLOCKQUOTE>

<P>

Instructions for compiling Perl or for installing on each operating

system are iNCluded with the distribution files. Follow the instructions

provided and you should having a working Perl installation rather

quickly. If you have trouble installing Perl, skip ahead to Chapter

22, &quot;Internet Resources,&quot; 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 text

on your monitor. First, you create a text file to hold the Perl

program. 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 a

series of Perl statements. Statements are written in what looks

like 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 rules

governing where a statement starts and ends are:

<UL>

<LI>Leading spaces on a line are ignored. You can start a Perl

statement anywhere you want: at the beginning of the line, indented

for clarity (recommended) or even right-justified (definitely

frowned 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-one

space is as good as a hundred. That means you can split statements

over several lines for clarity. A string is basically a series

of characters eNClosed in quotes. <A HREF="ch2.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch2.htm" >Chapter 2</A> &quot;Numeric and

String Literals,&quot; 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(&quot;My name is Yon Yonson\n&quot;);

</PRE>

</BLOCKQUOTE>

<P>

No prizes for guessing what happens when Perl runs this code-it

prints out <TT>My name is Yon Yonson</TT>.

If the &quot;\n&quot; doesn't look familiar, don't worry-it simply

means 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 statements

like this, or giving multiple arguments to the <TT>print()</TT>

fuNCtion:

<BLOCKQUOTE>

<PRE>

print(&quot;My name is Yon Yonson,\n&quot;);

print(&quot;I live in Wisconsin,\n&quot;, &quot;I work in a lumbermill there.\n&quot;);

</PRE>

</BLOCKQUOTE>

<P>

So what does a complete Perl program look like? Here's a small

example, complete with the invocation line at the top and a few

comments:

<BLOCKQUOTE>

<PRE>

#!/usr/local/bin/perl -w

print(&quot;My name is Yon Yonson,\n&quot;);

print(&quot;I live in Wisconsin,\n&quot;, &quot;I work in a lumbermill there.\n&quot;);

</PRE>

</BLOCKQUOTE>

<P>

That's not at all typical of a Perl program, though; it's just

a 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 your

system, the simplest way to run a Perl program is to type the

following:

<BLOCKQUOTE>

<PRE>

perl <I>filename</I>.pl

</PRE>

</BLOCKQUOTE>

<P>

The <TT><I>filename</I></TT> should

be replaced by the name of the program that you are trying to

run 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, you

need 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 of

this script file is to be run by <B>/usr/local/bin/perl</B>. The

second step is to make the program file itself executable by changing

its mode:

<BLOCKQUOTE>

<PRE>

chmod +x test.pl

</PRE>

</BLOCKQUOTE>

<P>

Now you can execute the program file directly and let the program

file tell the operating system what interpreter to use while running

it. 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 mechanics

of your program. For example, it is very easy to understand that

your program adds 66 to another value. But, in two years, you

may 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(&quot;Perl is easy.\n&quot;);      # 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 written

and executed a Perl program. Believe it or not, you've now done

more than most people that I talk to on the web. Let's quickly

review what you've read so far.

<P>

Perl was created to solve a need, not to match the ideals of computer

scieNCe. It has evolved from being a simple hack to a full-fledged

modern programming language. Perl's syntax is similar to the C

programming language. However, it has a lot of features that were

borrowed from UNIX tools.

<P>

Perl is very cost-effective in a lot of situations because it

is free. There are legal restrictions that you need to follow.

However, any restrictions are listed in the documentation that

comes 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 executables

for Windows 95 and Windows NT.

<P>

Perl programs are simply text files. They are created in any text

editor. As long as you give the file an extension of .pl, running

the 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 difficulty

may have been if you did not have Perl installed. The next part

of the journey will be to learn some basic building blocks in

the form of numeric and string literals. But literals will have

to 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 looking

at the links.

<LI>Create and run a Perl program that prints <TT>&quot;Hello,

World&quot;</TT> on the monitor.

</OL>

<HR>



<CENTER><P>

<A HREF="#CONTENTS"><IMG SRC="cc.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>

<A HREF="index-1.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/index-1.htm"><IMG SRC="hb.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>

<A HREF="ch2.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch2.htm"><IMG SRC="nc.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 + -