[Chicago-talk] perl touch

Andy_Bach at wiwb.uscourts.gov Andy_Bach at wiwb.uscourts.gov
Tue May 9 13:48:47 PDT 2006


You could look at touch.pl:
http://search.cpan.org/src/WYANT/Win32API-File-Time-0.005/touch.pl

for winx and uses:
use Win32API::File::Time qw{GetFileTime SetFileTime};

or Perl Power Tools "touch"
http://search.cpan.org/~cwest/ppt-0.14/bin/touch

which uses sysopen and utime
    utime LIST
             Changes the access and modification times on each
             file of a list of files.  The first two elements of
             the list must be the NUMERICAL access and
             modification times, in that order.  Returns the
             number of files successfully changed.  The inode
             change time of each file is set to the current time.
             This code has the same effect as the "touch" command
             if the files already exist:

                 #!/usr/bin/perl
                 $now = time;
                 utime $now, $now, @ARGV;

Here's the important part:

   # Check if the file exists. If not, create it.
    unless (-f $file) {
        next if $no_create;
        local *FILE;
        require Fcntl;  # Import
        sysopen FILE, $file, Fcntl::O_CREAT () or do {
            warn "$file: $!\n";
            next;
        };
        close FILE;

        # Nothing to be done, unless time different than now.
        next unless $special_time;
    }

    my ($aorig, $morig) = (stat $file) [8, 9] or do {
        warn "$file: $!\n";
        next;
    };

    my $aset = $access_time       ? $atime : $aorig;
    my $mset = $modification_time ? $mtime : $morig;

    utime $aset, $mset, $file or do {
        warn "$file: $!\n";
        next;
    };

Andy Bach, Sys. Mangler
Internet: andy_bach at wiwb.uscourts.gov 
VOICE: (608) 261-5738  FAX 264-5932

It was really tempting to name the program "sex" (for either "Smart  EXec" 
 or
"Send-EXpect"), but good sense (or perhaps just Puritanism) prevailed.
Don Libes - on naming "Expect"


More information about the Chicago-talk mailing list