graff has asked for the wisdom of the Perl Monks concerning the following question:

Every now and then, I'd like to implement something as a module, but also have it available as an executable script -- for example, I might want to "anonymize" zip codes in some way, keeping each zip code distinct (so I can keep track of addresses still group records that originally had the same zip code) without actually divulging the real zip codes. Such a function might be handy as a command-line tool (where @ARGV would be zip codes I just want to convert on a whim), and as a module (which could be used in a DBI or CGI script).

This flexibility is a common feature in Python -- you just add a few lines of code at the end of a "module" script to say, in effect, "if this file is being executed from the command line, invoke the class and/or object functions as follows, using the command-line args like so..."

So, if Python can do it, surely Perl can too, right? And so my question is: what would be a good way to do this?

My module-building skills don't get a lot of exercise, and I wouldn't presume to think I have a clue, but I came up with an example that seems to work, in a very simple-minded, unsophisticated way. Here's a little file I call "":

#!/usr/bin/perl package Runnable; use Exporter qw/import/; @EXPORT_OK = qw/do_something/; sub do_something { return "did something with @_"; } if ( $0 =~ / ) { print do_something( @ARGV ),"\n\n"; } 1;
I can "chmod +x" that file, run it with command-line args, and it prints "did something with ...(those args)". Now, here's a script that uses as a module:
#!/usr/bin/perl use strict; use Runnable qw/do_something/; print "I'm hoping that this test program ".do_something( @ARGV )."\n";
Sure enough, that runs too, and it prints exactly what I was hoping to see. The only "extra details" I need to handle here are to make sure that is in my shell's execution PATH, and to also make sure that its location shows up in @INC whenever I use it in another script.

Having the ".pm" extension on the command line when I run the module as a program seems a bit klunky and odd, but I guess there's no way around that if I want to use the same file as a module in other scripts in the "normal" way.

Does anyone see a problem with this, or a better way?