《PERL高效编程》学习笔记(1)【关于裸字处理】
[*]裸字处理
PERL的诗歌模式会将不产生歧义的标识符当做字符串对待。
use 5.010;
use strict 'vars';
my @array = qw(a b c d e f);
for(my $i = 0; $i < 10; $i++)
{
say $array; #这里i本意是写成$i
}
结果:
只需启用约束,即可防止此类错误。
use 5.010;
use strict 'vars';
use strict 'subs'; #启用约束
my @array = qw(a b c d e f);
for(my $i = 0; $i < 10; $i++)
{
say $array;
}
但裸字在关闭诗歌模式下仍然适用于散列。
use 5.010;
use strict 'vars';
use strict 'subs';
my %h = (
first => 'Hello',
last => 'World');
say $h{first};
say $h{last};
结果:
附:对PERL函数做一次备忘
use 5.010;
say hi("world");
say $main::a;
sub hi{
say @_;
$a = shift;
return "hi, $a";
}
页:
[1]