package Module::Refresh;

use strict;
use vars qw( $VERSION %CACHE );

$VERSION = "0.17";


    # Turn on the debugger's symbol source tracing
    $^P |= 0x10;

    # Work around bug in pre-5.8.7 perl where turning on $^P
    # causes caller() to be confused about eval {}'s in the stack.
    # (See for more info.)
    eval 'sub DB::sub' if $] < 5.008007;

=head1 NAME

Module::Refresh - Refresh %INC files when updated on disk


    # During each request, call this once to refresh changed modules:


    # Each night at midnight, you automatically download the latest
    # Acme::Current from CPAN.  Use this snippet to make your running
    # program pick it up off disk:



This module is a generalization of the functionality provided by
L<Apache::StatINC> and L<Apache::Reload>.  It's designed to make it
easy to do simple iterative development when working in a persistent

It does not require mod_perl.


=head2 new

Initialize the module refresher.


sub new {
    my $proto = shift;
    my $self = ref($proto) || $proto;
    $self->update_cache($_) for keys %INC;
    return ($self);

=head2 refresh

Refresh all modules that have mtimes on disk newer than the newest ones we've got.
Calls C<new> to initialize the cache if it had not yet been called.

Specifically, it will renew any module that was loaded before the previous call
to C<refresh> (or C<new>) and has changed on disk since then.  If a module was
both loaded for the first time B<and> changed on disk between the previous call 
and this one, it will B<not> be reloaded by this call (or any future one); you
will need to update the modification time again (by using the Unix C<touch> command or
making a change to it) in order for it to be reloaded.


sub refresh {
    my $self = shift;

    return $self->new if !%CACHE;

    foreach my $mod ( sort keys %INC ) {
    return ($self);

=head2 refresh_module_if_modified $module

If $module has been modified on disk, refresh it. Otherwise, do nothing


sub refresh_module_if_modified {
    my $self = shift;
    return $self->new if !%CACHE;
    my $mod = shift;

    if (!$INC{$mod}) {
    } elsif ( !$CACHE{$mod} ) {
    } elsif ( $self->mtime( $INC{$mod} ) ne $CACHE{$mod} ) {


=head2 refresh_module $module

Refresh a module.  It doesn't matter if it's already up to date.  Just do it.

Note that it only accepts module names like C<Foo/>, not C<Foo::Bar>.


sub refresh_module {
    my $self = shift;
    my $mod  = shift;


    local $@;
    eval { require $mod; 1 } or warn $@;


    return ($self);

=head2 unload_module $module

Remove a module from C<%INC>, and remove all subroutines defined in it.


sub unload_module {
    my $self = shift;
    my $mod  = shift;
    my $file = $INC{$mod};

    delete $INC{$mod};
    delete $CACHE{$mod};

    return ($self);

=head2 mtime $file

Get the last modified time of $file in seconds since the epoch;


sub mtime {
    return join ' ', ( stat( $_[1] ) )[ 1, 7, 9 ];

=head2 update_cache $file

Updates the cached "last modified" time for $file.


sub update_cache {
    my $self      = shift;
    my $module_pm = shift;

    $CACHE{$module_pm} = $self->mtime( $INC{$module_pm} );

=head2 unload_subs $file

Wipe out subs defined in $file.


sub unload_subs {
    my $self = shift;
    my $file = shift;

    foreach my $sym ( grep { index( $DB::sub{$_}, "$file:" ) == 0 }
        keys %DB::sub )

        warn "Deleting $sym from $file" if ( $sym =~ /freeze/ );
        eval { undef &$sym };
        warn "$sym: $@" if $@;
        delete $DB::sub{$sym};
        { no strict 'refs';
            if ($sym =~ /^(.*::)(.*?)$/) {
                delete *{$1}->{$2};

    return $self;

# "Anonymize" all our subroutines into unnamed closures; so we can safely
# refresh this very package.
    no strict 'refs';
    foreach my $sym ( sort keys %{ __PACKAGE__ . '::' } ) {
            if $sym eq
            'VERSION';    # Skip the version sub, inherited from UNIVERSAL
        my $code = __PACKAGE__->can($sym) or next;
        delete ${ __PACKAGE__ . '::' }{$sym};
        *$sym = sub { goto &$code };



=head1 BUGS

When we walk the symbol table to whack reloaded subroutines, we don't
have a good way to invalidate the symbol table properly, so we mess up
on things like global variables that were previously set.

=head1 SEE ALSO

L<Apache::StatINC>, L<Module::Reload>


Copyright 2004,2011 by Jesse Vincent E<lt>jesse@bestpractical.comE<gt>,
Audrey Tang E<lt>audreyt@audreyt.orgE<gt>

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

See L<>