Erwan Lemonnier > later-0.04 > later

Download:
later-0.04.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
View/Report Bugs
Module Version: 0.04   Source   Latest Release: later-0.05

NAME ^

later - A pragma to postpone using a module

SYNOPSIS ^

Assuming we have a module Foo exporting the function bar():

    package Foo;
    
    use base qw(Exporter);
    our @EXPORT = qw(bar);

    sub bar {
        # do whatever
    }

    1;

And somewhere else we use Foo and call bar():

    use Foo;
     
    bar('some','arguments');

Now, for a number of possibly rather unsane reasons you might want to delay running use Foo until a function in module Foo (anyone of them) is called at runtime. To do that, change the former code into:

    use later 'Foo';

    bar('some','arguments');

This works even for object packages:

    use later 'My::Classy::Class';

    my $object = new My::Classy::Class;

And supports passing import arguments to the module that is used later:

    use later 'My::Classy::Class', do_fuss => 1;

    my $object = new My::Classy::Class;

To see some debug messages, do:

    BEGIN { $later::DEBUG = 1; };

    use later 'Data::Dumper';
    print Dumper('bob');

DESCRIPTION ^

The later pragma enables you to postpone using a module until any of its methods is needed during runtime.

API ^

use later 'Module::Name';

or

use later 'Module::Name', arg1 => $value1, ...;

Delays running use Module::Name until an undefined subroutine is found in the current package at runtime. Only when it happens shall later evaluate use Module::Name inside the current package, hence hopefully importing the undefined subroutine into the current package's namespace. The formerly undefined subroutine is then called as if nothing unusual had happened.

Any further encounter with an undefined subroutine will still result in the standard 'Undefined subroutine' error.

If multiple modules are called with use later inside the same package, they will all be used upon the first encounter with an undefined subroutine in this package, despite the fact that only one of them should export the specific undefined subroutine.

If use later 'Module::Name' is followed by import arguments, those will be passed to Module::Name upon using it. Note that the later pragma does not support passing import arguments that are code refs.

You may use later modules that use later other modules (and so on recursively). It works.

Examples:

    use later 'Data::Dumper';

    # runs 'use Data::Dumper' upon executing Dumper()
    print Dumper([1,2,3]);

or

    use later 'MyLog', level => 1;
 
    # runs 'use MyLog level => 1' upon executing mylog()
    mylog("some message");

DIFFERENCE WITH OTHER SIMILAR MODULES ^

later versus autouse

With autouse you must specify which functions from the autoused module should trigger using the module. With later, you don't have to since any undefined function will have that effect.

autouse considers all arguments following use autouse MODULE to be names of functions from MODULE. later on the other hand handles them as real import arguments, to be passed to the module's import function.

later versus Class::Autouse

A module Foo::Bar declared with Class::Autouse is used when one of its methods is called with the class style syntax Foo::Bar::bob(). Calling bob() only does not trigger using Foo::Bar. later supports both syntax.

later versus load, AutoLoader, SelfLoader...

load, AutoLoader, SelfLoader and the like require modules to be divided in two parts separated by a __DATA__ or __END__, one compiled at once and one compiled later on during runtime. Those modules are built for a different purpose than later.

BUGS AND LIMITATIONS ^

This module is a proof of concept.

The later pragma will not work properly if the calling module or the postponed module have an AUTOLOAD function, since it will conflict with the AUTOLOAD that later silently injects into them.

The later pragma does not support passing code references as import arguments to the postponed module.

Since postponed modules are searched for and compiled only during runtime, any error in the module (compilation or other) is delayed until then. You are therefore at risk of crashing your program in the middle of its runtime due to errors that would normally be detected during compilation.

As far as I am concerned, I fail to see any sane situation where this pragma would be needed that cannot be implemented in a safer way without later. You have been warned :)

Should you find bugs or suggest changes, please send an email to <erwan@cpan.org>.

SEE ALSO ^

See 'load', 'SelfLoader', 'AutoLoader', 'autouse', 'Class::Autouse'.

VERSION ^

$Id: later.pm,v 1.8 2007/01/24 14:41:58 erwan Exp $

AUTHOR ^

Erwan Lemonnier <erwan@cpan.org>

COPYRIGHT AND LICENSE ^

This code is distributed under the same terms as Perl itself.

DISCLAIMER OF WARRANTY ^

This is free code and comes with no warranty. The author declines any personal responsibility regarding the use of this code or the consequences of its use.

syntax highlighting: