jackyrar 发表于 2015-12-26 09:39:40

[转

  【原文地址】 Perl初级教程
  注-- 本文UNIX系统环境为 SunOS 5.1 Generic# uname -a
  
  1.   创建UNIX文件
  touch PerlFile
  
  2. 修改UNIX文件的存取权限
  chmod 777 PerlFile
  




chmod 600 ××× (只有所有者有读和写的权限)
chmod 644 ××× (所有者有读和写的权限,组用户只有读的权限)
chmod 700 ××× (只有所有者有读和写以及执行的权限)
chmod 666 ××× (每个人都有读和写的权限)
chmod 777 ××× (每个人都有读和写以及执行的权限)
  
  
  3. 使用UE打开文件,写进代码。
  




#!/usr/local/bin/perl
#
# Program to do the obvious
#
print 'Hello world. \n';      # Print a message

                           # 此处 可以比较使用双引号与单引号,在打印时的效果是不一样的。 只有使用双引号时系统才实别为字符串处理,并将 \n 作为输出换行。


  
  
  1)每个perl程序的第一行都是:
  #!/usr/local/bin/perl- 虽然随着系统的不同而不同。这行告诉机器当文件执行时该怎么做(即告诉它通过Perl运行这个文件)。
  2)
  注释和语句:
  用#符号可以在程序中插入注释,并且从#开始到这行结尾都被忽略(除了第一行).
  块 注释
  =coment start
    代码....................
  =cut
  3)
  Perl中的语句必须在结尾加一个分号,象上面程序中最后一行那样。
  4)
  简单的打印:
  print函数输出一些信息。在上面的例子中它打印出字符串Hello world‘。
  
  
  【运行程序】
  perl PerlFile ./PerlFile PerlFile
  
  1) warning参数运行程序:
  perl -w progname
  这会在试图执行程序前显示警告和其它帮助信息。用调试器运行程序可以使用命令:
  2) perl -d progname
  当执行程序时,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) = 1
++$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

  
  
  
页: [1]
查看完整版本: [转