2

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.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
700 Software
  • 85,281
  • 83
  • 234
  • 341

3 Answers3

1

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
  • 9,171
  • 33
  • 51
1

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:

Perl script usable as a program and as a module

Community
  • 1
  • 1
DVK
  • 126,886
  • 32
  • 213
  • 327
1

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
  • 78,834
  • 30
  • 123
  • 180