RE: Require / Use



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






.



Relevant Pages

  • Re: Does loading additional forms drain resources?
    ... I also hadn't thought of loading a tab's subform until you click on the tab. ... One great tip is to not load sub forms until you need them. ...
    (microsoft.public.access.forms)
  • Re: [opensuse] How to set up LIRC?
    ... and if not then loading it and setting it to load at boot time. ... I Googled around and learned a bit about loading kernel modules but nothing answered this question. ... Well further Google attempts and a visit to the LIRC website seemed to indicate that I need another module loaded in the kernel called lirc_serial. ...
    (SuSE)
  • Re: Next Version of GDI+ (maybe GDI+ 2.0)
    ... loading and manipulating of such files isn't lightning fast unless you have really powerfull dedicated hardware. ... to see whole image you need to read whole image, and then eventualy generate a smaller sample for display. ... BUT to have this smaler sample you need to load ALL the pixels of oryginal image. ...
    (microsoft.public.dotnet.framework.drawing)
  • RE: 66 running processes?
    ... Win XP, a firewall and an AV and about seven background utilities, and with ... want it to load right from boot up, or could you take it off the load list ... Stopping programs you use loading early is only likely to make much ...
    (microsoft.public.windowsxp.perform_maintain)
  • Re: Next Version of GDI+ (maybe GDI+ 2.0)
    ... Alejandro Lapeyre ... > loading and manipulating of such files isn't lightning fast unless you ... > smaller sample for display. ... > load ALL the pixels of oryginal image. ...
    (microsoft.public.dotnet.framework.drawing)

Loading