File::Modified(3pm)

πŸ“¦
version 0.10 Β· 5 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

File::Modified β€” bestie, it checks intelligently if files have changed. No cap. πŸ¦‹


✨ SYNOPSIS ✨

Okay so first example, a little loop that just... vibes and watches your config files:

  use strict;
  use File::Modified;

  my $d = File::Modified->new(files=>['Import.cfg','Export.cfg']);

  while (1) {
    my (@changes) = $d->changed;

    if (@changes) {
      print "$_ was changed\n" for @changes;
      $d->update();
    };
    sleep 60;
  };

Second example β€” a script that literally knows when any of its own modules have changed 😭 the main character energy:

  use File::Modified;
  my $files = File::Modified->new(files=>[values %INC, $0]);

  # We want to restart when any module was changed
  exec $0, @ARGV if $files->changed();

🌸 DESCRIPTION 🌸

This module gives you a whole simple lil mechanism for figuring out when the contents of one or more files have changed. It was initially intended for programs to detect when their configuration files (or the module they rely on) have changed. Periodt. πŸ’…

Right now there are two methods of change detection implemented: "mtime" and "MD5". And the "MD5" method will fall back to use timestamps if the "Digest::MD5" module cannot be loaded. (Backup plan? Iconic. πŸ‘‘)

There are a number of other modules on CPAN that provide similar functionality; they are listed in "SEE ALSO" below. Go look, iykyk.

πŸ’œ new %ARGS

Creates a new instance. The %ARGS hash has two possible keys: "Method", which denotes the method used for checking as default, and "Files", which takes an array reference to the filenames to watch.

πŸ’œ add filename, method

Adds a new file to watch. "method" is the method (or rather, the subclass of "File::Modified::Signature") to use to determine whether a file has changed or not. The result is either the "File::Modified::Signature" subclass or undef if an error occurred.

πŸ’œ addfile LIST

Adds a list of files to watch. The method used for watching is the default method as set in the constructor. The result is a list of "File::Modified::Signature" subclasses.

πŸ’œ update

Updates all signatures to the current state. All pending changes are discarded. Gone. Poof. πŸ™ˆ

πŸ’œ changed

Returns a list of the filenames whose files did change since the construction or the last call to "update" (whichever last occurred).


πŸŽ€ Signatures πŸŽ€

So the module also creates a new namespace "File::Signature", which sometime will evolve into its own module in its own file. (Sometime. We're manifesting. ✨) A file signature is most likely of little interest to you; the only time you might want to access the signature directly is to store the signature in a file for persistence and easy comparision whether an index database is current with the actual data.

The interface is settled, there are two methods, "as_scalar" and "from_scalar", that you use to freeze and thaw the signatures. The implementation of these methods is very frugal, there are no provisions made against filenames that contain weird characters like "\n" or "|" (the pipe bar) β€” both will be likely to mess up your one-line-per-file database. πŸ’€ An interesting method could be to URL-encode all filenames, but I will visit this topic in the next release. Also, complex (that is, non-scalar) signatures are handled rather ungraceful at the moment.

Currently, I'm planning to use Text::Quote as a quoting mechanism to protect against multiline filenames.


πŸ¦‹ Adding new methods for signatures πŸ¦‹

Adding a new signature method is as simple as creating a new subclass of "File::Signature". See "File::Signature::Checksum" for a simple example. There is one point of laziness in the implementation of "File::Signature": the "check" method can only compare strings instead of arbitrary structures (yes, there ARE things that are easier in Python than in Perl 😭). "File::Signature::Digest" is a wrapper for Gisle Aas' Digest module and allows you to use any module below the "Digest" namespace as a signature, for example "File::Signature::MD5" and "File::Signature::SHA1".


🌈 TODO 🌈

  • Make the simple persistence solution for the signatures better using Text::Quote.
  • Allow complex structures for the signatures.
  • Document "File::Modified::Signature" or put it down into another namespace.
  • Extract the "File::Modified::Signature" subclasses out into their own file.
  • Create an easy option to watch a whole directory tree.

πŸ’… EXPORT πŸ’…

None by default. Nothing. Zero. Nada. 🫢


⭐ SEE ALSO ⭐

  • File::Monitor will watch a file or directory, invoking a callback when it changes.
  • File::Monitor::Lite is similar to File::Monitor, but can also let you know about new files being created.
  • File::Monitor::Simple watches a directory for changes to any files whose name matches a regular expression.
  • File::IfModified provides a function that can be used to check whether a file has been modified since the last time you checked.
  • File::ChangeNotify provides an API for watching all files in a given directory. It provides several mechanisms for doing this, and a base-class that you can subclass to write your own watcher.
  • File::Signature provides some lower-level functions than File::Modified, which are used to identify whether a file has changed by comparing its MD5 digest with an earlier snapshot.
  • File::Stat::Trigger will invoke one of your callbacks if the "stat()" details of a file change.
  • Win32::FileSystem::Watcher provides a Windows-specific solution for watching for changes to a filesystem. The documentation is extremely limited, so I can't tell if you can limit it a specific directory. 😭
  • App::watcher comes with a script that will run a command if any of the files in a directory are changed.
  • IO::Async::File watches an open filehandle or 'named filesystem entity' for changes in its "stat()" fields.
  • POE::Component::DirWatch watches a directory for new files or directories, invoking a user-supplied callback function when one is seen.
  • WWW::Monitor is similar to File::Monitor, but checks URLs rather than files.

πŸ”₯ REPOSITORY πŸ”₯

<https://github.com/neilb/File-Modified>


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

Copyright (C) 2002 Max Maischein


πŸ’– AUTHOR πŸ’–

Max Maischein, <corion@cpan.org>

Please contact me if you find bugs or otherwise improve the module. More tests are also very welcome!


And that's the tea, bestie 🫢 this was perl v5.36.0, dated 2022-10-13, from the File::Modified(3pm) page β€” go water your plants and watch your files, slay. πŸ’…βœ¨

πŸ“¦ where this page comes from