NAME
deferred - Defer loading of modules until methods are called
SYNOPSIS
use deferred "SomeModule";
use SomeModule;
# Module not yet loaded, but you don't have to worry about conditionally
# loading it.
my $foo = SomeModule->new; # Module now loaded
DESCRIPTION
For modules where you don't care about the ->import
method being called, such as pure OO modules you may not want to load the module until it is actually used. (The main reason being to save time or memory.)
You should be very careful if considering using this on code not under your control, please understand this may break assumptions the code makes (that are quite valid in normal circumstances).
USAGE
The interface to this module is via arguments to use deferred
.
Either a string or a regexp reference (qr//
) may be provided. A string will be interpreted as a regular expression matching the full module name (i.e. ^
and $
are prepended and appended respectively).
For example:
use deferred "Foo::.*"; # Defer loading all modules under the Foo namespace
use deferred "Bar", qr/Baz/; # Defer loading
use
statements executed after a use deferred
statement will be checked to see if loading of that module should be deferred, if so the module will not be loaded until a method is called on that module name.
Multiple use deferred
statements will add to the list of matches.
This is not currently a pragma, so it is not lexically scoped.
DISABLING
no deferred
will disable deferred loading from that point onwards. It will load all deferred modules at the moment it is called, unless -discard
is provided as an argument.
no deferred; # Disable all deferred loading, load modules
no deferred -discard; # Disable all deferred loading, discard list
An argument may be provided to stop future deferred loads of a previously specified item:
no deferred "Foo::.*";
If an argument is provided no modules will be loaded.
LIMITATIONS
This module certainly is a case of providing enough ammo to shoot yourself in the foot several times over. Deferring the loading of modules such as base and parent will almost certainly result in confusion. (The module itself tries to handle some of these issues, but it can't cope with some cases).
Combining this module with other modules that make use of code references in @INC
may or may not work, potentially dependant on order of loading. This module places its code reference onto the start of @INC
when loaded. (Particular modules -- that don't live in the Acme namespace -- to be aware of are everywhere and App::FatPacker.)
In order for this to work your use
statements must exactly match the classes you call methods on (i.e. with some modules it is common practice to use a high level module such as Foo
, then create an instance of Foo::Bar
-- this won't work).
This module also rudely hijacks UNIVERSAL::AUTOLOAD
.
SEE ALSO
Class::Autouse - I tried this, I actually wanted something between its superloader and manually specifying classes. I think it handles more edge cases though.
LICENSE
This program is free software. It comes without any warranty, to the extent permitted by applicable law. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See http://sam.zoy.org/wtfpl/ or Software::License::WTFPL_2 for more details.
AUTHORS
David Leadbeater <dgl@dgl.cx>, 2010