Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

subroutine is confusing

by Abhisek (Novice)
on Feb 08, 2008 at 09:53 UTC ( #666945=perlquestion: print w/replies, xml ) Need Help??
Abhisek has asked for the wisdom of the Perl Monks concerning the following question:

Hi Folks If you can tell me ,By any chance is this subroutine will be in use if i declare it after my first line. (The key word lexical)
@keys = sort lexical keys(%identifierMapold); sub lexical { $a cmp $b };

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.

      i am not yet given the permission to touch the script
        So? Copy it into a new file, initialize the variables with test values, and run that. No big deal.

        BTW you don't need <readmore>-tags around single lines. No readmore needed for post with less than, let's say 30 lines.

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.


    _($_=" "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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://666945]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2017-11-24 11:25 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (348 votes). Check out past polls.