Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the reason to use parenthesis-less subroutine calls in Perl? [closed]

As per perldoc perlsub:

The & is optional in modern Perl, as are parentheses if the subroutine has been predeclared.

I notice that a lot of times, people use the fact that you can omit parenthesis when calling Perl subroutines (e.g., randomly quoting from a recent SO answer):

open my $fin, '<', $file;

is as equally valid as

open(my $fin, '<', $file);

What are the main (ideally, technical) reasons to use the second (parenthesis-less) version?

perldoc perlsyn doesn't say anything on the topic other than again mention optionality.

For me, always using the parenthesis are mostly a stylistic choice due to my origins as a C developer; but I'd like to know if I'm missing something out on not using the optional parenthesis-less syntax as a Perl developer.


P.S. I know full well the reasons to prefer the version with parenthesis - e.g. the issues with indirect object notation, or requirement to declare non-builtins before they are used without parenthesis, or issues with precedence visavi or vs ||. I'm interested in the opposite.


P.P.S. I'm not greatly interested in answers merely stating "it's better style"/"more readable" without any studies backing the opinion up. I'm interested in either technical reasons to prefer parenthesis omission, or backed up stylistic difference preferences (Please don't confuse "backed up" with "appeal to authority" or "argumentum ad populum" fallacies. A study showing improvement in speed or comprehension of code is proof. "Everyone in Perl commmunity agrees" or "Damien Conway suggests this" without explaining how Damien backs this up is not).

like image 345
DVK Avatar asked Apr 02 '13 19:04

DVK


3 Answers

I think the only time that it actually matters, other than style, is for the rarely used &subname which according to perldoc perlsub:

&NAME;     # Makes current @_ visible to called subroutine.

Of course, using parens might make for easier disambiguation in some cases (both for the parser and the future reader), or else not using them might remove noise in others; the degree to which either of those is necessary is probably the primary reason I would ever choose one or the other.

like image 99
Joel Berger Avatar answered Nov 16 '22 15:11

Joel Berger


From Perl::Critic::Policy::CodeLayout::ProhibitParensWithBuiltins:

Conway suggests that all built-in functions be called without parentheses around the argument list. This reduces visual clutter and disambiguates built-in functions from user functions.

Conway is the author of the Perl Best Practices book.

like image 33
toolic Avatar answered Nov 16 '22 15:11

toolic


There are no technical reasons to omit parentheses. It's purely a matter of preferred coding style.

like image 7
Dave Sherohman Avatar answered Nov 16 '22 16:11

Dave Sherohman