Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

This first part is partially in response to demerphq's node elsewhere in this thread where he mentions my use of exit main;. But I didn't want the rest of this to get lost too deep in this large thread.

I mentioned this habit here, and I actually make an exception for main() and call it before I declare it. I define main() at the very bottom of the script for the same reasons many people put their "main" code near the top... You see, you can't put your "main" code at the very top, you have to put your #! line, your modules uses, your file-scoped variables (that act like "globals"), your package variables (real "globals"), etc. before everything. In a real-life script, that introductory cruft can get rather long and usually isn't the most interesting part of the program.

So by putting main() at the very bottom of the script, it is easier to find than if I put it as close to the top as I can get away with.

But I call main() via exit( main(@ARGV) ); as close to the top as possible for reasons currently partially covered on tye's scratchpad.

I also try to declare subroutines before I call them. Although the benefits of this are somewhat minor with current versions of Perl when using good practices, there are still some benefits (some mistakes that get caught for you). You can even intentionally avoid using parens with your function calls and get told at compile-time when you've mispelled a function name.

But I suspect that the benefits will increase greatly with future versions of Perl. Having the ability to catch simple mistakes in how you called a function, especially at compile time, can be a great benefit. Not having to always declare how the function to be called is also a benefit that Perl will probably never lose. But I expect future versions of Perl to have much more support for this type of declaration and checking of function invocations (at compile time).

And when you have that in a language, defining the function before you call it is nice. Now, sometimes you'll want to avoid having to perform a topological sort on your order of subroutine definitions so you'll put separate subroutine declarations at the top and define your subroutines willy nilly (or in some other sensible order) below. But sometimes that is a pain in itself and the topological sorting is easier.

So I start out with my subroutines sorted topologically so that they are all defined before they are called except for main() which I call before any subroutines are defined (where "before" means earlier in the source code, not earlier in time, of course).

I currently find practical benefits in this practice. But I suspect I'll be even happier with it when Perl 6 comes out, especially when I start converting old scripts to make use of Perl 6 features. (:

        - tye (but my friends call me "Tye")

In reply to (tye)Re: where do you put your subs by tye
in thread where do you put your subs by greenFox

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-03-28 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found