Python and Java both have the concept of running main methods from a module. For example, in Python you can do this:

 def GetFoo():
     return 'bar'

 if __name__ == '__main__':
     # unit test GetFoo
     print GetFoo()

Maybe my Perl is just rusty, but I had to reinvent this concept in Perl. Here is what I came up with:

# runmod.pl
use strict;

my $module;
($module = $ARGV[0]) =~ s/\.pm//;
die "Need to pass in module name" if not $module;

eval("use $module;");
print $@;

eval("${module}::__main__();");
print $@;

This routine lets you stick a main method in a Perl module, and you can call it as follows:

perl runmod.pl FooModule.pm

-- SteveHowell

MainMethodsInModulesForPerl (last edited 2008-03-04 08:33:03 by localhost)