require vs. allow package reference

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?
 
P

Paul Lalli

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?

Yes. You're going about it backwards, though. Rather than try to
duplicate require() without the die(), just use require, and if it
die()s, trap that die(). That's precisely what the eval() function is
for:

sub allow {
my $package = shift;
eval {
require $package;
}
if ($@) {
# 'require' died, $package not available
return;
} else {
# 'require succeeded, $package loaded.
return 1;
}
}

For more information, see:
perldoc -f eval

Hope this helps,
Paul Lalli
 
G

Glenn

Essentially, I want require without its "die" semantics if the
Rather than try to duplicate require() without the die(), just use
require, and if it die()s, trap that die(). That's precisely what the
eval() function is for:

Hope this helps,
Paul Lalli

Indeed it does. I knew about die's change of behavior within eval,
but had forgotten ... thanks much.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top