Config-Reload
view release on metacpan or search on metacpan
lib/Config/Reload.pm view on Meta::CPAN
has wait => (
is => 'rw',
default => quote_sub q{ 60 },
);
has checked => ( is => 'rw' );
has loaded => ( is => 'rw' );
has error => ( is => 'rw' );
has _md5 => ( is => 'rw' ); # caches $self->md5($self->found)
has _zomg => ( is => 'rw', handles => [qw(find found)] );
has _config => ( is => 'rw' );
sub BUILD {
my ($self, $given) = @_;
# don't pass to Config::ZOMG
delete $given->{$_} for qw(wait error checked);
$self->_zomg( Config::ZOMG->new($given) );
}
sub load {
my $self = shift;
my $zomg = $self->_zomg;
if ($self->_config) {
if (time < $self->checked + $self->wait) {
return $self->_config;
}
if ($self->_md5 eq files_hash( $zomg->find )) {
$self->checked(time);
return $self->_config;
} else {
$self->_config(undef);
}
}
$self->checked(time);
try {
$self->error(undef);
$self->_config( $zomg->reload ); # may die on error
$self->loaded(time);
$self->_md5( files_hash( $self->found ) );
} catch {
$self->error($_);
$self->loaded(undef);
$self->_md5( files_hash() );
$self->_config( { } );
};
return $self->_config;
}
sub files_hash {
md5_hex( map { my @s = stat($_); ($_, $s[9], $s[7]) } sort @_ );
}
1;
__END__
=pod
=head1 NAME
Config::Reload - Load config files, reload when files changed.
=head1 VERSION
version 0.21
=head1 SYNOPSIS
my $config = Config::Reload->new(
wait => 60, # check at most every minute (default)
... # passed to Config::ZOMG, e.g. file => $filename
);
my $config = $config->load;
sleep(60);
$config = $config->load; # reloaded
=head1 DESCRIPTION
This Perl package loads config files via L<Config::ZOMG> which is based on
L<Config::Any>. Configuration is reloaded on file changes (based on file names
and last modification time).
This package is highly experimental and not fully covered by unit tests!
=head1 METHODS
=head2 new
Returns a new C<Config::Reload> object. All arguments but C<wait>, C<error>
and C<checked> are passed to the constructor of L<Config::ZOMG>.
=head2 load
Get the configuration, possibly (re)loading configuration files. Always returns
a hash reference, on error this C< { } >.
=head2 wait
Get or set the number of seconds to wait between checking. Set to 60 (one
minute) by default.
=head2 checked
Returns a timestamp of last time files had been checked.
=head2 loaded
( run in 0.801 second using v1.01-cache-2.11-cpan-39bf76dae61 )