Mostly, it's a problem of style. If you know what you are doing, it doesn't really matter. There are some pitfalls that you should be aware of, though.
Consider the following case, with subs on bottom, and calls to the subs on top. You see that there is a variable $stuff, declared before the sub. So you think it shouldn't be a problem to call your sub, which should print "ABCD". Unfortunately, it doesn't work like that. Your variable doesn't get initialized, because the program flow hasn't reached that point yet.
#!/usr/bin/perl -w use strict; dosomething(); my $stuff = "ABCD"; # bottom sub sub dosomething { print "$stuff\n"; }
OTOH, if you put your call to the sub at the end of the file, transforming your sub into a "top" sub, then you face the problem of unintended globals that Juerd is referring to. In the following case, your "dosomething" will work as expected, unless you call "dosomethingelse" first, in which case your lexical variable gets changed.
Using globals (including unintended ones) in this way is not recommended. If you really must have them, consider either creating a package or using closures.
#!/usr/bin/perl -w use strict; my $stuff = "ABCD"; # visible to all subs from now on #top subs { # start closure my $privatestuff = "WOW!"; #visible only from dosomething sub dosomething { print "$stuff \t $privatestuff\n"; } } # end closure sub dosomethingelse { $stuff = "WXYZ"; } dosomething();
Most of the problems that you might have with subs are related to using globals (or lexically scoped that act like globals) or not.
If your subs use parameters instead of globals, there are no such side effects when you call them, no matter where they are declared. The only difference will be that you have to use parentheses when you call subs that have not been defined yet.
#!/usr/bin/perl -w use strict; dosomething("ABCD"); #top subs sub dosomething { my $arg = shift; print "$arg\n"; } dosomething "EFGH";
My personal recommendation is: if you have a lot of subs, consider making a module out of them.
HTH
 _  _ _  _  
(_|| | |(_|><
 _|   

In reply to Re: Subs by gmax
in thread Placement of Subs by venimfrogtongue

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.