#!/usr/bin/env perl # # latest: execute the given command with a most recently modified file i nthe current directory # # $Id$ # see also ./latest use strict; use warnings; use File::stat; use File::Spec::Functions qw(catfile); my @dirs; while ( @ARGV && -d $ARGV[0] ) { push( @dirs, shift(@ARGV) ); } @ARGV or die "supply a command as argument[s]\n"; @dirs or @dirs = ('.'); foreach my $d (@dirs) { opendir( my $dh, $d ) or die "cannot read $d\n"; my @f = map { catfile( $d, $_ ) } readdir($dh); closedir($dh); my $latest_mtime = -1; my $latest_f; foreach my $f ( grep { -f && -r } @f ) { my $mtime = stat($f)->mtime or next; if ( defined($mtime) && $mtime > $latest_mtime ) { $latest_mtime = $mtime; $latest_f = $f; } } defined($latest_f) or die "no latest plain file found in .\n"; exec( @ARGV, $latest_f ) or die join( 'failed to execute:', @ARGV, $latest_f ), "\n"; }