Taking the idea behind
Corion's code, but working on how to do this without needing to modify the individual scripts, I came up with the idea of wrapping the code of each script into a subroutine in your General.pl.
I've banged together a script that works (for some definition), but I'm sure could run afoul if you aren't careful or if your needs are not quite the same as the assumptions I made.
#!/usr/bin/perl
use strict;
use warnings;
my @modules = qw(1 2 3 4);
my %sub;
for my $module (@modules) {
if ( -e "$module.pl" and -r "$module.pl" ) {
local $/;
open my $fh, '<', "$module.pl" or die "cannot open $module.pl: $
+!\n";
my $perl = <$fh>;
close $fh;
my $sub = sub {
local @ARGV = @_;
eval $perl;
warn "$module failed: $@\n" if ($@);
};
if ($@) {
die "$module.pl failed to load: $@\n";
}
$sub{$module} = $sub;
}
else {
die "$module.pl does not exist or is not readable\n";
}
}
for my $sub ( @modules ) {
warn "calling $sub\n";
$sub{$sub}->();
}
print "finished calling all modules\n";
I happen to have several scripts in my directory (1.pl, 2.pl, 3.pl, etc.) because I use them for writing test / proof of concept code. This works in that it wraps each file in a subroutine; it calls each subroutine; the work is done; output is returned as expected and errors are trapped and handled. This requires no modification to the existing scripts. If you need to pass arguments to the scripts, they can be passed to the subourtines and all is rosey.
Ivan Heffner
Sr. Software Engineer
WhitePages.com, Inc.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.