in reply to what are all the things to be considered to write a effective perl script or module?

There's a huge diff between writing a script and writing a module.
A script is possibly likely throw-away. A script can be something you put in your ~/bin - it might be a hack.
A script will likely use modules.

A module is a very freaking diff thing.
A module implies multiple usage.
Possibly distribution.
With distribution comes documentation. And tests. And if you release something for other people to use, by god it better be tested (lookup test suites .. ./t) and documented. You don't want to release something that doesn't do what you say it does.

What if your script uses "Carp" and one day your computer blows up. You don't want to find out it's because of Carp.

Oh please no.. not carp..

A script is code. A module is a work of art. Hopefully it has some value.

  • Comment on Re: what are all the things to be considered to write a effective perl script or module?

Replies are listed 'Best First'.
Re^2: what are all the things to be considered to write a effective perl script or module?
by salazar (Scribe) on Feb 07, 2009 at 03:41 UTC
    I'm surprised nobody else hit on this yet.

    Granted I'm a Perl newbie, this seems like a very important distinction to make -- that a script is a whole different beast from a module.

    Even if you weren't planning on redistributing (and thus documenting, speed testing - beyond your own desires, etc.), the testing is essential for the module. And even if the Carp module doesn't blow up your computer, if it's defective it could cause some headaches in scripts you're trying to run with it.


    Anyways, in response to your original question, here's my $0.02. Whenever I need to write a script, a function, whatever, I've found it's definitely easiest to whip out some sort of text editor, and plan out what you need to do.

    Generally the process involves first listing what I want to do, and then what design constraints are in place. I agree with all above posters who have said that your first goal is to make it work. Worry about benchmarking and beautification later. With that done, I'll plan out in English a few methods for going about this -- it's easier to see what makes sense and what doesn't this way, no matter how comfortable you are with code. With that in mind, I'll start adding code, and eventually piece them together.
    (Then comes the testing, documentation wherever it needs to go, and cleanup).
Re^2: what are all the things to be considered to write a effective perl script or module?
by ryanc (Monk) on Feb 07, 2009 at 23:29 UTC
    A module is a very freaking diff thing.

    Hear, hear!

    1. Make a script that "uses" your module. It doesn't matter that your modules isn't written yet, this is practice toward how you would like to use or expect your module to be used.

    2. Turn your pretend script into usable tests; make sure to include tests that will intentionally fail.

    3. After #1 and #2, then start coding on your module to actually deliver what your tests expect.

    As others have mentioned, go check out Perl Best Practices. For those less patient, the Top 10 can be found on perl.com here:

        http://www.perl.com/pub/a/2005/07/14/bestpractices.html

    cheers.
    ryanc