perl入门网站和几个好玩的地方

http://www.cbi.pku.edu.cn/chinese/documents/perl/index.htm
--------------------------
.双引号内的字符串中支持转义字符
Table 3.1. Escape sequences in strings.

Escape Sequence Description
\a Bell (beep)
\b Backspace
\cn The Ctrl+n character
\e Escape
\E Ends the effect of \L, \U or \Q
\f Form feed
\l Forces the next letter into lowercase
\L All following letters are lowercase
\n Newline
\r Carriage return
\Q Do not look for special pattern characters
\t Tab
\u Force next letter into uppercase
\U All following letters are uppercase
\v Vertical tab
\L、\U、\Q功能可以由\E关闭掉,如:
$a = "T\LHIS IS A \ESTRING"; # same as "This is a STRING"
------------------------
列表/数组的长度
  当数组变量出现在预期简单变量出现的地方,则PERL解释器取其长度。
    @array = (1, 2, 3);
    $scalar = @array; # $scalar = 3,即@array的长度
    ($scalar) = @array; # $scalar = 1,即@array第一个元素的值
  注:以数组的长度为循环次数可如下编程:
    $count = 1;
    while ($count <= @array) {
    print ("element $count: $array[$count-1]\n");
    $count++;
    }
-------------------------------
@array = (70, 100, 8);
@array = sort(@array); # @array = (100, 70, 8) now
@array2 = reverse(@array);
@array2 = reverse sort (@array);
chop--数组去尾
join/split--连接/拆分

-------------------------流----------
读:open (MYFILE, "file1") || die ("Could not open file");
写:open(OUTFILE, ">outfile");
    print OUTFILE ("Here is an output line.\n");
关闭:close(MYFILE); 关闭文件
-------------------------------shell也有这个判断------------
open(INFILE, "infile") && !(-e "outfile") &&
    open(OUTFILE, ">outfile") || die("Cannot open files\n");
文件测试操作符

操作符	描述
-b	 是否为块设备
-c	 是否为字符设备
-d	 是否为目录
-e	 是否存在
-f	 是否为普通文件
-g	 是否设置了setgid位
-k	 是否设置了sticky位
-l	 是否为符号链接
-o	 是否拥有该文件
-p	 是否为管道
-r	 是否可读
-s	 是否非空
-t	 是否表示终端
-u	 是否设置了setuid位
-w	 是否可写
-x	 是否可执行
-z	 是否为空文件
-A	 距上次访问多长时间
-B	 是否为二进制文件
-C	 距上次访问文件的inode多长时间
-M	 距上次修改多长时间
-O	 是否只为“真正的用户”所拥有
-R	 是否只有“真正的用户”可读
-S	 是否为socket
-T	 是否为文本文件
-W	 是否只有"真正的用户"可写
-X	 是否只有"真正的用户"可执行
注:“真正的用户”指登录时指定的userid,与当前进程用户ID相对,命令suid可以改变有效用户ID。

-----这个有意思,突然想结合abs一起看-(同时学好几门语言其实比只学一门语言要简单)------------
象C一样,PERL也有存储命令行参数的数组@ARGV,可以用来分别处理各个命令行参数;与C不同的是,$ARGV[0]是第一个参数,而不是程序名本身。
    $var = $ARGV[0]; # 第一个参数
    $numargs = @ARGV; # 参数的个数
  PERL中,<>操作符实际上是对数组@ARGV的隐含的引用,其工作原理为:
1、当PERL解释器第一次看到<>时,打开以$ARGV[0]为文件名的文件;
2、执行动作shift(@ARGV); 即把数组@ARGV的元素向前移动一个,其元素数量即减少了一个。
3、<>操作符读取在第一步打开的文件中的所有行。
4、读完后,解释器回到第一步重复。
  例:
    @ARGV = ("myfile1", "myfile2"); #实际上由命令行参数赋值
    while ($line = <>) {
    print ($line);
    }
  将把文件myfile1和myfile2的内容打印出来。
----------------管道------------
open (MESSAGE, "| cat >hello");
    print MESSAGE ("Hi, Dave! Your Perl program sent this!\n");
    close (MESSAGE);

------------------------循环-------------
@list = (1, 2, 3, 4, 5);
  foreach $temp (@list) {
    if ($temp == 2) {
      $temp = 20;
    }
  }
----------------子程序----------
1、用&调用 
  &subname;
  ...
  sub subname{
    ...
  }
2、先定义后调用 ,可以省略&符号
  sub subname{
    ...
  }
  ...
  subname;
3、前向引用 ,先定义子程序名,后面再定义子程序体
  sub subname;
  ...
  subname;
  ...
  sub subname{
    ...
  }
4、用do调用 
  do my_sub(1, 2, 3);等价于&my_sub(1, 2, 3);


----------------别名--------------没看懂--perl的确是恶搞的语言
http://www.cbi.pku.edu.cn/chinese/documents/perl/perl8.htm
-------------构造函数 析构函数--
BEGIN {
    print("Hi! Welcome to Perl!\n");
  }
  AUTOLOAD{
    print("subroutine $AUTOLOAD not found\n"); # 变量$AUTOLOAD即未找到的子程序名
    print("arguments passed: @_\n");
  }
END{}

猜你喜欢

转载自haoningabc.iteye.com/blog/1092689
今日推荐