<2>集腋成裘

时间:2023-03-08 20:45:27
标量项:

[root@wx03 2]# cat a1.pl
unshift(@INC,"/root/big/2");
use Horse;;
print $Horse::days;
print "\n";
[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\n";
print "\$::bert is $::bert\n";
print $main::bert;
print "\n";
[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 "\n"; @arr=qx/ls -ltr/;
print @arr;
print "\n"; [root@wx03 2]# cat a5.pl
print __LINE__;
print "\n";
print __FILE__;
print "\n";
print __LINE__;
print "\n";
[root@wx03 2]# perl a5.pl
1
a5.pl
5 __LINE__ 当前行号 __FILE__ 当前文件 [root@wx03 2]# cat a6.pl
print __PACKAGE__;
print "\n";
[root@wx03 2]# perl a6.pl
main 当前包为main包 [root@wx03 2]# cat a7.pl
print "111111111\n";
print "222222222\n";
print __END__;
print "333333333\n";
[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\n"; @x=funkshun();
print "\@x is @x\n"; [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\n";
[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\n"; $stuff = @stuff;
print "\$stuff is $stuff\n";
[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 "\n";
print $arr[6];
print "\n";
[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 "\n";
[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 "\n";
[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\n"; print "\$fh is $$fh\n"; [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\n"; print "\$fh is $$fh\n"; $bar="bbb";
*foo = \$bar;
print "\$foo is $foo\n"; $a1="aaaa111";
*foo=\$a1;
print $foo;
print "\n";
[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 "\n"; print $foo;
print "\n";
[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