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).
In reply to Re: to paren or not to paren
by tobyink
in thread to paren or not to paren
by quixadhal
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |