in reply to What are the brackets in this for ?

As well as the potential for inlining that has already been pointed out, you also get a compile time error if you inadvertently introduce something that could be interpreted as an argument to the constant subroutine, see for instance this stupid example:

sub FOO { 1; } + sub BAR () { 2; } + my $foo = FOO 1; + my $bar = BAR 1;
You get the error from the BAR 1 whereas the FOO 1 is undetected, obviously in real code this could possibly be much less obvious and lead to subtle bugs.

/J\

Replies are listed 'Best First'.
Re^2: What are the brackets in this for ?
by bunnyman (Hermit) on May 22, 2006 at 14:14 UTC

    Not only that, but you can leave off the parens for the prototyped sub. Otherwise you would have to write FOO() instead of FOO.

    This is usually done in situations where you would use #define in a C program to introduce a constant. In C, you would write

    #define FOO 1

    and then you could use FOO in your program and it would be the same as if you had written a literal 1. Creating a Perl sub with a prototype lets you write FOO, which looks like a constant, instead of FOO(), which looks like a sub call.

      It's not quite as cut and dried as that. The prototyping makes no difference if the subroutine is not introduced before it is first referenced, you will still get the "Bareword" error when using strict (and if you are not using strict the sub name will be taken as a string.) If you define the subroutine first or make a forward declaration (e.g sub FOO;) then you can use it without the parentheses regardless of the prototyping.

      /J\

      Not only that, but you can leave off the parens for the prototyped sub. Otherwise you would have to write FOO() instead of FOO.

      Really?

      #!/usr/bin/perl -l use strict; use warnings; sub FOO { @_? 1+shift : print 'Foo'; } FOO for 1..FOO FOO; __END__