RE: Require / Use
- From: cclarkson@xxxxxxxxxx (Charles K. Clarkson)
- Date: Thu, 21 Apr 2005 10:14:43 -0500
Chris Devers <mailto:cdevers@xxxxxxxxx> wrote:
: : Ahem. It is most useful when I want to determine the module to load
: : dynamically, or when I want to load a module only if necessary to
: : save the time spent compiling it in the cases when it is not needed.
:
: :-)
:
: I think that counts as an esoteric-enough requirement that beginners
: can safely ignore the need for it unless & until they hit that wall.
:
: I'm assuming that most beginners won't be writing much or any code
: that needs to optimize loading a module multiple times...
Dynamic loading does not apply just to multiple loading of a single
module. It could also be used to check the existence of a module before
loading it.
An answer to the original question is incomplete without at least
a mention of the usefulness of 'require' under some intermediate to
advanced perl programming.
my $mail_module = load_mail_module();
warn q(No mail module) unless $mail_module;
sub load_mail_module {
eval { require mail_foo };
unless ( $@ ) {
mail_foo::import();
return 'mail_foo';
}
eval { require mail_bar };
unless ( $@ ) {
mail_bar::import();
return 'mail_bar';
}
eval { require mail_baz };
unless ( $@ ) {
mail_baz::import();
return 'mail_baz';
}
return;
}
... or to load modules only as needed.
print redirect( '/' );
sub user_data_form {
require Data::FormValidator;
Data::FormValidator::import();
# ...
}
sub redirect {
my $page = shift;
require CGI;
return CGI::redirect( $page );
}
__END__
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328
.
- References:
- Re: Require / Use
- From: Chris Devers
- Re: Require / Use
- Prev by Date: Re: Feed Net::FTP an in-memory file
- Next by Date: Re: Require / Use
- Previous by thread: Re: Require / Use
- Next by thread: Starting Template for calling C from Perl
- Index(es):
Relevant Pages
|
Loading