Modulinos++

BooK on 2004-12-10T09:38:40

I am subscribed to both TPR and TPJ and I do not regret it*. After all, I can read brian d foy's articles in both publications... ;-)

And in November's TPJ, I could read his article about Modulinos. This is such a great idea!

Now that I am compulsive about tests, I'm very disappointed with how I can test scripts. These tests are a pain to write: it's difficult to cover all the tests, I have to create loads of data and configuration files to check for each case, I do not get test coverage, etc. Test::Cmd is a great tool (except that I can't have two instances of a Test::Cmd at the same time) but, as bdfoy notes, when testing scripts, you only test the whole process. Does that input produce the expected output? How many files appeared and weren't removed? And so on.

Most of the time, a script is a set of individual actions combined according to the command-line options. Sure, but I don't want to write a whole module and install it in @INC, just to encapsulate those individual actions.

Well, now I can. I just write the module inside the script file, add the magic one-liner** given by brian, and write tests scripts!

The only drawback in my opinion is that, if you want to use the "module", you'll have to name your script foo.pm. If it's only for scripts, I guess you can simply require "foo" in the test suite. And Devel::Cover works as expected!

I think I'll use^Wrequire^Wuse this technique for my future production scripts.

(*) Except for the fact that TPJ is online only... Did you also notice that, contrary to almost every magazine, a particular month's issue is available at the end of the month? I suppose they were late at some point, and never catched up.

(**) You noticed I did not write the one-liner here. It's on purpose. Either 1) brian will write it somewhere for all to see, or 2) you'll subscribe to TPJ. Or both.


Re: Modulinos++

brian_d_foy on 2004-12-10T20:42:31

I first talked about the idea of modulinos on Perlmonks: How a script becomes a module. It's a much longer version of what appears in TPJ, although the TPJ version is much more polished and developed.

As for the drawback about naming the script, I thought I had talked about that in the article (although I haven't read what they published. I can't read my own stuff once someone else publishes it). The trick is to create the module and install it in the usual place, then create a symlink (or link) to it for the script. That way it works both ways. diagnostics.pm does this, for instance.