Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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)

  • As pointed out by choroba here, when the Perl compiler sees the parens while parsing, it deduces that it is a subroutine call, even before it has seen the subroutine definition ... while without parens, a strict compile fails because the function name is an unrecognized bareword (Bareword "SomeFn" not allowed while "strict subs" in use).
  • For static parseability, parser toolkit Guacamole's standard mandates "All subroutines must use parentheses" (see also Re^8: poll ideas quest 2021).
  • Indirect Object Syntax (from perldoc perlobj) - see also Indirect Object Syntax by Bod from (working class) Coventry, UK (though he hails from posh Warwick).
  • Re^2: Correct keys in hashes by Aristotle - who notes that $foo{name} is always interpreted as $foo{'name'} and you need $foo{name()} to indicate a function call ... and you should get in the habit of never calling functions without parens.


In reply to Re: New Discovery!!! (sub call without parentheses) - Coding Style by eyepopslikeamosquito
in thread New Discovery!!! (sub call without parentheses) by harangzsolt33

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-28 13:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found