Thursday, February 3, 2011

Black Book

chap 1: Perl Basic


1#perl -e 'print "@INC";'
2#perl -
print "hello\n";
__END__
3#命令行开关1.2.8
4#perl -w xxx.pl ; 推荐
5#perl -c xxx.pl ; check the grammer.
6#while ($temp = ) {
print $temp;
} ; 阅读键盘输入
7#while (<>) {
print;
}; 默认变量 $_
等于:
while ($_ = ) {
print $_;
}

Chap 2
2.1# 3种数据类型: 标量$ 数组@ 哈希%


chap 3 array and hash

3.2.3 push and pop
@b = (1, 2, 3);
print "$b[1]\n";

push (@b, "4");
print "$b[-1]\n";
print $#b; #how many elements in the array

shift/unshift did the same job in opposite way.
splice is more flexible. it can insert the element in the middle.

splice (@b, 2, 0, "five");
print join (", ", @b);


3.2.10 Loop in array
more 3_2_10_Loop_in_Array.pl
@a = ("one", "two", "three");
for ($i=0; $i<=$#a; $i++) {
print "$a[$i]\n";
}

@b =(1,2,3,4,5);
foreach $element (@b) {
print "$element\n";
}


Chap 6 Regular Expression
RE的组成部分:
1,字符 m/text/g, 特殊字符 \w, \n
2,字符类 【A-Za-z】所有字符 [^A-Z] 非大写。^在【】中为否
3,量词 * + ?
4,断言 ^行首 $行尾巴 \b---\b单词边界


Chap 7. sub
7.1.3 return.
sub addem
{
($value1,$value2)=@_;
return $value1+$value2;
}
print "2+2=" .addem(2,2). "\n"; # DOT is the connector, refer to 4.2.9


7.2.14 MY与LOCAL的差别

$value=1;
sub printem () {print "\$value=$value\n"};
sub makelocal ()
{
local $value=2; #将全局变量本地化,离开时带走。
printem;
}
makelocal;
printem;

#====================
$value2=1;
sub printem2 () {print "\$value2=$value2\n"};
sub makelocal2 ()
{
my $value2=2; #MY创建新变量,无全局副作用。
printem2;
}
makelocal2;
printem2;



7.2.18 递归
sub factorial
{
my $value=shift (@_);
return $value==1 ? $value : $value * factorial ( $value-1); # if trun ? then xxx : else yyy
}
$result=factorial(6);
print $result;




CHAP 8. 格式和字符串处理

Perl: Practical Extraction and Reporting Language

8.1.1 perl format: 靠左,靠右,居中
$texta = "left";
$textb = "right";
$textc = "left2";
$textd = "left3";

format STDOUT =
@<<<<<<<<<<<<<<<<<<<<<<@>>>>>>>>>>>>>>>>>>
$texta $textb
.
write;

#format STDOUT =
#@<<<<<<<<<<<<<<<<<<<<<<@<<<<<<<<<<<<<<<<<
#$textc $textd
#.
#write;



8.2.1 perl here:
print <whatever
u
want
to
aprint the fortmat
EOD #no more lines after that, otherwise err "Bareword found where operator expected"

No comments: