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

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: subroutine is confusing
by cLive ;-) (Prior) on Feb 08, 2008 at 10:02 UTC

    Well. What happened when you tried it?

    Are you really so lazy that you cannot run a two line script to see if it works or not?

    And then read perldoc perlsub for further information.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: subroutine is confusing
by shmem (Chancellor) on Feb 08, 2008 at 10:07 UTC
    Can't you tell yourself by just trying?

    The sub will be used no matter which line goes first. For evidence just reverse the comparison in the sub body.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: subroutine is confusing
by wfsp (Abbot) on Feb 08, 2008 at 10:39 UTC
    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.

      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.