暂无图片
暂无图片
暂无图片
暂无图片
暂无图片
EssentialPerl.pdf
8
25页
0次
2025-04-16
免费下载
Essential Perl Page: 1
Essential Perl
This document is a quick introduction to the Perl language. Perl has many features, but you can
get pretty far with just the basics, and that's what this document is about. The coverage is pretty
quick, intended for people with some programming experience. This document is available for
free in the spirit of engineering goodwill -- that success is not taken, it is earned.
Stanford CS Education #108
http://cslibrary.stanford.edu/108/
by Nick Parlante copyright (c) 2000-2002
Revised 5/2002
This is document #108 in the Stanford CS Education Library -- see
http://cslibary.stanford.edu/108/ for this and other free educational CS materials. This
document is free to be used, reproduced, or sold so long as this paragraph and the copyright are
clearly reproduced.
Contents
1. What is Perl?
2. Variables
3. Strings
4. Arrays
5. Associative Arrays
6. If, while, etc.
7. File Input
8. Print output
9. Strings and Regular Expressions
10. Subroutines
11. Running External Programs
12. References
13. Terse Perl
1. What Is Perl?
Perl is a free, open source programming language created by Larry Wall. Perl aims for adjectives
like "practical" and "quick" and not so much words like "structured" or "elegant". A culture has
built up around Perl where people create and give away modules, documentation, sample code,
and a thousand other useful things -- visit the Comprehensive Perl Archive Network (CPAN),
http://www.cpan.org/, or http://www.perl.com/ to see the amazing range of Perl material
available.
Perl Niche
Perl is probably best known for text processing -- dealing with files, strings, and regular
expressions. However, Perl's quick, informal style makes it attractive for all sorts of little
programs. If I need a 23 line program to get some task done, I can write it in Perl and be done in 3
Essential Perl Page: 2
minutes. Perl code is very portable -- I frequently move Perl programs back and forth from the
Mac to various Unixes and it just works. With Perl, you are not locked in to any particular vendor
or operating system. Perl code is also robust; Perl programs can have bugs, but they will not
crash randomly like C or C++ programs. On the other hand, in my opinion, Perl's easy-going
style makes it less appealing for large projects where I would rather use Java.
Warning: My Boring Perl Style
Perl is famous for allowing you to write solutions to complex problems with very short, terse
phrases of code. There's something satisfying about reducing a whole computation down to a
single line of dense code. However, I never do that. I write Perl code in a boring, straightforward
way which tends to spell out what it's actually doing step by step. The terse style is mentioned
briefly in the Terse Perl section. Also, in versions 5 and 6, Perl has accumulated more sophisticated
features which are not covered here. We just do simple old Perl code.
Running Perl
A Perl program is just a text file. You edit the text of your Perl program, and the Perl interpreter
reads that text file directly to "run" it. This structure makes your edit-run-debug cycle nice and
fast. On Unix, the Perl interpreter is called "perl" and you run a Perl program by running the Perl
interpreter and telling it which file contains your Perl program...
> perl myprog.pl
The interpreter makes one pass of the file to analyze it and if there are no syntax or other obvious
errors, the interpreter runs the Perl code. There is no "main" function -- the interpreter just
executes the statements in the file starting at the top.
Following the Unix convention, the very first line in a Perl file usually looks like this...
#!/usr/bin/perl -w
This special line is a hint to Unix to use the Perl interpreter to execute the code in this file. The "-w"
switch turns on warnings which is generally a good idea. In unix, use "chmod" to set the execute
bit on a Perl file so it can be run right from the prompt...
> chmod u+x foo.pl ## set the "execute" bit for the file once
>
> foo.pl ## automatically uses the perl interpreter to "run" this file
The second line in a Perl file is usually a "require" declaration that specifies what version of Perl
the program expects...
#!/usr/bin/perl -w
require 5.004;
Perl is available for every operating system imaginable, including of course Windows and MacOS,
and it's part of the default install in Mac OSX. See the "ports" section of http://www.cpan.org/ to
Essential Perl Page: 3
get Perl for a particular system.
2. Syntax And Variables
The simplest Perl variables are "scalar" variables which hold a single string or number. Scalar
variable names begin with a dollar sign ($) such as $sum or $greeting. Scalar and other
variables do not need to be pre-declared -- using a variable automatically declares it as a global
variable. Variable names and other identifiers are composed of letters, digits, and underscores (_)
and are case sensitive. Comments begin with a "#" and extend to the end of the line.
$x = 2; ## scalar var $x set to the number 2
$greeting = "hello"; ## scalar var $greeting set to the string "hello"
A variable that has not been given a value has the special value "undef" which can be detected
using the "defined" operator. Undef looks like 0 when used as a number, or the empty string ""
when used as a string, although a well written program probably should not depend on undef in
that way. When Perl is run with "warnings" enabled (the -w flag), using an undef variable prints a
warning.
if (!defined($binky)) {
print "the variable 'binky' has not been given a value!\n";
}
What's With This '$' Stuff?
Larry Wall, Perl's creator, has a background in linguistics which explains a few things about Perl. I
saw a Larry Wall talk where he gave a sort of explanation for the '$' syntax in Perl: In human
languages, it's intuitive for each part of speech to have its own sound pattern. So for example, a
baby might learn that English nouns end in "-y" -- "mommy," "daddy," "doggy". (It's natural for a
baby to over generalize the "rule" to get made up words like "bikey" and "blanky".) In some small
way, Perl tries to capture the different-signature-for-different-role pattern in its syntax -- all scalar
expressions look alike since they all start with '$'.
3. Strings
Strings constants are enclosed within double quotes (") or in single quotes ('). Strings in double
quotes are treated specially -- special directives like \n (newline) and \x20 (hex 20) are expanded.
More importantly, a variable, such as $x, inside a double quoted string is evaluated at run-time
and the result is pasted into the string. This evaluation of variables into strings is called
"interpolation" and it's a great Perl feature. Single quoted (') strings suppress all the special
evaluation -- they do not evaluate \n or $x, and they may contain newlines.
$fname = "binky.txt";
$a = "Could not open the file $fname."; ## $fname evaluated and pasted in
-- neato!
$b = 'Could not open the file $fname.'; ## single quotes (') do no
special evaluation
## $a is now "Could not open the file binky.txt."
of 25
免费下载
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文档的来源(墨天轮),文档链接,文档作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。