cheng029 发表于 2015-12-28 12:37:07

perl 初级教程1

  每个perl程序的第一行都是:
  #!/usr/local/bin/perl

  注释和语句:
  用#符号可以在程序中插入注释,并且从#开始到这行结尾都被忽略(除了第一行)。把注释扩展到几行的唯一办法是在每行前都用#符号。
  Perl中的语句必须在结尾加一个分号,象上面程序中最后一行那样。
++++++++++++++++++++++++++++++++++++++++
Perl中最基本的变量是标量。标量可以是字符串或数字,而且字符串和数字可以互换。例如,语句
  $priority = 9;
  设置标量$priority为9,但是也可以设置它为字符串:
  $priority = 'high';
  Perl也接受以字符串表示的数字,如下:
  $priority = '9'; $default = '0009';
  而且可以接受算术和其它操作。

  一般来说,变量由数字、字母和下划线组成,但是不能以数字开始,而且$_是一个特殊变量,我们以后会提到。同时,Perl是大小写敏感的,所以$a和$A是不同的变量。
  
  操作符和赋值语句:
  Perl使用所有的C常用的操作符:

$a = 1 + 2;# Add 1 and 2 and store in $a
$a = 3 - 4;# Subtract 4 from 3 and store in $a
$a = 5 * 6;# Multiply 5 and 6
$a = 7 / 8;# Divide 7 by 8 to give 0.875
$a = 9 ** 10;# Nine to the power of 10 ----9的10次方
$a = 5 % 2;# Remainder of 5 divided by 2 ---- 5 除以 2的余数,5 和 % 和2 之间要有空格,句末要有;
++$a;# Increment $a and then return it
$a++;# Return $a and then increment it
--$a;# Decrement $a and then return it
$a--;# Return $a and then decrement it

  
对于字符串,Perl有自己的操作符:

$a = $b . $c;# Concatenate $b and $c
$a = $b x $c;# $b repeated $c times

  ******************
$c = 'a';
$d = 'd';
$e = $c . $d;
print $e;----结果是ad
$c = 'a';
$d = 3;
$e = $c x $d;---x要小写的,不能是大写的
print $e;----结果是aaa--重复3 次
******************
Perl的赋值语句包括:

$a = $b;# Assign $b to $a
$a += $b;# Add $b to $a
$a -= $b;# Subtract $b from $a
$a .= $b;# Append $b onto $a

  $c = 4;
$c += $c;
print $c;   -----结果为8
$a = 4;
$c = 1;
$c += $a;
print $c;   -----结果为5
$a = 4;
$c = 1;
$c .= $a;
print $c; --------结果为14
$a = a;
$c = 1;
$c .= $a;
print $c; --------结果为1a   
++++++++++++++++++++++++++++++++++++++++++
页: [1]
查看完整版本: perl 初级教程1