AnyEvent-Inotify-Simple

 view release on metacpan or  search on metacpan

lib/AnyEvent/Inotify/Simple.pm  view on Meta::CPAN

    return $file if $file->is_absolute;
    return $file->absolute($self->directory)->resolve->absolute;
}

sub handle_move_from {
    my ($self, $file, $cookie) = @_;

    $self->cookie_jar->{from}{$cookie} = $file;
}

sub handle_move_to {
    my ($self, $to, $cookie) = @_;

    my $from = delete $self->cookie_jar->{from}{$cookie};
    confess "Invalid move cookie '$cookie' (moved to '$to')"
        unless $from;

    my $abs = eval { $self->rel2abs($to) };
    $self->_watch_directory($abs) if $abs && -d $abs;

    $self->handle_move($from, $to);
}

# inject our magic
before 'handle_create' => sub {
    my ($self, $dir) = @_;
    my $abs = eval { $self->rel2abs($dir) };
    return unless $abs && -d $abs;
    $self->_watch_directory($abs);
};

sub DEMOLISH {
    my $self = shift;
    return unless $self->inotify;
    for my $w (values %{$self->inotify->{w}}){
        next unless $w;
        $w->cancel;
    }
}

1;

__END__

=head1 NAME

AnyEvent::Inotify::Simple - monitor a directory tree in a non-blocking way

=head1 SYNOPSIS

   use AnyEvent::Inotify::Simple;
   use EV; # or POE, or Event, or ...

   my $inotify = AnyEvent::Inotify::Simple->new(
       directory      => '/tmp/uploads/',
       wanted_events  => [ qw(create move) ],
       event_receiver => sub {
           my ($event, $file, $moved_to) = @_;
           given($event) {
               when('create'){
                  say "Someone just uploaded $file!"
               }
           };
       },
   );

   EV::loop;

=head1 DESCRIPTION

This module is a wrapper around L<Linux::Inotify2> that integrates it
with an L<AnyEvent> event loop and makes monitoring a directory
simple.  Provide it with a C<directory>, C<event_receiver>
(L<AnyEvent::Inotify::Simple::EventReceiver>), and an optional coderef
C<filter> and/or optional array ref C<wanted_events>, and it will
monitor an entire directory tree.  If something
is added, it will start watching it.  If something goes away, it will
stop watching it.  It also converts C<IN_MOVE_FROM> and C<IN_MOVE_TO>
into one virtual event.

Someday I will write more, but that's really all that happens!

=head1 METHODS

None!  Create the object, and it starts working immediately.  Destroy
the object, and the inotify state and watchers are automatically
cleaned up.

=head1 REPOSITORY

Forks welcome!

L<http://github.com/jrockway/anyevent-inotify-simple>

=head1 AUTHOR

Jonathan Rockway C<< <jrockway@cpan.org> >>

Current maintainer is Rob N ★ C<< <robn@robn.io> >>

=head1 COPYRIGHT

Copyright 2009 (c) Jonathan Rockway.  This module is Free Software.
You may redistribute it under the same terms as Perl itself.



( run in 1.114 second using v1.01-cache-2.11-cpan-483215c6ad5 )