The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


later - A pragma to postpone using a module


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

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

    sub bar {
        # do whatever


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

    use Foo;

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';


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');


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


use later 'Module::Name';


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.


    use later 'Data::Dumper';

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


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


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.


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 <>.


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


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


Erwan Lemonnier <>


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


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.