M
Mark Mackey
Hi all.
I've got a simple problem that I can't work out an elegant way to solve.
I have a simple Perl module which is basically a wrapper for some
external binaries. I want the module to check for the existence of those
binaries on import, but only for the binaries which are actually going
to be used by the subroutines that are exported.
For example, I have Thingy.pm:
package Thingy;
require Exporter;
@ISA = qw(Exporter);
# symbols to export on request
@EXPORT_OK = qw(sub1 sub2 sub3);
use strict;
sub sub1 {
print `/bin/ls`;
}
sub sub2 {
print `/bin/true`;
}
sub sub3 {
print `/bin/no_exist`;
}
BEGIN {
my(@required)=qw( /bin/ls /bin/true /bin/no_exist );
my(%required)=( sub1=>"/bin/ls", sub2=>"/bin/true", sub3=>"/bin/no_exist" );
foreach (@required) {
print "Checking $_\n";
die "Can't find required utility $_" if (! -x $_);
}
}
1;
Sub1 will only work if "/bin/ls" is present, sub2 will only work if
"/bin/true" is present, and sub3 will only work if "/bin/no_exist" is
present. With the current structure, all 3 are checked on import, so
use Thingy qw();
dies telling me that "bin/no_exist" doesn't exist .
What I want is to be able to check the relevant binaries' existence only
for the methods that are imported, so in this case you could do
use Thingy qw(sub1);
and everything would be happy, but
use Thingy qw(sub1 sub2 sub3);
would bail out and inform me that 'sub3' depends on "/bin/no_exist" and
hence it can't continue.
The reason I'd like this check is that this module is used in lots of
programs, each of which uses a different subset of its subroutines. Some
of the programs have very long runtimes, so I don't really want them to
run for hours and then suddenly bail out when sub3() finally gets
called. I suspect the answer involves fiddling around with import()
methods, but I don't completely understand the documentation for
Exporter.pm about this.
Help?
I've got a simple problem that I can't work out an elegant way to solve.
I have a simple Perl module which is basically a wrapper for some
external binaries. I want the module to check for the existence of those
binaries on import, but only for the binaries which are actually going
to be used by the subroutines that are exported.
For example, I have Thingy.pm:
package Thingy;
require Exporter;
@ISA = qw(Exporter);
# symbols to export on request
@EXPORT_OK = qw(sub1 sub2 sub3);
use strict;
sub sub1 {
print `/bin/ls`;
}
sub sub2 {
print `/bin/true`;
}
sub sub3 {
print `/bin/no_exist`;
}
BEGIN {
my(@required)=qw( /bin/ls /bin/true /bin/no_exist );
my(%required)=( sub1=>"/bin/ls", sub2=>"/bin/true", sub3=>"/bin/no_exist" );
foreach (@required) {
print "Checking $_\n";
die "Can't find required utility $_" if (! -x $_);
}
}
1;
Sub1 will only work if "/bin/ls" is present, sub2 will only work if
"/bin/true" is present, and sub3 will only work if "/bin/no_exist" is
present. With the current structure, all 3 are checked on import, so
use Thingy qw();
dies telling me that "bin/no_exist" doesn't exist .
What I want is to be able to check the relevant binaries' existence only
for the methods that are imported, so in this case you could do
use Thingy qw(sub1);
and everything would be happy, but
use Thingy qw(sub1 sub2 sub3);
would bail out and inform me that 'sub3' depends on "/bin/no_exist" and
hence it can't continue.
The reason I'd like this check is that this module is used in lots of
programs, each of which uses a different subset of its subroutines. Some
of the programs have very long runtimes, so I don't really want them to
run for hours and then suddenly bail out when sub3() finally gets
called. I suspect the answer involves fiddling around with import()
methods, but I don't completely understand the documentation for
Exporter.pm about this.
Help?