Method calls require parentheses unless they take no arguments.

If you want one single rule about when to use parentheses, this is probably the closest thing you're going to get:

If Perl is able to figure out what sub is getting called at compile time, you don't usually need parentheses because Perl can use prototypes to figure out how to parse the sub call and argument list. Otherwise, Perl can't use prototypes, so you have to fall back to Perl's default parsing for the sub call and argument list.

In the case of $self->selector->remove, Perl can't figure out what remove sub is going to get called at compile time, because it has no idea what kind of object $self->selector will return. This goes for all method calls. Perl always has to defer to run time to decide which sub will get called. Thus Perl always has to defer to its default parsing, which is: if the method name is followed by an opening parenthesis, a list of arguments begins; if it's followed by anything else (including EOF), then the list of arguments is empty.

For function calls (i.e. sub calls which are not method calls), the default parsing behaviour (i.e. when Perl encounters a sub call where it can't figure out which sub is being called at compile-time) is: if the function name is followed by an opening parenthesis, a list of arguments begins; if it's followed by anything else, then the behaviour depends on whether strict is in effect. If strict is in effect, the parser throws an exception; otherwise the parser treats the function name as a bareword string.

These "default" parsing rules are for when Perl cannot determine which sub is getting called at compile time. Often Perl can determine this for function calls (but never for method calls) - for example if the sub was declared earlier in the same file, or if it was imported earlier in the same file. In cases where it can determine which function is getting called, then the rules aren't as simple. ;-) Perl has to take into account the prototype of the sub (if any), plus any custom parsing rules that have been established for the sub (i.e. the Perl keyword API).

use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

In reply to Re: to paren or not to paren by tobyink
in thread to paren or not to paren by quixadhal

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.