NAME

File::Monitor::Object - Monitor a filesystem object for changes.

VERSION

This document describes File::Monitor::Object version 0.10

SYNOPSIS

Created by File::Monitor to monitor a single file or directory.

use File::Monitor;
use File::Monitor::Object;

my $monitor = File::Monitor->new();

for my $file ( @files ) {
    $monitor->watch( $file );
}

# First scan just finds out about the monitored files. No changes
# will be reported.
$monitor->scan;

# Later perform a scan and gather any changes
for my $change ( $monitor->scan ) {
    # $change is a File::Monitor::Delta
}

DESCRIPTION

Monitors changes to a single file or directory. Don't create a File::Monitor::Object directly; instead call watch on File::Monitor.

A File::Monitor::Object represents a single file or directory. The corresponding file or directory need not exist; a file being created is one of the events that is monitored for. Similarly if the file or directory is deleted that will be reported as a change.

Changes of state are returned as a File::Monitor::Delta object.

The state of the monitored file or directory at the time of the last scan can be queried. Before scan is called these methods will all return undef. The following methods return the value of the corresponding field from "stat" in perlfunc:

dev inode mode num_links uid gid rdev size
atime mtime ctime blk_size blocks

For example:

my $file_size = $object->size;
my $modified  = $object->mtime;

If any error occured during the previous scan it may be retrieved like this:

my $last_error = $obj->error;

It is not an error for the file being monitored not to exist.

Finally if a directory is being monitored and the files or recurse option was specified the list of files in the directory may be retrieved like this:

my @contained_files = $obj->files;

If files was specified this will return the files and directories immediately below the monitored directory but not the contents of any subdirectories. If recurse was specified the entire directory tree below this directory will be returned.

In either case the returned filenames will be complete absolute paths.

Caveat for Directories

Note that File::Monitor::Object has no magical way to quickly perform a recursive scan of a directory. If you point it at a directory containing 1,000,000 files and specify the recurse option directory scans will take a long time.

INTERFACE

new( $args )

Create a new File::Monitor::Object. Don't call new directly; use instead File::Monitor->watch.

scan()

Perform a scan of the monitored file or directory and return a list of changes. The returned list will contain either a single File::Monitor::Delta object describing all changes or will be empty if no changes occurred.

if ( my $change = $object->scan ) {
    # $change is a File::Monitor::Delta that describes all the
    # changes to the monitored file or directory.
}

When scan is first called the current state of the monitored file/directory will be captured but no change will be reported.

callback( [ $event, ] $coderef )

Register a callback. If $event is omitted the callback will be called for all changes. Specify $event to limit the callback to certain event types. See File::Monitor::Delta for a full list of events.

$object->callback( sub {
    # called for all changes
} );

$object->callback( metadata => sub {
    # called for changes to file/directory metatdata
} );

See File::Monitor::Delta for a full list of events that can be monitored.

name

Returns the absolute name of the file or directory being monitored. If new was passed a relative path it is resolved relative to the current directory at the time of object creation to make it absolute.

files

If monitoring a directory and the recurse or files options were specified to new, files returns a list of contained files. The returned filenames will be absolute paths.

Other Accessors

In addition to the above the following methods may be called to return the value of the corresponding field from "stat" in perlfunc:

dev inode mode num_links uid gid rdev size
atime mtime ctime blk_size blocks

For example:

my $inode = $obj->inode;

Check the documentation for "stat" in perlfunc to discover which fields are valid on your platform.

DIAGNOSTICS

%s is read-only

You have attempted to modify a read-only accessor. It may be tempting for example to attempt to change the name of the monitored file or directory like this:

# Won't work
$obj->name( 'somefile.txt' );

All of the attributes exposed by File::Monitor::Object are read-only.

When options are supplied as a hash there may be no other arguments

When creating a new File::Monitor::Object you must either supply new with a reference to a hash of options or, as a special case, pass a filename and optionally a callback.

The name option must be supplied

The options hash must contain a key called name that specifies the name of the file or directory to be monitored.

A filename must be specified

You must suppy new with the name of the file or directory to be monitored.

CONFIGURATION AND ENVIRONMENT

File::Monitor::Object requires no configuration files or environment variables.

DEPENDENCIES

None.

INCOMPATIBILITIES

None reported.

BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to bug-file-monitor@rt.cpan.org, or through the web interface at http://rt.cpan.org.

AUTHOR

Andy Armstrong <andy@hexten.net>

Faycal Chraibi originally registered the File::Monitor namespace and then kindly handed it to me.

LICENCE AND COPYRIGHT

Copyright (c) 2007, Andy Armstrong <andy@hexten.net>. All rights reserved.

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

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.