Re: code organization style




On Apr 22, 2005, at 1:42 PM, Gabor Szabo wrote:

How do you people do it ?
Where can I see examples ?

It's all about what you're after and what's right for your program.

I like to create classes to divide up the work. The classes can be widgets, or data classes, or control classes, whatever makes sense.

You don't *have* to put your classes in separate files. It's quite legal to declare multiple packages in a single perl source file. This is mostly a deployment issue; if your classes are in separate files, your script has to be able to find them at runtime, which means they need to be installed somewhere --- in the perl library, in a private data directory, somewhere --- and that can make hard to deploy your program if you want it to be self-contained. It also raises namespace issues, and reuse issues, and versioning issues.

My rule of thumb is: if this code is or could be useful outside the context of my program, i'll make a separate module of it, otherwise, keep it inside the main file until that file gets too unwieldy, at which point something has to go.


As an example, i put several extra packages into the single-file object_browser so that the program could be distributed as a single script. http://asofyet.org/muppet/software/gtk2-perl/object_browser- methodscraper Note that i had to add a few BEGIN blocks that you wouldn't see if the packages were in separate files, in order to ensure that the code in them runs at the right time.


--
"There's a documentary that i wanted to watch on PBS. I heard about it in NPR. ... Oh my god, did i just say that?"
  -- elysse




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]