E
eponymousalias
I have a question about how to structure conditional code.
I've got a Perl script I want to give to customers A, B, and C.
Customers A and B will run the script unmodified. Customer C
needs some special tweaks, but I don't want to contaminate the
standard script with customer-specific code. So I'd like to
build a package interface, and execute that package conditionally,
depending on whether or not it's installed at the customer site.
sub allow
{
my ($package) = @_;
# I'm not sure how to efficiently check if the package exists.
# Preferably, this check would not duplicate the work that
# require will do internally, but maybe that's not possible.
if ($package exists) { require $package; return 1; }
return 0;
}
my $package_foo_is_available = allow Foo;
...
if ($package_foo_is_available)
{
Foo::execute arguments ... ;
}
Essentially, I want require without its "die" semantics if the
package cannot be found; I'd rather return a value to the calling
program so it can adapt gracefully to local conditions.
Is there some standard pattern/implementation for this?
I've got a Perl script I want to give to customers A, B, and C.
Customers A and B will run the script unmodified. Customer C
needs some special tweaks, but I don't want to contaminate the
standard script with customer-specific code. So I'd like to
build a package interface, and execute that package conditionally,
depending on whether or not it's installed at the customer site.
sub allow
{
my ($package) = @_;
# I'm not sure how to efficiently check if the package exists.
# Preferably, this check would not duplicate the work that
# require will do internally, but maybe that's not possible.
if ($package exists) { require $package; return 1; }
return 0;
}
my $package_foo_is_available = allow Foo;
...
if ($package_foo_is_available)
{
Foo::execute arguments ... ;
}
Essentially, I want require without its "die" semantics if the
package cannot be found; I'd rather return a value to the calling
program so it can adapt gracefully to local conditions.
Is there some standard pattern/implementation for this?