File::Repl - Perl module that provides file replication utilities
use File::Repl;
%con = { dira => 'C:/perl', dirb => 'M:/perl', verbose => '1', age => '10', };
$ref=File::Repl->New(\%con); $r1 = $ref->Update('\.p(l|m)','a<>b',1); $r2 = $ref->Update('\.t.*','a<>b',1,'\.tmp$');
The File:Repl provides simple file replication and management utilities. Its main functions are
This method also allows files to be tombstoned (ie removed from the replicated file sets). A file is tombstoned by appending .remove to the file name. The first Update will cause the file to be set to zero size, and any replica files to be renamed (so the original file does not return). The next update after the ttl has expired will cause deletion of all file replicas.
If a directory is tombstoned (by adding .remove to its name) the directory and contents are removed and a file with the directory name and the .remove suffix replaces it. The file is removed as a normally tombstoned file.
The Update method returns a reference to data structures evaluated during the method call. This is based on the method arguments, and allows arrays and hash's of the file structure meeting the selection criteria to be returned. See EXAMPLES. Note that the aonly, bonly, amatch and bmatch array references, and the common hash reference all refer to the file structure state BEFORE the Update method makes any changes.
e.g. /\.pl$/\.perl/
This examplewill rename all files (that meet regex and noregex criteria) from .pl to .perl
File::Find; File::Copy; File::Basename;
Win32::AdminMisc (Win32 platforms only) Win32::API (Win32 platforms only)
On FAT filesystems, mtime resolution is 1/30th of a second. A fudge of 2 seconds is used for synching FAT with other filesystems. Note that FAT filesystems save the local time in UTC (GMT).
On FAT filesystems, ``stat'' adds TZ_BIAS to the actual file times (atime, ctime and mtime) and conversley ``utime'' subtracts TZ_BIAS from the supplied parameters before setting file times. To maintain FAT at UTC time, we need to do the opposite.
If we don't maintain FAT filesystems at UTC time and the repl is between FAT and NON-FAT systems, then all files will get replicated whenever the TZ or Daylight Savings Time changes.
A simple example that retrieves and prints the working variables from the Update method
$ref=File::Repl->New(\%hash); $my=$ref->Update('.*','A>B',1);
$sub = sub { # simple sub that determines the reference type and prints the associated values my ($ref) =$_[0]; if ( ref($ref) eq "SCALAR" ) { print " SCALAR $ref\n"; }elsif( ref($ref) eq "ARRAY" ) { print " ARRAY"; foreach (@$ref) { print "\t$_\n"; } }elsif( ref($ref) eq "HASH" ) { print " HASH "; foreach (keys %$ref) { print "\t$_ => $$ref{$_}\n"; } }elsif( ref($ref) eq "REF" ) { &$sub($$ref); }else{ print " VALUE\t$ref\n"; } print "\n"; }; foreach my $key (sort keys %$my) { print "$key:\n"; &$sub($$my{$key}); }
and a sample output
References and values of $my amatch: ARRAY /a/b/c/d/e/dummy.c /a/b /a/b/c/d/e/bar.pl /a/b/c/d/e/ABCDE.XYZ /a /a/b/c/d/e/foo.tst /a/b/c/d /a/b/c/d/e /a/b/c
aonly: ARRAY /a/b/c/d/e/foo.tst /a/b/c/d/e/dummy.c /a/b/c/d/e/ABCDE.XYZ
bmatch: ARRAY /a/b /a/b/c/d/e/bar.pl /a /a/b/c/d /a/b/c/d/e /a/b/c
bonly: ARRAY common: HASH /a/b => /a/b /a/b/c/d/e/bar.pl => /a/b/c/d/e/bar.pl /a => /a /a/b/c/d => /a/b/c/d /a/b/c/d/e => /a/b/c/d/e /a/b/c => /a/b/c
The amatch and bmatch array references are those files and directory's in the adir and bdir structures that met the regex and negregex regular expression criteria. The aonly and bonly array references give those files and directories that exist only in that directory structure.
The common hash reference identifies those files and directories that exist in both dira and dirb directory structures. The key is for the dira, and value for dirb. Note that, depending on the nocase value the key and value may show differences in case on FAT and NTFS file systems.
A similar approach could be used to determine the referenced data from $ref. This would give access to
In addition the scalar values of various settings determined when the New method is called can be determined.
Dave Roberts
You can send bug reports and suggestions for improvements on this module to me at DaveRoberts@iname.com. However, I can't promise to offer any other support for this script.
This module is Copyright © 2000, 2001 Dave Roberts. All rights reserved.
This script is free software; you can redistribute it and/or modify it under the same terms as Perl itself. This script is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. The copyright holder of this script can not be held liable for any general, special, incidental or consequential damages arising out of the use of the script.
$Log: Repl.pm $ Revision 1.14 2001/07/12 21:51:50 jj768 additional documentation - and minor code changes
Revision 1.13 2001/07/12 15:18:43 Dave.Roberts code tidy up and reorganisation fixed logic errors (A>B! mode in Update method was not copying new files from A to B), also for A<B! removed several local variables and used referred object directly
Revision 1.12 2001/07/11 10:30:16 Dave.Roberts resolved various errors introduced in 1.11 - mainly associsated with reference errors rehacked fc subroutine - to give more logical messages still in need of more documentation - esp of object reference returned and associated variables
Revision 1.11 2001/07/06 14:52:53 jj768 double referencing of blessed object removed (from New method) and subsequent methods updated. Requires Testing. Update and other methods now return reference to data arrays and hashs evaluated during method call
Revision 1.10 2001/07/06 08:23:48 Dave.Roberts code changes to allow the colume info to be detected correctly using Win32::AdminMisc when a drive letter is specified (was only working with UNC names)
Revision 1.9 2001/06/27 13:35:53 Dave.Roberts minor presentation changes
Revision 1.8 2001/06/27 12:59:22 jj768 logic to prevent ``Use of uninitialized value in pattern match (m//)'' errors on use of $vol{FileSystemName}
Revision 1.6 2001/06/21 12:32:15 jj768
*** empty log message ***
Revision 1.5 2001/06/20 20:39:21 Dave.Roberts minor header changes
Revision 1.4 2001/06/20 19:55:21 jj768 re-built module source files as per perlmodnew manpage
Revision 1.1 2001/06/20 19:53:03 Dave.Roberts Initial revision
Revision 1.3.5.0 2001/06/19 10:34:11 jj768 Revised calling of the New method to use a hash reference, rather than a hash directly
Revision 1.3.4.0 2001/06/19 09:48:38 jj768 intermediate development revision. Introduced Delete method and the _generic subroutine (used for all methods except New) this is preparatory to the hash being passed as a reference
Revision 1.3.3.0 2001/06/14 15:42:48 jj768 minor code changes in constructing hash and improvement in documentation -still need more docs on Timezones.