Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: where do you put your subs

by Juerd (Abbot)
on Mar 08, 2002 at 08:26 UTC ( [id://150256]=note: print w/replies, xml ) Need Help??


in reply to where do you put your subs

My subs are always at the top. In my opinion, a sub should be in the file before the point where it's first used. If that isn't possible, for example when two subs call eachother, I put empty declarations above them. If you have subs before everything else, you don't have to wrap it in a BEGIN block if you need simple closures.

My sripts look like this:
#!/usr/bin/perl -w use Module::Foo; use Module::Bar; use Module::Baz qw(foo bar); use strict; sub first { # ... } sub second { # ... } # main code, if applicable


My modules look like:
package Module::Xyzzy use Module::Foo; use Module::Bar; use Module::Baz qw(foo bar); use base 'Exporter'; use Carp use strict; our @EXPORT_OK = qw(...); sub CONSTANT () { 1 } sub new { # I try to always put new before other methods } sub first { # ... } sub second { # ... } sub DESTROY { # If there's a DESTROY method, I put it at the end } # main code - that is: "1;" :) __END__ =pod That's right, I use __END__ and =cut, just to be sure. My documentatio +n is NOT mixed with the code. Whenever code is unclear, I comment it, but I dislike, +no I hate pod and code mixed. =cut


It's just a matter of taste

++ vs lbh qrpbqrq guvf hfvat n ge va Crey :)
Nabgure bar vs lbh qvq fb jvgubhg ernqvat n znahny svefg.
-- vs lbh hfrq OFQ pnrfne ;)
    - Whreq

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://150256]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-24 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found