3

I built a simple subroutine, and I have a question about whether calling it requires parentheses.

#!/usr/bin/perl
sub echo {
    print "@_ \n" ;
}
echo(@ARGV);

When I use

echo @ARGV

or

echo (@ARGV)

or (without a space)

echo(@ARGV)

they all work. Which one is correct?

mklement0
  • 382,024
  • 64
  • 607
  • 775
capser
  • 2,442
  • 5
  • 42
  • 74
  • 3
    [TMTOWTDI](https://en.wikipedia.org/wiki/There%27s_more_than_one_way_to_do_it)? – Benjamin W. Feb 17 '16 at 04:15
  • 1
    `perldoc perlstyle` recommends `No space between function name and its opening parenthesis.` Using parentheses makes it clear that it is a user-defined function, as opposed to a built-in. – xxfelixxx Feb 17 '16 at 04:31
  • 1
    @xxfelixxx: Good info re space, but just to be clear: I assume you're recommending adopting the _convention_ of calling all user-defined functions _with_ parentheses, and built-ins _without_ (technically, however, built-ins _can_ be called _with_ parentheses, and user-defined functions _can_ be called _without_ parentheses, if predeclared). – mklement0 Feb 17 '16 at 05:21
  • 1
    The linked question that some consider this question to be a duplicate of has a different focus: The linked question knows under what circumstances parentheses are needed, and asks _why_ that is so. By contrast, _this_ question asks about _whether_ parentheses are needed, whether a space in front of the opening parentheses matters, and when to choose which invocation style. – mklement0 Feb 17 '16 at 16:13
  • 1
    @mklement0 Agreed, voted to reopen. I'm really surprised there isn't a suitable duplicate for this. – ThisSuitIsBlackNot Feb 17 '16 at 16:57

2 Answers2

5

echo @ARGV, echo (@ARGV), and echo(@ARGV) are technically all correct in your case, but using parentheses is sometimes necessary; beyond that, it's a matter of choice, and some people employ conventions around when to use what style.

  • Enclosing the entire argument list in parentheses ALWAYS works - whether spaces precede the opening parenthesis or not - echo (@ARGV) or echo(@ARGV) - and whether you're calling a built-in or user-defined function (subroutine):

    • As @xxfelixxx notes in a comment on the question, perldoc perlstyle recommends no spaces between the function name and ( - echo(@ARGV).
    • Enclosing the entire argument list in parentheses can be used to disambiguate:
      • print (1 + 2) + 4 prints only 3, because (1 + 2) is interpreted as the entire argument list (with + 4 added to the the expression value of the print call, the result of which is not output).
      • print((1 + 2) + 4) resolves the ambiguity and prints 7.
      • Alternatively, prefix the parenthesized first argument with + to achieve the same effect: print +(1 + 2) + 4 also prints 7.
  • Not using parentheses - echo @ARGV - works:

    • with built-in functions: always
    • with user-defined functions: ONLY if they are predeclared, which can be ensured in one of the following ways:
      • The function is defined in the same script before its invocation.
      • The function is forward-declared with sub <name>; before its invocation.
      • The function is imported from a module with use before its invocation (require is not enough).
    • This predeclaration requirement is is an unfortunate side effect of backward compatibility with the earliest Perl versions - see this answer.
    • In the absence of a predeclaration, the safest approach is to use parentheses (while &echo @ARGV works in principle, it bypasses any prototypes (a form of parameter typing) that the function may declare).

As for conventions:

  • Since using parentheses always works (even when not strictly needed) with user-defined functions, whereas they are never needed for built-in functions, some people recommend always using parentheses with user-defined functions, and never with built-in functions.
    In source code that adheres to this convention, looking at any function call then tells you whether a built-on or user-defined function is being invoked.
Community
  • 1
  • 1
mklement0
  • 382,024
  • 64
  • 607
  • 775
2

The parens are optional. You need them in some situations to explicitly show which values are the arguments to the function, for example, when passing the result of a function call to another function:

myfunc(1, 2, otherfunc(3), "z");

Without parens around the 3, otherfunc will receive both 3 and "z" as arguments.

As xxfelixxx mentioned, it's best to use them all the time.

Andy Schweig
  • 6,597
  • 2
  • 16
  • 22