大骆驼 第二章

时间:2023-01-26 10:34:50
perl 里的语句是用分号结束的,


数值相等用==


2.4 变量:


$days 简称标量值 $days


$days[28] 数组@days 的第29个元素




$day{'Feb'} 散列%days 的“Feb"值


${days} 和$days一样,不过在字母数字前面不易混淆


$Dog::days 在Dog包里面的不同的$days变量




$#days 数组@days的最后一个索引




$days->[28] $days一个引用指向的数组的第29个元素




$days[0][2] 多维数组












defined 函数时询问一个标量是否为undef,不能用于数组


2.9 散列数组:
[oracle@jhoa big]$ cat 1.pl
%map = (
red => 0xff0000,
green => 0x00ff00,
blue => 0x0000ff,
);
print %map;
print "\n";
[oracle@jhoa big]$ perl 1.pl
green65280blue255red16711680






2.10 型团(typeglob) 和文件句柄




2、11、1 命令输入(反勾号)操作符


[oracle@jhoa big]$ cat 2.pl
$perl_info = qx(ps $$);
print "\$perl_info is $perl_info\n";
[oracle@jhoa big]$ perl 2.pl
$perl_info is PID TTY STAT TIME COMMAND
17411 pts/0 S+ 0:00 perl 2.pl


这里的$$ 是perl处理的对象


2.11.2 行输入(尖角)操作符
[oracle@jhoa big]$ cat 3.pl
$var=<>;
print "\$var is $var\n";
[oracle@jhoa big]$ perl 3.pl
111
$var is 111


[oracle@jhoa big]$ cat a.tmp
aaaaaaa
2222222
3333333
[oracle@jhoa big]$ perl 3.pl
1111111
$var is 1111111


is aaaaaaa
[oracle@jhoa big]$
[oracle@jhoa big]$ cat 3.pl
$var=<>;
print "\$var is $var\n";




open (C,"<","a.tmp") ;
$count = <C>;
chomp($count);
print "$a is $count\n";










[oracle@jhoa 2]$ cat a1.pl
while (glob "*.pl"){ print "\$_ is $_\n"};


[oracle@jhoa 2]$ perl a1.pl
$_ is 1.pl
$_ is 2.pl
$_ is 3.pl
$_ is 4.pl
$_ is 5.pl
$_ is 6.pl
$_ is 7.pl
$_ is a1.pl




等价于:
[oracle@jhoa 2]$ cat a2.pl
while (<*.pl>){ print "\$_ is $_\n"};
[oracle@jhoa 2]$ perl a2.pl
$_ is 1.pl
$_ is 2.pl
$_ is 3.pl
$_ is 4.pl
$_ is 5.pl
$_ is 6.pl
$_ is 7.pl
$_ is a1.pl
$_ is a2.pl