简短的总结:“讨厌的 perl 代码”。
更长的答案:
sub update_dialer {
# take a global variable $lref.
# scope it locally it can be modified within the local subroutine
# run the sub "read_file_lines" and pass it the contents of `$config{'file'}
# assign the result to $lref
local $lref = &read_file_lines( $config{'file'} );
#removes elements from an array.
#Number based on a hash reference passed in as the first argument in @_
#but is also calling the dialer_lines subroutine as part of it.
splice(
@$lref, $_[0]->{'line'},
$_[0]->{'eline'} - $_[0]->{'line'} + 1,
&dialer_lines( $_[0] )
);
#run the sub 'flush_file_lines'.
&flush_file_lines();
}
它正在实施一系列不良做法:
手册页指出了这有什么问题:
您可能真的想改用 my,因为 local 并不是大多数人认为的“本地”。 See Private Variables via my() in perlsub for details.
local 作为一种临时覆盖全局变量的方式存在。它很有用 - 例如 - 如果您想更改输入记录分隔符 $/。
你可以这样做:
{
local $/ = "\n\n";
my $first_record = <$filehandle>;
print $first_record;
}
这意味着一旦你退出你的代码块,$/ 将返回到它的原始值,并且不会在你的代码中搞砸文件 IO 的所有其余部分。在此示例中没有充分的理由像这样使用它。
& 子程序前缀
Difference between &function and function() in perl
用& 为子添加前缀会做一些你几乎从未真正想要的事情,因为它会弄乱原型设计之类的东西。因此,您通常不应该这样做。
删除和替换数组中的元素。不是特别错误,而是它以它的方式做这件事的事实使得很难告诉它在做什么。
(但我认为因为它正在本地化$lref,它的值在这个子的末尾消失了。
$_[0] -> {'line'}
当一个 sub 被调用时,它会传递一个数组 @_ 和函数的参数。您可以使用 $_[0] 访问此数组的第一个元素 - 它 NOT 与 $_ 相同,因为所有原因 $list[0] 和 $list 不一样。
它的使用方式 - $_[0] -> {'line'} 告诉我们这是一个哈希引用,它被取消引用以访问某些变量。
但这并不完全是在创建漂亮且可读的代码。
你可以这样做:
my ( $parameter_ref ) = @_;
或许:
my $parameter_ref = shift;
这里的口味问题 - 默认情况下,shift 使用 @_ 与许多其他函数默认使用 $_ 的方式大致相同。不过我更喜欢前者。
但是通过命名参数,您可以清楚地了解它们是什么以及它们在做什么。