tags:

views:

61

answers:

3

I have a Perl script. If it was called direct from the command line, I want to run sub main.

If it was called using require, I want to do nothing and wait for the caller script to call sub job at it's leisure.

+1  A: 

It sounds as if you should be splitting your script into a module and a front-end program. Then programs which want to use the functionality can load the module using use (or require, if you insist) and the stub program can do that and the call the job subroutine as well.

Tim
Yea, I just came here to close the question. It turns out there is not a use for the script anymore.
cjavapro
+1  A: 

There's a recommended Perl solution for code which needs to be runnable from a script as well as a perl module.

It's usually called "modulino", which can be used as Google or SO search term to find how-tos.

Here's one how-to with reference links:

http://stackoverflow.com/questions/3395733/perl-script-usable-as-a-program-and-as-a-module/3395759#3395759

DVK
Thanks! Works perfect. It is so easy, I will be using it for testing.
cjavapro
You're welcome :)
DVK
+1  A: 

You could so something like this:

#!/usr/bin/env perl
package Some::Module;

sub job { ... }

sub main { ... } 

if (!caller) {
     main(@ARGV);
}

1;
__END__

=head1 NAME

Some::Module - program, podpage, and module

....

That can now simultaneously be all of:

  1. an executable script to be called from the command line as a program (which will call its main() function)

  2. a module to use from elsewhere in which case it just loads function definitions

  3. a podpage you can run pod2text on

Is that what you were looking for?

tchrist