Author image Georg Moritz

NAME

AutoReloader - Lazy loading and reloading of anonymous subroutines

SYNOPSIS

    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

DESCRIPTION

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.

METHODS

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__/$function.al 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/rand.al . 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.

SEE ALSO

 AutoLoader, AutoSplit, DBIx::VersionedSubs

BUGS

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.

Author

 shmem <shmem@cpan.org>

CREDITS

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

COPYRIGHT

Copyright 2007 - 2021 by shmem <shmem@cpan.org>

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