Having a perl script uses one of several secondary scripts

I have the main program mytool.pl to run from the command line. There are several helper scripts special1.pl, special2.pl, etc., Each of which contains a pair of routines and a hash, all equally named for the scripts. Suppose they are called MySpecialFunction (), AnotherSpecialFunction () and% SpecialData.

I want mytool to include / use / import the contents of one of the special * .pl files, only one, according to the command line parameter. For example, the user will execute:

bash> perl mytool.pl  --specialcase=5

and mytools will use MySpecialFunction () from special5.pl and ignore all other special * .pl files.

Is it possible and how to do it?

It’s important to note that the choice of which special file to use is made at runtime, so adding the “use” at the top of mytool.pl is probably wrong.

Note. I am a long C programmer, not a perl expert; Maybe I'm asking for something obvious. This is for a one-time project that will turn into dust in just a month. Neither mytool.pl nor the special? .Pl (or perl) will be interesting after the completion of this short project. Therefore, we do not care about solutions that are developed or require the study of some deep magic. Quick and dirty. I guess the Perl module mechanism is too crowded for this, but I have no idea what the alternatives are.

+3
source share
2 answers

use , Perl require BEGIN ( ). , script , require .

if ($special_case_1) {
  require 'special1.pl';
  # and go about your business
}

, use vs. require.

+1

specialcase to .pl require do .

#!/usr/bin/env perl

use strict; use warnings;

my @handlers = qw(one.pl two.pl);

my ($case) = @ARGV;
$case = 0 unless defined $case;

# check that $case is within range

do $handlers[$case];

print special_function(), "\n";
+2

All Articles