File::...:Object(3pm)

๐Ÿ“ฆ
version 1.00 ยท 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

๐Ÿ’– NAME ๐Ÿ’–

File::Monitor::Object โ€” it monitors a filesystem object for changes. That's it, that's the whole job. ๐Ÿ‘‘

(this one lives in the User Contributed Perl Documentation, manual section 3pm โ€” the page header just gets squished to File::...:Object(3pm) because terminals are dramatic.)


๐ŸŒŸ VERSION ๐ŸŒŸ

This doc describes File::Monitor::Object version 1.00 โœจ


๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

Okay so bestie, listen: this module is created by File::Monitor to monitor a single file or directory. You do not build it yourself. No cap.

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 ๐ŸŒธ

It monitors changes to one single file or directory. That's the assignment.

Do NOT create a File::Monitor::Object directly โ€” instead call watch on File::Monitor. Periodt.

A File::Monitor::Object represents a single file or directory. And here's the iconic part: the corresponding file or directory doesn't have to exist yet. A file being created is literally one of the events it's monitoring for. Similarly, if the file or directory gets deleted, that gets reported as a change too. It's giving "we see everything." ๐Ÿ‘€

Changes of state come back to you as a File::Monitor::Delta object.

You can also query the state of the monitored file or directory as it was at the time of the last scan. But heads up โ€” before scan is called, all of these methods return undef. Lowkey important.

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, you can grab it like this:

my $last_error = $obj->error;

And no โ€” it is not an error for the file being monitored to not exist. We're chill like that. ๐Ÿซถ

Finally, if a directory is being monitored and the files or recurse option was specified, you can get the list of files in the directory like this:

my @contained_files = $obj->files;

So, what do you actually get back?

  • If files was specified โ†’ you get the files and directories immediately below the monitored directory, but not the contents of any subdirectories.
  • If recurse was specified โ†’ you get the entire directory tree below this directory.

In either case, the returned filenames will be complete absolute paths. (Yes bestie, absolute. We don't do relative around here.) ๐ŸŒˆ

๐Ÿš Caveat for Directories ๐Ÿš

Real talk: File::Monitor::Object has no magical way to quickly perform a recursive scan of a directory. So if you point it at a directory containing 1,000,000 files and specify the recurse option, directory scans will take a long time. That's not a bug, that's just physics. ๐Ÿ’€


โœจ INTERFACE โœจ

  • new( $args ) โ€” Creates a new File::Monitor::Object. Do NOT call new directly; use File::Monitor->watch instead. I'm serious. ๐Ÿ™ˆ
  • scan() โ€” Performs a scan of the monitored file or directory and returns a list of changes. That list will either contain a single File::Monitor::Delta object describing all the changes, or it'll be empty if nothing changed.
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 gets captured โ€” but no change will be reported. First scan = just vibes and baseline data. ๐ŸŒฑ

  • callback( [ $event, ] $coderef ) โ€” Registers a callback. If $event is omitted, the callback gets 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
} );

Again: 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 gets resolved relative to the current directory at the time of object creation to make it absolute. Time of creation. Remember that. โฐ
  • files โ€” If you're 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 ๐Ÿ”ฎ

On top of all the above, these methods can 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 actually valid on your platform โ€” because not every platform is built the same, and that's fine. ๐Ÿ’…


๐Ÿšจ DIAGNOSTICS ๐Ÿšจ

  • "%s is read-only" โ€” You tried to modify a read-only accessor. And yeah, it's tempting โ€” for example, you might try 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. Every single one. It's giving "look, don't touch." ๐Ÿšซ

  • "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. Pick one, bestie. Not both. ๐Ÿ˜ญ
  • "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. No name, no monitoring. Simple as that.
  • "A filename must be specified" โ€” You must suppy new with the name of the file or directory to be monitored. (Yeah, it needs a name. That's the whole thing it's monitoring.)

๐Ÿงš CONFIGURATION AND ENVIRONMENT ๐Ÿงš

File::Monitor::Object requires no configuration files and no environment variables. Clean. Minimal. Iconic. โœจ


๐Ÿ“ DEPENDENCIES ๐Ÿ“

None. Look at that. Zero. We love a low-maintenance module. ๐Ÿ’–


๐ŸŒˆ INCOMPATIBILITIES ๐ŸŒˆ

None reported. ๐ŸŒˆ


๐Ÿ› BUGS AND LIMITATIONS ๐Ÿ›

No bugs have been reported. (Ate, honestly. ๐Ÿ’…)

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. Shoutout to Faycal, that was so sweet of them. ๐Ÿซถ


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 ๐Ÿ’€

Okay so this next part is the boring-but-binding bit. I'm keeping it exactly as written, in all its ALL-CAPS legal glory, because lawyers wrote it and I am not about to mess with that:

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 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.

And that's the whole page, bestie! ๐Ÿ’– This one comes to you courtesy of perl v5.36.0, dated 2022-10-21 โ€” so if you're reading this on some future Perl, just know this doc is out here living its best vintage life. Go watch some files. ๐ŸŽ€๐Ÿฆ‹๐Ÿ’…

๐Ÿ“ฆ where this page comes from