quixadhal has asked for the wisdom of the Perl Monks concerning the following question:

That is the question!

I've recently dug back into writing some object oriented perl code, and one annoyance I've discovered is a bit surprising. I was wondering if someone can explain this to me.

In most cases, I find I can happily write code which calls functions without needing to use those pesky parenthesis, other than to clarify which arguments are being passed, and which are part of another expression.

However, in some cases, and it seems more often when using methods via $self, the perl parser frowns upon what (to me) seems an obvious case of wanting to call a method and pass an argument.

$self->selector->remove $self->listener;
This yields the error:
Scalar found where operator expected at Mud/Comm.pm line 149, near "-> +remove $self" (Missing operator before $self?) syntax error at Mud/Comm.pm line 149, near "->remove $self"
But if I add parens to that call, like this
$self->selector->remove($self->listener);

Perl is content and all is well.

So my question boils down to... under what conditions can you omit the function calling parenthesis, and when MUST you use them?

Replies are listed 'Best First'.
Re: to paren or not to paren
by eyepopslikeamosquito (Archbishop) on Apr 26, 2014 at 08:43 UTC

    I always use parens when calling user-defined subroutines and methods (except when playing golf) because:

    • The consistency of always using parens when calling user-defined subroutines/methods makes the code easier to read (for me).
    • Code tends to be more robust if it is reorganized in the future. 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 risk by always using parens in the first place.
    This is essentially the same advice given in Perl Best Practices. The first item in Chapter 9 is: "Call subroutines with parentheses but without a leading &". Luckily, Chapter 9 happens to be the free sample chapter from this book so you can read why Damian offers this advice right now.

      Very well put, ++eyepopslikeamosquito.

      To me, the consistent use of parentheses when calling user-defined subroutines and all methods makes the code more readable. It looks nicer.

Re: to paren or not to paren
by kcott (Archbishop) on Apr 26, 2014 at 06:01 UTC

    G'day quixadhal,

    When in doubt, you can check how Perl will parse your code fragment from the command line, rather than having to run a (test) script, with this:

    perl -MO=Deparse,-p -e '... code fragment here ...'

    See B::Deparse for more information about this.

    "In most cases, I find I can happily write code which calls functions without needing to use those pesky parenthesis, other than to clarify which arguments are being passed, and which are part of another expression." [my emphasis]

    Clarification is exactly what's needed here. Perl doesn't know if you meant

    $self->selector->remove($self)->listener;

    or

    $self->selector->remove($self->listener);

    Notice in the following what's valid and invalid syntax:

    $ perl -MO=Deparse,-p -e '$self->selector->remove $self->listener;' Scalar found where operator expected at -e line 1, near "->remove $sel +f" (Missing operator before $self?) syntax error at -e line 1, near "->remove $self" -e had compilation errors.
    $ perl -MO=Deparse,-p -e '$self->selector->remove($self)->listener;' $self->selector->remove($self)->listener; -e syntax OK
    $ perl -MO=Deparse,-p -e '$self->selector->remove($self->listener);' $self->selector->remove($self->listener); -e syntax OK

    For more information about calling methods, see perlobj. In particular, these three sections of that documentation:

    -- Ken

Re: to paren or not to paren
by tobyink (Canon) on Apr 26, 2014 at 07:44 UTC

    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
Re: to paren or not to paren
by wjw (Priest) on Apr 26, 2014 at 05:53 UTC
    Something about that Here I believe. And probably here on PM too... . Hope that is helpful.

    ...the majority is always wrong, and always the last to know about it...
    Insanity: Doing the same thing over and over again and expecting different results...
Re: to paren or not to paren
by Anonymous Monk on Apr 28, 2014 at 03:57 UTC
    Above all, be consistent and be clear.