Chinaunix首页 | 论坛 | 博客
  • 博客访问: 128902
  • 博文数量: 89
  • 博客积分: 2580
  • 博客等级: 少校
  • 技术积分: 775
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-05 20:09
文章分类

全部博文(89)

文章存档

2009年(89)

我的朋友

分类:

2009-01-05 20:42:47



1. Perl is a free-form language, whitespace mostly serves to separate tokens(unlike python). Do What I Mean: you can often leave such explicit elements off and Perl will figure out what you meant.

个人感悟:就我现在对perl的浅显认识,确实,perl是个自由度很高的语言,为了达到同样的效果,可以有好几种语句的写法。上文所说的whitespace只作为token的分隔符,也是平时想当然使用了很久的功能。

2. Declaration。 perl只需申明report formats跟子程序(有时候子程序也不需要申明)。声明可以放在程序的任何地方,不影响结果。申明在编译时期生效。

3. 一个变量在赋值之前的值为undef,被用作数字时是0;被用作string的时候是"";当被用作引用时,报error;使用warning可以避免未初始化的变量。特例:boolean上下文,及用于++ , -- , += , -= , .=等符号。


4. The number 0, the strings '0' and '' , the empty list () , and undef 在boolean的上下文中都是FALSE

5.You can always put another block inside of it (for ) or around it (for ) to do that sort of thing

do {{
    next if $x == $y;
    
# do something here

    }} until $x++ > $z;

For , you have to be more elaborate:

LOOP: {
        do {
        last if $x = $y**2;
        
# do something here

        } while $x++ <= $z;
    }



6. a sequence of statements that defines a scope is called a block
 

7. 由于逻辑控制体是由block定义的(以避免if跟else的配对问题),因此不能省略{},如需变形,有以下几种可选方式。

 

if (!open(FOO)) { die "Can't open $FOO: $!"; }
    die "Can't open $FOO: $!" unless open(FOO);
    open(FOO) or die "Can't open $FOO: $!"; # FOO or bust!

    open(FOO) ? 'hi mom' : die "Can't open $FOO: $!";
            

8. 

The  command starts the next iteration of the loop

The  command immediately exits the loop in question. The  block, if any, is not executed

The  command restarts the loop block without evaluating the conditional again. The  block, if any, is not executed. 

 

9. 循环控制语句(next,last,redo等)不对if,unless起作用,如需使用需多加一层{},由于block本身是一个执行一遍的loop。

 

foreach var (LIST){}

 

10. Foreach中的VAR的申明,如果带my则作用域在循环内;如果不带my,默认为local,在循环结束时赋回原值;如果变量在循环前已经被申明为my,则使用此变量的值,而不是全局变量的值,类型仍为local。 总之:foreach对于var在循环外的值没有影响。

11. 如果LIST中的值可以为左值,则在循环中对var的修改会体现在LIST中。(但不要对LIST中的数组做添加或删除。)

 

Switch statements

 

12. 5.10开始提供switch功能。可以通过

Use feature "switch"; #或者

Use feature ":5.10"; #来开启

13. 基本用法:其中$var的值会自动赋给$_;每个when隐含以break结束;when的匹配使用smart matching。

 

given($var) {
    when (/^abc/) { $abc = 1; }
    when (/^def/) { $def = 1; }
    when (/^xyz/) { $xyz = 1; }
    default { $nothing = 1; }
    }

13.  When的特性也可用于loop中

my $count = 0;
    for (@array) {
    when ("foo") { ++$count }
    }
    print "\@array contains $count copies of 'foo'\n";

smart matching

13.  详见:;可以重载~~操作符,实现自定义比较;perl6中的smart matching与版本5中的定义有别。

 

PODs:enbedded documentation

14.  行首为"= word" 到"=cut" 之间的语句都被认为是comment

阅读(573) | 评论(0) | 转发(0) |
0

上一篇:Perldoc阅读笔记

下一篇:perldata 读书笔记

给主人留下些什么吧!~~