Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: New Discovery!!! (sub call without parentheses) - Coding Style

by eyepopslikeamosquito (Bishop)
on Dec 08, 2018 at 17:46 UTC ( #1226973=note: print w/replies, xml ) Need Help??


in reply to New Discovery!!! (sub call without parentheses)

I always use parens when calling user-defined subroutines and methods because:

  • The consistency of always using parens when calling user-defined subroutines/methods makes the code easier to read (for me).
  • The code is more robust when reorganized. For example, if you later switch from use (compile-time) to require (run-time) -- to speed initial module load or when porting to a new platform, say -- your code may break in surprising ways. Even if you can "easily" fix the code to use parens (so that it will parse correctly with require), doing so risks breaking what might be thousands of lines of working production code ... so avoid that future pain by always using parens in the first place.

As a matter of style, some folks find the code easier to read when user-defined functions are always called with parens and built-in functions always called without parens. Perl Best Practices endorses this style:

  • Don’t use unnecessary parentheses for builtins and ‘honorary’ builtins (item 4)
  • Call subroutines with parentheses but without a leading & (item 113)

Note that:

use SomeModule (); is equivalent to: require SomeModule;

while:

use SomeModule; is equivalent to: require SomeModule; SomeModule->import();

with use performed at compile time, require at run time. See perlmod for details.

See Also (Update 2021)

  • Comment on Re: New Discovery!!! (sub call without parentheses) - Coding Style

Replies are listed 'Best First'.
Re^2: New Discovery!!! (sub call without parentheses)
by stevieb (Canon) on Dec 08, 2018 at 23:00 UTC

    Two very good points there.

    I always use parens on my user-defined functions whether I've got parameters or not. Kind of a habit actually now that I'm fluent in more than just a couple of languages, most of which enforce using parens. In fact, thinking about it, I think Perl's the only language which *doesn't* enforce parens if a function is pre-declared. Also, at a glance (at least with my own code), it's easier to identify built-ins, as I typically don't use parens for them (eg: sleep 10;).

    The only time I'll leave parens off of a call, is if it's a method call where I'm not sending in any params:

    my $object = My::Thing->new(speak => 'hi'); ... $object->speak;
      > I think Perl's the only language which doesn't enforce parens if a function is pre-declared

      Ruby never does, but probably you wanted to say "if and only if" ?

      The possibility to omit parenthesis is essential for many syntactic sugar and DSL approaches.

      Compare "has" in Moose which looks like a keyword.

      update

      example

      has 'first_name' => ( is => 'rw', isa => 'Str', );

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re^2: New Discovery!!! (sub call without parentheses)
by LanX (Sage) on Dec 08, 2018 at 19:25 UTC
    Yes, but with exceptions.

    When designing syntactic sugar and/or DSLs its useful to leave the parentheses off.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1226973]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2021-12-01 01:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?