perlquestion
graff
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 <strike>keep track of addresses</strike> <em>still group records</em> 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).
<P>
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..."
<P>
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?
<P>
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 "Runnable.pm":
<c>
#!/usr/bin/perl
package Runnable;
use Exporter qw/import/;
@EXPORT_OK = qw/do_something/;
sub do_something
{
return "did something with @_";
}
if ( $0 =~ /Runnable.pm/ ) {
print do_something( @ARGV ),"\n\n";
}
1;
</c>
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 Runnable.pm as a module:
<c>
#!/usr/bin/perl
use strict;
use Runnable qw/do_something/;
print "I'm hoping that this test program ".do_something( @ARGV )."\n";
</c>
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 Runnable.pm 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.
<P>
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.
<P>
Does anyone see a problem with this, or a better way?