AutoReloader - Lazy loading and reloading of anonymous subroutines


    use AutoReloader;
    my $sub = AutoReloader -> new ($file, $checksub, $autoprefix);
    $result = $sub -> (@args);

    $sub -> check (0);           # turn source file checking off for $sub
    $sub -> checksub ($coderef); # provide alternative checking routine

    use AutoReloader qw (AUTOLOAD);
    AutoReloader -> check (1);      # turn source file checking on
    $result = somefunc (@args);
    *somefunc{CODE}->check(0);   # turn off checking for this named sub


AutoReloader provides lazy loading like AutoLoader, but for function files which return an anonymous subroutine upon require.

Before requiring that file, it is checked via some subroutine returning a value (default is mtime). The returned value is remembered. At each call to that sub the check subroutine is run again, and if the returned value changed, the source file is reloaded.

Importing the AUTOLOAD method provides for lazy loading of anonsubs as named subs. The wrapped anonsub will be assigned to a symbol table entry named after the filename root of the function source file.


new ($file, $checksubref, $autoprefix)

subroutine constructor. $file can be the path to some function file or a function name which will be expanded to $autoprefix/__PACKAGE__/$ and searched for in @INC. $checksubref and $autoprefix are optional. If they are not provided, the default class settings are used.

auto ($autoprefix)

set or get the default autoprefix. Default is 'auto', just as with AutoLoader: for e.g. POSIX::rand the source file would be auto/POSIX/ . AutoReloader lets you replace the 'auto' part of the path with something else. Class method (for now).

suffix ($suffix)

set or get the suffix of your autoloaded files (e.g. '.al', '.pl', '.tmpl') as a package variable.

check (1)

set or get the check flag. Turn checking on by setting this to some true value. Default is off. Class and object method, i.e. AutoReloader->check(1) sets the default to on, $sub->check(1) sets checking for a subroutine. For now, there's no way to inculcate the class default on subs with a private check flag.

checksub ($coderef)

set the checking subroutine. Class and object method. This subroutine will be invoked with a subroutines source filename (full path) every time the sub for which it is configured - but only if check for that subroutine is true -, and should return some value special to that file. Default is 'sub { (stat $_[0]) [9] }', i.e. mtime.


 AutoLoader, AutoSplit, DBIx::VersionedSubs


AutoReloader subroutines are always reported as __ANON__ (e.g. with Carp::cluck), even if they are assigned to a symbol table entry. Which might not be a bug.

There might be others.


 shmem <>


Many thanks to thospel, Corion, diotalevi, tye and chromatic (these are their nicks) for review and most valuable hints.


Copyright 2007 - 2021 by shmem <>

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.