软件下载 | 资讯教程 | 最近更新 | 下载排行 | 一键转帖 | 发布投稿
您的位置:最火下载站 > 网络编程 > 编程综合 > Perl的基本语法和功能、使用详细介绍

Perl的基本语法和功能、使用详细介绍

烈火建站学院转载 本文很长,是台湾的一个同胞写的,PERL类的文章很少,希望大家好好学习 这篇文章是花了我很多时间、费了我很多心血才完成的,虽然连我自己都觉得无法达到尽善尽美的境界,但希望能帮助大家入门,稍微了解到Perl到底是个什麼样的东西,Perl到底有那些强大的功能,那麼这篇文章的目的就达到了。我分做资料型态、控制叙述、副程式、I/O和档案处理、Regular Expressions、Spectial Variables、Help 这几部分来讲解,但只是叙述了一些Perl的基本语法而已,Perl活泼的特性和程式的技巧就无法一一详述了,甚为缺憾。

(1) 资料型态(Data type):Perl的资料型态大致分为四种:Scalar、Scalar Array、Hash Array、References,看起来虽少但用起来却绰绰有餘。尤其在写Perl程式时可以不必事先宣告变数,这一点对刚学程式语言的人甚为方便,不过为了以后程式除错和维护方便,我建议你还是养成事先宣告变数的习惯比较好。 (a) Scalar:

纯量变数是Perl裡最基本的一种资料型态,它可以代表一个字元、字串、整数、甚至浮点数,而Perl把它们都看成是一样的东东! 你甚至可以混著用,不可思议吧。例如:

# 井字号开头的后面都是註解。# 纯量变数以$开头。# my 是一种宣告变数的方式,它可以使变数区域化。# 宣告变数时若不加 my 或 local 则Perl会把它当作全域变数使用。# 习惯上,我们会将字串用双引号括起来,而数值就不用加引号。my $x="abc";my $x=123;my $x=4.56;

那麼程式怎麼判断这是数值还是字串呢? 其实不是程式判断,而是你自己要判断。Perl分别提供了一堆运算子来处理数字和字串,你必须知道这个变数是数值或字串,才能使用个别的运算子来对变数做运算。我分别列出字串运算子和数值运算子,好让大家能区分它们的不同。

◎字串运算子String Operator Purpose  x Returns a string consisting of the string on the left of the operand, repeated the number of times of the right operand.  . Concatenates the two strings on both sides of the operator.  eq Returns True if the two operands are equivalent, False otherwise.  ne Returns True if the two operands are not equal, False otherwise.  le Returns True if the operand on the left is stringwise less than the operand on the right of the operator. Returns False otherwise.  lt Returns True if the operand on the left is stringwise less than or equal to the operand on the right of the operator. Returns False otherwise.  ge Returns True if the operand on the left is stringwise greater than or equal to the operand on the right of the operator. Returns False otherwise.  gt Returns True if the operand on the left is stringwise greater than the operand on the right of the operator. Returns False otherwise.  cmp Returns -1, 0, or 1 if the left operand is stringwise less than, equal to, or greater than the right operand.  , Evaluates the left operand, the evaluates the right operand. It returns the result of the right operand.  ++ Increments the string by one alphabetic value. 

◎数值运算子Value Operator Purpose  + Computes the additive value of the two operands.  - Computes the difference between the two operands.  * Computes the multiplication of the two operands.  / Computes the division between the two operands.  % Computes the modulus(remainder) of the two operands.  = = Returns Ture if the two operands are equivalent, False otherwise.  != Returns Ture if the two operands are not equal, False otherwise.  <= Returns Ture if the operand on the left is numerically less than or equal to the operand on the right of the operator. Returns False otherwise.  => Returns Ture if the operand on the left is numerically greater than or equal to the operand on the right of the operator. Returns False otherwise.  < Returns Ture if the operand on the left is numerically less than the operand on the right of the operator. Returns False otherwise.  > Returns Ture if the operand on the left is numerically greater than the operand on the right of the operator. Returns False otherwise.  < = > Returns -1 if the left operand is less than the right, +1 if is it greater than, and 0(False) otherwise.  && Performs a logical AND operation. If the left operand is True m then the right operator is not evaluated.  || Performs a logical OR operation. If the left operand is True m then the right operator is not evaluated.  & Returns the valueof the two operators bitwise ANDed.  | Returns the valueof the two operators bitwise ORed.  ^ Returns the valueof the two operators bitwise XORed.  ++ Increment operator. Increments the variable's value by 1.  -- Decrement operator. Decrements the variable's value by 1.  ** Computes the power of the left-hand value to the power of the rihght-hand value.  += Adds the value of the right-hand operand to the value of the left-hand operand.  -+ Subtracts the value of the right-hand operand to the value of the left-hand operand.  *= Mlutiplies the value of the left-hand operand to the value of the right-hand operand.  >> Shifts the left operand right by the number of bits that is specified by the right operand.  << Shifts the left operand left by the number of bits that is specified by the right operand.  ~ Performs a 1s complement of the operator. This is a unary operator. 

相关阅读
栏目导航
推荐软件