Perl Language Reference Guide

上一篇 / 下一篇  2008-12-13 11:58:44 / 个人分类:Perl

我的栏目
  • 栏目:Perl

Note: This Guide is from Harvard Computer Society. I learned it and modified it for my own purpose. It is useful for Perl Learning. And all rights reserved.51Testing软件测试网n1E'{C'g

   51Testing软件测试网idR(t w-`#p

Table of Contents51Testing软件测试网 l-d X~)oQ

Introduction

N'AI;u1VY*g-Tl0

Invocation and #!51Testing软件测试网-m/hylr)H7Z.\

Basic Semantics

4~p8T`o0

Hello, World

ni9l~a k!vR4y#l0

Scalars

_dXG)I"~;L0

$@%&*.51Testing软件测试网 K1Y5J` eU@

Numerical Data and Operators51Testing软件测试网e}!VZ.y#~6A*ae

Strings51Testing软件测试网Ma.Dx0S*TF

Single and Double Quoting51Testing软件测试网J_.AEd5V^;z MR Jw

Comparison Operators: Arithmetic vs. String51Testing软件测试网0Nk&J:Q RU~$RJ

undef

TY1z)ak$_2|@0

Lists51Testing软件测试网:LzT,vNP

Subscrīpting51Testing软件测试网9mn7amOZ-r&q

Array Sizes51Testing软件测试网0Rs{eO0y%_G

Some Array Functions

[SX/LO HN0

Context

| W0C(P G P)h!u0

Control Structures

fk_:lSE t6y d0

If51Testing软件测试网k!fX*q5xz

Unless

^,g9s|?2n/pe;e0

Doing Them Backwards51Testing软件测试网H.bo6bT+x0K'k0kw

And, Or

8ao#WYk0

What is Truth51Testing软件测试网.t&`E5p R"\

While

or#Ox1m"s0

For

#KQ Dh8~5N4|f^0

Foreach51Testing软件测试网FaH8lKecK

last, next, and redo

ge6K K5?&z[n~/h8E0

Hashes51Testing软件测试网;c\;?f K?u1YS9q

Keys() and Values()

`1WlUE-nL0

Deleting51Testing软件测试网-Bb#l6]'HK

Filehandles and I/O51Testing软件测试网 wA3c3O$Ph?U

<STDIN> and <>

-O2QVV!Lk2W HIc0

Make Your Own Filehandles51Testing软件测试网 U0C)|$CW

Chopping51Testing软件测试网#se1`s%MD

Printing to Filehandles

im q*M*t y&N6^d0

Handling File Errors51Testing软件测试网]h5bfC

File Tests51Testing软件测试网2mDz8T$t%u&?[2C

Pipes51Testing软件测试网aD\HfQ \

Regular Expressions51Testing软件测试网 A V&q7x \O!w b,D&W

Examples

&o%N.sFw }q X0

Using the Matching Operator51Testing软件测试网q4N/@9S$c:r

Case Insensitivity51Testing软件测试网7t-Xg ^%p

The Magic of Parentheses51Testing软件测试网`j4|2yqS'x#H | `?

Substitution51Testing软件测试网&f,CRT/VY4Ra{

split() and join()51Testing软件测试网&LV8xHC%G

Functions

{ O zb+X&?0

&calling(them)

7{X6v;k1E{C#|0

my() oh my()

O]:~~n"B!NC S6AY0

Special Sort Functions

ZJ](J1S/f0

Formats

meNp(i.o0

Introduction

Perl stands for Practical Extraction and Report Language, or Pathologically Eclectic Rubbish Lister. Perl is a very useful programming language that is standard on almost all Unix systems. Its semantics are a lot like C, but its text processing and regular expression matching features are far more powerful. Perl is the ideal tool for the lazy programmer; almost any simple task can be performed using a Perl scrīpt that can be written in under 10 minutes. Perl is particularly powerful for scanning large quantities of text and doing pattern matching. Perl is also very useful as a scrīpting language for web pages (as CGI).51Testing软件测试网GK&C8GHMb

Invocation and #!

To create a Perl scrīpt, simply put your program into a text file. You can invoke it several ways:51Testing软件测试网NLg PoD$]vL2g

1)    Execute it from the command prompt withperl -w <file>51Testing软件测试网MZl&]5]+v&y;xc!Np

2)    Put#!/usr/local/bin/perl –w at the top of your scrīpt, make the scrīpt executable, and invoke it simply by typing the scrīpt name at the command prompt. (You still have to type the path to the scrīpt, or if the scrīpt is in the current directory, type./scrīptname.51Testing软件测试网] P2U*v*w&e%WY

Tip: You’ll need to make the permissions on the scrīpt executable to run it this way; to do so type:51Testing软件测试网-Wmk%_wU

chmod 700 scrīptname51Testing软件测试网 NT,|.d l:S,j+Df

Basic Semantics

In Perl, comments start with a # and continue to the end of the line. Otherwise, the semantics are basically very similar to C: all statements end with a semicolon; whitespace is ignored; curly braces are used for control structures; functions have arguments in parentheses following the function name.

#C\ z9J&oTZ1~(g0

Hello, World

Pioneered by Brian Kernighan, the Hello, world program is the traditional way to start off a new language. One of the nicest features of Perl, unlike C, is that to do something very simple requires only a very simple scrīpt.  You can just go ahead and say:

oEj L)A,N `4y0

 

,i&FWz qSn*LV0

#!/usr/local/bin/perl -w51Testing软件测试网.aX)S.f2j$o{];O]

 51Testing软件测试网(pW~ zksD9j

   # My first perl program

Ls3NQ#@Zw9H0

   print "Hello, world!\n";51Testing软件测试网3hs#C[ \G


Scalars

Unlike C, Perl does not have separate data types for numbers, characters, strings, and so on. Instead, Perl has three primary data types: scalars, lists, and hashes. Scalars can hold one of several kinds of data:

*Vu o.yY:dr0

 51Testing软件测试网7B8N&w'YKd{

      Type             Example(s)51Testing软件测试网EPu4hN

      Integers  3

,h[/hx Wt9?0

          -9

!hB.q:o U4CT1O j0

0377  # octal51Testing软件测试网${.I.N9uq}x?p

0x15  # hexadecimal51Testing软件测试网![~z(J {Zz1W+r

   Floats1.25

oF(H ?r,?S-d'?0

          6.55e12

)qt!v G1y%m rn0

          -2e-1551Testing软件测试网Q0f1lI"vn

   Strings   ‘Perl’51Testing软件测试网 kK2?^ Kix2K

          “This is a sentence with a newline at the end.\n”51Testing软件测试网#_Ef \ N&y

 51Testing软件测试网aMbv,L;\ y7o

Also unlike C, you do not need to declare your variables ahead of time. When you want a variable, you simply use it and it springs into being. Likewise, Perl deals with allocating the appropriate amount of memory for the data you want a scalar (or anything else) to hold.51Testing软件测试网1^"h'f,J

$@%&*

Perl is a very punctuation-heavy language. Regular expressions are the worst offenders. In addition, all variable names start with an item of punctuation:51Testing软件测试网s8kRv#M?;Y

      $    Scalar51Testing软件测试网bAw(Q&HE

      @   Array or List

b3A9Bo'Lk*}0

      %   Hash51Testing软件测试网0[ A)mKb~K

      &    Function (aka Subroutine)51Testing软件测试网To-KT"}*?-@

*Typeglob(we won’t cover this)51Testing软件测试网9v tgI#z`"?

Numerical Data and Operators

Arithmetic operators and their associated assignment operators are very much like C's. A notable exception is /, which will create a float from an integer if appropriate. Useint ($a / 2)for integer arithmetic.51Testing软件测试网 W6{puT z

 

(Mw9J2k$rfd0

   $a = 4;

.H8Hu6T(Zl-Sr0

   $b = 2.5;51Testing软件测试网v(`%Hj2i ?)U

   $c = $a + $b;# now $c is 6.551Testing软件测试网 Iu.~8G x'S)b.}

   $b += 2;     # $b is 4.5

G!P+R3vE9vIx.`D0

   $a %= 3;     # $a is 1

,]}.@3p,n l*z0

   $a++;        # $a is 2 (same as C)

/MW.V&q'yMI0

Strings

Strings are also held in scalars. They are not represented as arrays of characters (well, they are internally, but you never have to worry about that). Anything you want to do, you can do:51Testing软件测试网m'_ M6SZ6F

      

l0|#?9O7wG(YGX0

   $a = "Harvard";51Testing软件测试网+T%Q'^H$dpE2|K/U

   $b = 'Computer';51Testing软件测试网'gDf*c)yH8X7h

   $c = $a . " " . $b; # $c is Harvard Computer

9Cu,Wp:^Rl-Hk0

   $c .= " Society";   # $c is Harvard Computer Society

Vi4~4fV%a0

   $d = $a x 4;     # $a is HarvardHarvardHarvardHarvard

Ii;G*@MM oO0

   $b x= 2;         # $b is ComputerComputer

#D1n%r]C6M v[O!P1R*i0

 

[k:B2K0x0

Since you never have to explicitly iterate over the characters of a string, strings are not null terminated. All perl operators know when they have reached the end of a string and stop.51Testing软件测试网?o*z!u;m$Q w

Single and Double Quoting

You can put a string literal into your program with either single or double quotes. Inside a single-quoted string, all characters stay as they are and are not interpreted. The only exception is toput a single quote in the string, use\'and toput a backslash in the string, use\\.51Testing软件测试网Ny*Ne+sC|7fe:Z&NRJ,W

 51Testing软件测试网L-PiP5N:yL#} }.s

Double quoted strings translate many more backslashed expressions, such as \n for newline. Also, any variable name placed in the double quoted string isinterpolated; in other words, the variable name (including the $ or other identifier) is replaced by the value of that variable.

5W-[$N@(i8R'EK0

 51Testing软件测试网SC-pr]&V&z

   $a = 'Hello'51Testing软件测试网Q7xpM#^WF4m"F

   $b = 'He said, \'Hi.\'' # $b is: He said 'Hi.'

[eJT7Vl0

   $c = "Hello"     # same as first example

Bw{/z4_ p:K^)G0

   $c = "$a, world!\n" # $c is: Hello, world! plus a newline (interpolation)

,W5Op1sX!q%|2Hgv0

   $d = '$a, world!\n'    # $d is: $a, world!\n (no interpolation)

(r5V,]$~8_0Z/ib0

 51Testing软件测试网[ t(]}i.O

Comparison Operators: Arithmetic vs. String

      Comparison                       Numeric        String51Testing软件测试网JPM P)Xg

   Equal               ==        eq51Testing软件测试网Bw[%ydqX.Y+f{

   Not Equal           !=        ne

T-eL$}{B0

   Less Than           <         lt51Testing软件测试网VQD'e\

   Greater Than        >         gt

+^Sy2V0u#i"T hn0

   Less Than or Equal To      <=        le

PE A"dDH8Z-[0

   Greater Than or Equal To>=        ge

#_[,\4|{)z0

 51Testing软件测试网owP&k"zk+G)V*^

It makes a difference which one you pick. All of them will work in all cases, but Perl's behavīor is different.

-@PM'A9O0

   5 < 30              # is true

1wg?TI0

   5 lt 30                 # is false51Testing软件测试网/SQ P0BYg

   "hcs" gt "hascs"           # is true

}-F-@/h"QR0P0

   "hcs" > "hascs"            # is false (they are both equal to 0)

mRRZ[f'~0

   "5 golden rings" eq "5 fingers"  # is false

etFJFuxOv-U0

   "5 golden rings" == "5 fingers"# is true (both are the number 5)

*T(wp3m&T'[J0`X7P0

 51Testing软件测试网 @%x#~.k2@O8x

The gt and lt tests compare in alphabetic order, so 30 comes before 5. If you do a numeric comparison on strings,Perl tries to convert them to numbers. It will take any numeric digits from the front and discard the rest; if there are no numeric digits, it converts to 0. This is an example of how Perl rarely gives errors based on type usage; instead, it will try to interpret the data in the appropriate way. Sometimes it does what you want; sometimes it does not. If you stick to the main path, you will never be surprised; but if you understand the behavīor of a certain operator you can often take shortcuts and save yourself time.

3e3^\:O^,JOK3O9b|0

 51Testing软件测试网x Q t!m j.vYi

undef

There is a special scalar value calledundef. If you access any variable that has not yet been assigned to, you getundef. You can set any variable toundefto clear it. If used as a number,undeflooks like 0, if used as a string, it looks like "" (the empty string). Some operators/functions returnundefunder various circumstances; this will usually look like 0 or "" if you try to use it, which is typically not a problem. Perl also includes a function calleddefinedthat tests to see if a value isundef.

.] Z(A5@g*br6LM$]_0

Lists

Perl's second type of data structure is the list, or array. A list is an ordered series of scalars. List variables start with the @ character. In your program, you can write arrays as a sequence of comma-separated values in parentheses.51Testing软件测试网9]wG8T {4wR7Lw;V

 51Testing软件测试网eI y1q3~ @5jkx

@a = (1, 2, 3);

OuT1z8gw"s4m0

@b = ("Lewis", "Epps", 4); # you can mix types – they're all scalars anyway

{3r%O!i`p/A!w0

@c = ($a + $b, $c . "\n"); # expressions are evaluated51Testing软件测试网 L z m+po*eM

@d = ();            # an empty array with 0 elements51Testing软件测试网 ` t0{3Mv D2s

@e = 2;         # it must be an array, so Perl makes it (2)

9R h9ACMB0

@f = (@a, 5, @b)       # no multi-level arrays51Testing软件测试网/w/b7xu3aZHF`f:j

             # this becomes (1, 2, 3, 5, "Lewis", "Epps", 4)

"KN)uOF}j9eh"x0

Subscrīpting

You can access individual elements of an array using square brackets. The index of an array starts at 0:

K ?&J8h\f0

   $a[0]     # is 1

%e-m"IPt0

   $a[2]     # is 351Testing软件测试网ZhCV:n!p$C

   $a[3]     # is undefined

-h2m-\"^pvF uF0

   $b[$a[0]] # is $b[1], which is "Epps"51Testing软件测试网@Z2o myp'rcS

   @a[1,2]   # is (2, 3)51Testing软件测试网#Y/w%gL ^D%G3P$v,JS

 51Testing软件测试网!PP}A1[d2p\ D

$a[0]is written with a $ because the value of that expression is a scalar. Likewise, if you take multiple values from an array, you get an array, so@a[1,2]is written with an @. Whatever type you get out is the type of identifier you use.

0qAoi,Y(H)J/l8L)_0

 51Testing软件测试网 ByuJ#mQ}aC8o

Note that$purpleis very different from$purple[2]. The first is a scalar named $purple; the second refers to the third element of an array named@purple.

;Zhm-L_ lrd/t|0

 

$X1Eg.[9q,} N9o"{"a0

You can also assign to individual elements or whole arrays:

'yUxSB8giHR%r0

 51Testing软件测试网e^'f&@V$AlC

   $b[2] = "Nathans";        # now @b is ("Lewis", "Epps", "Nathans")

{n4xRe o`0

   ($b[0], $b[1]) = ($b[1], $b[0]); # swap first two items51Testing软件测试网 w9{w q [x*Q"\%h k

   $a[0]--;              # now @a is (0, 2, 3)

HW uA d WU)h0

   $d[3] = "Knowles";        # now @d is (undef, undef, undef, "Knowles")51Testing软件测试网M$p:m;E;dro

 51Testing软件测试网,Lo G/j:L ] LP

As you can see from the last example above, Perl automatically changes the sizes of arrays to accommodate the data that is in them. Basically, you can do just about whatever you want to, and Perl will make it work out.

j6BuM9| g(_ Al2Ll0

 

[fG/D.FZ0

Array Sizes

The expression$#arrayevaluates to the subscrīpt of the last element of@array. In other words, it is equal to the number of elements in@array, minus one. If you assign to$#array, the size of the array will change; if you make an array smaller, values on the end will be removed. You don’t often want to do this, though, since you can easily lose data, so be careful.51Testing软件测试网-WFf@-J_0OS

 51Testing软件测试网D{9DVza9`2qN

   @conc = ("CS", "Government", "English");51Testing软件测试网2bj(A3O*[Y CGj

   $size_conc = $#conc;# now $size_conc is 251Testing软件测试网.t*_2k)f)N~

   $#conc = 1;         # we don't need the humanities51Testing软件测试网M'a;J3WK4bm

                 # now @concentrations is ("CS", "Government")51Testing软件测试网YH9j$Q\uOv]V d

   $#conc = 2;         # the last value was lost51Testing软件测试网9|1p\i0Dj4j q

                 # now @concentrations is ("CS", "Government", undef)51Testing软件测试网+RF;O?I_x

Some Array Functions

push()andpop()access an array like a stack, adding and removing things on the right side. unshift()andshift()do the same on the left, respectively.51Testing软件测试网5bR?;f.OBJ3t'Q

 

6Q} D(HWX0

   @example = (1, 2, 3);51Testing软件测试网9FR8XG)BHO.Tl.A

   push(@example, "Spam");   # now @example is (1, 2, 3,

TAG: Perl

 

评分:0

我来说两句

Open Toolbar