<2>集腋成裘

标量项:

[root@wx03 2]# cat a1.pl 
unshift(@INC,"/root/big/2"); 
use Horse;;
print $Horse::days;
print "
";
[root@wx03 2]# cat Horse.pm 
package Horse;
our $days="test111";
1;

$Horse::days; 访问Horse 包中的变量$days


有时候你想为"所有名叫foo的东西"(无论他的印记是什么)起一个名字

。为此, 符号团记录可以有一个*作为前缀,这里的星号(*)表示所有其

他的印记。

这些称为类型团(typeglobs)


perl main包:

[root@wx03 2]# cat a2.pl 
$bert="aa";
print "$bert is $bert
";
print "$::bert is $::bert
";
print $main::bert;
print "
";
[root@wx03 2]# perl a2.pl 
$bert is aa
$::bert is aa
aa

2.6.3  选择自己的引号:

` ` qx// 执行命令 是

[root@wx03 2]# cat a4.pl 
@arr=`ls -ltr`;
print @arr;
print "
";

@arr=qx/ls -ltr/;
print @arr;
print "
";


[root@wx03 2]# cat a5.pl 
print __LINE__;
print "
";
print __FILE__;
print "
";
print __LINE__;
print "
";
[root@wx03 2]# perl a5.pl 
1
a5.pl
5

__LINE__  当前行号

__FILE__  当前文件


[root@wx03 2]# cat a6.pl 
print __PACKAGE__;
print "
";
[root@wx03 2]# perl a6.pl 
main

当前包为main包


[root@wx03 2]# cat a7.pl 
print "111111111
";
print "222222222
";
print __END__;
print "333333333
";
[root@wx03 2]# perl a7.pl 
111111111
222222222
[root@wx03 2]# 

END 可以用于真正的文件结束符之前表示搅拌的逻辑结束,

任何后面的文本都被忽略


[root@wx03 2]# cat a8.pl 
sub funkshun{
return(a1,a2,a3)};

$x=funkshun();

print "$x is $x
";

@x=funkshun();
print "@x is @x
";



[root@wx03 2]# perl a8.pl 
$x is a3
@x is a1 a2 a3

2.7.3  空(void )环境:

[root@wx03 2]# cat a9.pl 
$stuff = ( "one", "two", "three");
print "$stuff is $stuff
";
[root@wx03 2]# perl a9.pl 
$stuff is three

只是把值"three" 赋予了变量$stuff


[root@wx03 2]# cat a10.pl 
@stuff = ("one", "two", "three"); 
print "@stuff is @stuff
";

$stuff = @stuff;
print "$stuff is $stuff
";
[root@wx03 2]# perl a10.pl 
@stuff is one two three
$stuff is 3

[root@wx03 2]# cat a11.pl 
@stuff=(1,2,3);
@nonsense=(4,5,6);
sub funkshun{
return 100;

};

@arr=(@stuff,@nonsense,funkshun());
print @arr;
print "
";
print $arr[6];
print "
";
[root@wx03 2]# perl a11.pl 
123456100
100



[root@wx03 2]# cat a12.pl 
%map = (
red => 0xff0000,
green => 0x00ff00,
blue => 0x0000ff,
);

print $map{red};
print "
";
[root@wx03 2]# perl a12.pl 
16711680


初始化任何当作记录使用的匿名散列引用:

[root@wx03 2]# cat t1.pl 
$rec = {
NAME => 'John Simth',
RANK => 'Captain',
SERNO => '951413',
};

print $rec->{NAME};
print "
";
[root@wx03 2]# perl t1.pl 
John Simth

或者用命名的参数激活复杂的函数:



2.10  型团(typeglob)

Perl 里面有种特殊的类型叫类型团(typeglob)用以保留整个符号表

记录。(符号表记录
*foo 包括 $foo, @foo, %foo,&foo 和其他几个 foo 的简单解释

值。)类型团
(typeglob)的类型前缀上一个 *,因为它代表所有类型。

[root@wx03 2]# cat t3.pl 
$fh=*var;
$var=aa;
print "$fh is $fh
";

print "$fh is $$fh
";

[root@wx03 2]# perl t3.pl 
$fh is *main::var
$fh is aa


[root@wx03 2]# cat t3.pl 
$fh=*var;
$var=aa;
print "$fh is $fh
";

print "$fh is $$fh
";

$bar="bbb";
*foo = $bar;
print "$foo is $foo
";


$a1="aaaa111";
*foo=$a1;
print $foo;
print "
";
[root@wx03 2]# perl t3.pl 
$fh is *main::var
$fh is aa
$foo is bbb
aaaa111

符号表就是引用:

[root@wx03 2]# cat t4.pl 
$bar="abc123";
*foo=$bar;
print *foo;
print "
";

print $foo;
print "
";
[root@wx03 2]# perl t4.pl 
*main::foo
abc123





2.11.2  行输入(尖角)操作符
[root@wx03 2]# cat t5.pl 
while ($_ = <STDIN>) { print $_; };
[root@wx03 2]# perl t5.pl 
1
1
33
33

原文地址:https://www.cnblogs.com/zhaoyangjian724/p/6199989.html