Perl calling methods with and without parentheses
Some Perl books advise using parentheses when calling class methods, saying that it helps prevent the parser from guessing the intent of the code. However, almost all Perl code I've seen (including cpan modules) very rarely uses parentheses when calling methods with no arguments.
Is it okay to leave those parentheses or should I always type them.
I wrote a little test code to measure the difference between calling a method with and without parentheses, and it does show a small difference between 1% and 2% for a class with two ways. I guess it might be uplifting if the class is large.
here is the test script I used for comparison:
#!/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 },
});
and these are the test results:
Rate without () with ()
without () 3320053/s -- -1%
with () 3338898/s 1% --
I am guessing the application uses hundreds of modules and each module has hundreds of methods called without parentheses, if that adds a lot of speed difference?
If so why is everything encoded without parentheses?
source to share
The 1% difference is system noise. Both versions boil down to the same bytecode, so there can be no systematic difference between them. Use the option that makes the code easier to read.
If you want to see what it compiles, you can do it like this:
perl -MO=Concise -E '$foo->bar()'
source to share
I'm not sure if the 1% difference is significant, and I doubt you can measure any difference in a real program.
Some people think that at the end of a method call it looks like no help ()
. It's enough. You will also see it with features.
For me, I try to do this when I want to indicate "no parameters here". Basically only with getter attributes, etc.
If the method can take optional parameters, and they just default, then I prefer not to, so I can distinguish between "no parameters" and "I did not provide parameters, but could have".
source to share
I think there is a systematic error in your measurement. When I run your code, it without ()
hits with ()
every time. But when I use strings of the same length in cmpthese
for example:
cmpthese(10_000_000, {
'with ()' => sub { $class->message() },
'without ()' => sub { $class->message },
});
I got these (rather expected) results:
Rate with () without ()
with () 3412969/s -- -1%
without () 3460208/s 1% --
Rate without () with ()
without () 2994012/s -- -0%
with () 3003003/s 0% --
Rate without () with ()
without () 3278689/s -- -1%
with () 3300330/s 1% --
Rate with () without ()
with () 3039514/s -- -2%
without () 3105590/s 2% --
Rate without () with ()
without () 3267974/s -- -3%
with () 3378378/s 3% --
So I think this is only about best practices.
source to share
Because you can. Indeed. I think. Because Perl allows so much, people use it. Sometimes for the good, as you can write very complex things in short sequences, and sometimes for the worse as it often gets complicated (especially for people new to Perl).
It also depends on what your program is doing. The difference should only be at compile time (I guess). Compile time is often a short part of the entire application life cycle, where it doesn't really matter.
source to share