in reply to subroutine is confusing

While not directly related to your example there is a case when you need to declare a sub before you use it and it involves the use of parens. (Assuming the use of strict and warnings).
#!/usr/bin/perl use strict; use warnings; my_sub(); # works # my_sub; # fails "Bareword not allowed..." print qq{done}; sub my_sub { print qq{here\n} };
If you declare the sub first
#!/usr/bin/perl use strict; use warnings; sub my_sub { print qq{here\n} }; my_sub(); # works my_sub; # now works too print qq{done};
you can now use the sub without parens.

I reckon a good bet, in these circumstances, is to always use the parens.

Replies are listed 'Best First'.
Re^2: subroutine is confusing
by jwkrahn (Abbot) on Feb 08, 2008 at 12:46 UTC
    I reckon a good bet, in these circumstances, is to always use the parens.

    Good advise but not applicable when using sort as sort only requires the name of the sub to use as a callback whereas with parentheses tells perl to run the sub.

    $ perl -le' my @x = qw[ one two three four ]; sub compare { $a cmp $b } print for sort compare @x; ' four one three two $ perl -le' my @x = qw[ one two three four ]; sub compare { $a cmp $b } print for sort compare() @x; ' Array found where operator expected at -e line 9, at end of line (Missing operator before ?) syntax error at -e line 9, near ") @x" Execution of -e aborted due to compilation errors.
    Update: Had parentheses in wrong place ... oops.