源过滤器是坏的并且不应该在生产代码中使用是"常识" .
在回答类似但更具体的问题时,我找不到任何好的参考资料,清楚地解释了为什么过滤器是坏的以及何时可以安全使用.我想现在是时候创造一个了.
一些Perl书籍建议在调用类方法时使用括号,这说明这有助于使解析器不必猜测代码的意图.但是,我所看到的几乎所有Perl代码(包括cpan上的模块)在调用没有参数的方法时很少使用括号.
放下这些括号是否正常,或者我应该总是输入它们.
我编写了一个小测试代码来衡量调用带括号和无括号的方法之间的区别,并且它确实显示了仅使用两种方法的类在1%和2%之间的微小差异.我想如果班级很大,这可能会增加.
这是我用来测试的测试脚本:
#!/usr/bin/perl
use Benchmark qw(:all);
{
package Messages;
sub new {
my ($self) = @_;
return bless {}, $self;
}
sub message {
my ($self) = @_;
return "Hello world";
}
sub another {
my ($self) = @_;
return "Another Hello world";
}
}
my $class = Messages->new();
cmpthese(10_000_000, {
'with () ' => sub { $class->message() },
'without () ' => sub { $class->message },
});
Run Code Online (Sandbox Code Playgroud)
这是基准测试的结果:
Rate without () with ()
without () 3320053/s -- …Run Code Online (Sandbox Code Playgroud)