Win32::FileNotify - Monitor file changes


Win32-FileNotify documentation Contained in the Win32-FileNotify distribution.

Index


Code Index:

NAME

Top

Win32::FileNotify - Monitor file changes

SYNOPSIS

Top

  use Win32::FileNotify;

  my $file = './test.txt';
  my $notify = Win32::FileNotify->new( $file );
  $notify->wait;

  print $file, " wurde veraendert\n";

DESCRIPTION

Top

This is a wrapper around Win32::ChangeNotify. With Win32::FileNotify you can monitor one specific file and you get notified when the file has changed.

METHODS

Top

new

  my $filename = '/path/to/file.txt';
  $notify = Win32::FileNotify->new( $filename );

creates a new object for the given file.

wait

  $notify->wait;

See Win32::IPC

SEE ALSO

Top

Win32::ChangeNotify

AUTHOR

Top

Renee Baecker, <module@renee-baecker.de>

COPYRIGHT AND LICENSE

Top


Win32-FileNotify documentation Contained in the Win32-FileNotify distribution.

package Win32::FileNotify;

use strict;
use warnings;

use Carp;
use File::Basename;
use File::stat;
use Win32::ChangeNotify;

our $VERSION = '0.31';

sub new{
    my ($class,$file) = @_;
    
    croak "file does not exist" unless -e $file;
    
    my $dir = dirname( $file );
       $dir ||= '.';
       
    my $stat = stat $file if -e $file;
    
    my $self = bless {},$class;
    $self->_modified( $stat );
    $self->_file( $file );
    $self->_obj( $dir );

    return $self;
}

sub wait{
    my ($self) = @_;
    my $return;
    
    while( 1 ){
        $self->_obj->wait or last;
        $self->_obj->reset;
        if( $self->_is_changed ){
            $return = 1;
            last;
        }
    }

    return $return;
}

sub _is_changed{
    my ($self) = @_;
    my $return = 0;
    
    my $stat = stat $self->_file;
    
    if( $stat->mtime != $self->_modified ){
        $return = 1;
        $self->_modified( $stat );
    }
    
    return $return;
}

sub _modified{
    my ($self,$stat) = @_;
    
    if( $stat ){
        $self->{__modified} = $stat->mtime;
    }

    return $self->{__modified};
}

sub _file{
    my ($self,$file) = @_;
    
    if( defined $file ){
        $self->{__file} = $file;
    }

    return $self->{__file};
}

sub _obj{
    my ($self,$dir) = @_;
    
    if( defined $dir ){
        $self->{__obj} = Win32::ChangeNotify->new( $dir, 0, 'LAST_WRITE' );
    }

    return $self->{__obj};
}

1;

__END__
# Below is stub documentation for your module. You'd better edit it!