in reply to alternative way to subs
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.#!/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";
|
---|