21天入门Perl系列,DAY 01

今天我们要学习的是:
什么是perl为什么perl有用
怎么得到perl
怎么用perl写脚本
解释编程语言和编译编程语言的区别(interpretive and compiled programming languages)
什么是算法,怎么升级

What Is Perl?
Perl是实用提取和报告语言的缩写。它是由拉里·沃尔设计用于在UNIX环境中编写程序的一种工具,并由他不断更新和维护。
Perl拥有与高级程序设计语言C一样的能力和灵活性。事实上,如您所见,许多语言的特点是从C中借用来的。
和shell脚本语言,Perl不需要特殊的编译器和链接器把程序编写工作代码。相反,所有你要做的就是写程序,并告诉Perl运行它。这意味着,Perl是理想的快速解决小程序问题的方案,或者创建原型来测试更大的问题的可能解决办法。
Perl提供的所有功能脚本语言sed和awk,加上这两种语言之外的功能。Perl也支持sed-to-Perl translator 和 an awk-to-Perl translator.
简单来说就是perl像C语言一样有用而且像sed, awk 和shell一样方便。

How Do I Find Perl?

查看perl是否已经安装在你的系统上:
如果你在UNIX系统上工作,查看这个/usr/local/bin/perl目录是否存在
如果你在其他系统,查看你的常用的程序安装文件夹。
如果没有可以自己安装perl

Where Do I Get Perl?
perl之所以如此流行其中一个理由是可以免费获取和任意修改。只要上网就可以从FTP站点上下载得到perl。如下所示:
$ ftp prep.ai.mit.eduConnected to prep.ai.mit.edu.220 aeneas FTP server (Version wu-2.4(1) Thu Apr 14 20:21:35 EDT 1994) ready.Name (prep.ai.mit.edu:dave): anonymous331 Guest login ok, send your complete e-mail address as password.Password:230-Welcome, archive user!230-230-If you have problems downloading and are seeing “Access denied” or230-“Permission denied”, please make sure that you started your FTP 230-client in a directory to which you have write permission.230-230-If you have any problems with the GNU software or its downloading, 230-please refer your questions to <gnu@PREP.AI.MIT.EDU>. If you have any230-other unusual problems, please report them to <root@aeneas.MIT.EDU>.230-230-If you do have problems, please try using a dash (-) as the first 230-character of your password – this will turn off the continuation230-messages that may be confusing your FTP client.230-230 Guest login ok, access restrictions apply.ftp> cd pub/gnu
250-If you have problems downloading and are seeing “Access denied” or250-“Permission denied”, please make sure that you started your FTP250-client in a directory to which you have write permission.250-250-Please note that all files ending in ‘.gz’ are compressed with 250-‘gzip’, not with the unix ‘compress’ program. Get the file README250- and read it for more information.250-250-Please read the file README250- it was last modified on Thu Feb 1 15:00:50 1996 – 32 days ago250-Please read the file README-about-.diff-files250- it was last modified on Fri Feb 2 12:57:14 1996 – 31 days ago250-Please read the file README-about-.gz-files250- it was last modified on Wed Jun 14 16:59:43 1995 – 264 days ago250 CWD command successful.ftp> binary200 Type set to I.ftp> get perl-5.001.tar.gz200 PORT command successful.150 Opening ASCII mode data connection for perl-5.001.tar.gz (1130765 bytes).226 Transfer complete.1130765 bytes received in 9454 seconds (1.20 Kbytes/s)ftp> quit221 Goodbye.$
once you’ve retrieved the Perl distribution, do the following:
1. Create a directory and move the file you just received, perl-5.001.tar.gz, to this directory. (Or, alternatively, move it to a directory already reserved for this purpose.)
2. The perl-5.001.tar.gz file is compressed to save space. To uncompress it, enter the command$ gunzip perl-5.001.tar.gzgunzipis the GNU uncompress program. If it’s not available on your system, see your system administrator. (You can, in fact, retrieve it from prep.ai.mit.eduusing anonymous FTP with the same commands you used to retrieve the Perl distribution.) When you run gunzip, the file perl-5.001.tar.gzwill be replaced by perl-5.001.tar, which is the uncompressed version of the Perl distribution file.
3. The next step is to unpack the Perl distribution. In other words, use the information in the Perl distribution to create the Perl source files. To do this, enter the following command:$ tar xvf – <perl-5.001.tarAs this command executes, it creates each source file in turn and displays the name and size of each file as it is created. The tarcommand also creates subdirectories where appropriate; this ensures that the Perl source files are organized in a logical way.
4. Using your favorite C compiler, compile the Perl source code using the makefile provided. (This makefile should have been created when the source files were unpacked in the last step.)5. Place the compiled Perl executable into the directory where you normally keep your executables. On UNIX systems, this directory usually is called /usr/local/bin, and Perl usually is named /usr/local/bin/perl.

A Sample Perl Program
现在您的系统上可用的Perl,是时候告诉你一个简单的程序说明了它是多么容易使用Perl。listing_1.1是一个简单的程序,它要求一个输入行,写出来。

Running a Perl Program
1. Using your favorite editor, type the previous program and save it in a file called program1_1.
2. Tell the system that this file contains executable statements. To do this in the UNIX environment, enter the command$ chmod +x program1_1
3. Run the program by entering the command$ program1_1

If Something Goes Wrong
如果Listing1.1存在文件program1_1,通过了前面的步骤,程序应该成功的运行了。如果没有,那么应该是没有找到文件program1_1或者没有找到perl.
可以指定program1_1的位置 ./program1_1,

The First Line of Your Perl Program: How Comments Work
现在你已经运行了第一个perl脚本,下面我们来按行阅读这个脚本。

第一行 #!/usr/local/bin/perl
“#” 是注释符号,它告诉系统后面的是不可执行部分,注释语句可以跟在代码后面或者自己单独一行
“!” 是特殊字符,它告诉系统这是什么类型的脚本
“/usr/local/bin/perl” 是perl在系统中的路径

第二行 Statements, Tokens, and <STDIN>
$inputline = <STDIN>;
这是第一行真正运行的代码。

Statements and Tokens
这一行代码是perl语句的一个例子,基本上一个语句是perl运行的一个任务。所以perl程序可以看做是perl语句的集合。
当perl解释器看见语句,语句打碎成一个个小的信息组。在这个例子中是$inputline, =, <STDIN>,;所有这些都叫做token。

Tokens and White Space
Token可以被任意数量的tabs和spaces隔开,例如以下:
$inputline = <STDIN>;
$inputline=<STDIN>;
$inputline = <STDIN>;
并且这语句可以被分割成多个行,例如:
$inputline
=
<STDIN>
;
新的语句一直从新行开始;
一个空格符用来隔开语句;

What the Token do: Reading from standard input
第一行的第一个标记是$inputline是字符变量的例子,一个变量能够存储一个信息。
<STDIN>是告诉解释器从输入文件中读取。

print, 标准输出。
Q&AQ:
Is there any particular editor I need to use with Perl?
A:No. Perl programs are ordinary text files. You can use any text editor you like.

Q:Why do I need to enter the chmod +x command before running my program?
A:Because Perl programs are ordinary text files, the UNIX operating system does not know that they are executable programs. By default, text files have read and write permissions granted, which means you can look at your file or change it. The chmod +x command adds execute permission to the file; when this permission is granted, the system knows that this is an executable program.

Q:Can I use print to print other things besides input lines?
A:Yes. You’ll learn more about how you can use print on Day 3, “Understanding Scalar Values.”

Q:Why is Perl available for free?
A:This encourages the dissemination of computer knowledge and capabilities. It works like this: You can get Perl for free, and you can use it to write interesting and useful programs. If you want, you can then give these programs away and let other people write interesting and useful programs based on your programs. This way, everybody benefits. You also can modify the source for Perl, provided you tell everybody that your version is a modification of the original. This means that if you think of a clever thing you want Perl to do, you can add it yourself. (However, you can’t blame anybody else if your modification breaks something or if it doesn’t work.) Of course, you don’t have to give your Perl programs away for free. In fact, you even can sell your Perl programs, provided you don’t borrow anything from somebody else’s program.

Exercises
1. Modify program1_1 to print the input line twice.
2. Modify program1_1 to read and print two different input lines。
3. Modify program1_1 to read two input lines and print only the second one.

4. BUG BUSTER: What is wrong with the following program?

#!/usr/local/bin/perl
$inputline = <STDIN>;
print ($inputline)

5. BUG BUSTER: What is wrong with the following program?
#!/usr/local/bin/perl
$inputline = <STDIN>;
# print my line! print($inputline);

6. What does the following program do?
#!/usr/local/bin/perl
$inputline = <STDIN>;
$inputline2 = <STDIN>;print ($inputline2);
print ($inputline);

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s