My practice is, if I'm writing a script called foo.pl, to write a module called foo.pm. Then all I have in foo.pl is the flow control, which is either a dispatch table or an if / else tree, made up of calls to subroutines in foo.pm. The subs in foo.pm are arranged "by order of appearance" in foo.pl.
I don't claim this is either original or perfect, but it works for me for the following reasons:
- makes my subs immediately available for re-use elsewhere - which is a good thing in itself, but also subliminally gets me thinking about code re-use
- enforces loose coupling - each sub has to be passed and to return all its inputs and outputs - so I am forced to think about minimising inputs and maximising usefulness of outputs
- I'm a bit unusual in that I do all my development on a remote server. Given that, I find it useful to write each new sub in a module which I call dev.pm, which has nothing in it except (a) the sub I'm working on now and (b) some useful de-bugging shortcuts (eg print out the contents of a data structure using Data::Dumper). Then when the sub is ok, I cut and paste it into foo.pm (or whatever). So the module with the subs in it grows and grows, but I rarely look at it, whilst the files I'm working with stay slim. Saves seconds that I can use for staring blankly into space
- foo.pl acts as an "index" of my subs
§ George Sherston