Sara has asked for the wisdom of the Perl Monks concerning the following question:

Hello guys , in shell I use the following :
$${PDL}.pdl -nt $${PDL}.cxx
to check weather the one file is newer than the other one . how can I do the same exact line in Perl .. thanks

Title edit by tye

Replies are listed 'Best First'.
Re: comparing
by dga (Hermit) on Aug 14, 2002 at 15:57 UTC

    Depends on if you want modified time, access time or inode change time.

    For modified time which is likely the most common.

    if( -M "file.pdl" < -M "file.cxx" ) { print "file.pdl is newer\n"; }

    Likewise with -A for access time and -C for inode change time ( which used to be create time hence the C )

    These filetests list the files age in days since the start of your perl script execution. This is usually positive for a briefly running script if your dates on the filesystem reflect reality and your clock is correct. Thus by comparing 2 numeric quantities you can make these descisions.

    update: mea culpa, changed the text 'negative' to bolded 'positive'. Think days ago.

Re: comparing
by BrowserUk (Patriarch) on Aug 14, 2002 at 15:53 UTC

    if ( -M $file1 < -M $file2) { print $file2, ' is older!', $/; }

    You might also consider reading this perlfunc:_X.


    What's this about a "crooked mitre"? I'm good at woodwork!
Re: comparing
by waswas-fng (Curate) on Aug 14, 2002 at 15:50 UTC
    Can't do the exact same line in perl. =)
    You can do this:
    print "Newer\n" if ((stat($filename))[9] > (stat($filename2))[9]);

    -Waswas
Re: comparing
by kschwab (Vicar) on Aug 14, 2002 at 15:56 UTC
    Would print -1,0,1 if the first file was older,the same, or newer than the second file:
    print "" . ( (stat($ARGV[0]))[9] <=> (stat($ARGV[1]))[9] ) ."\n";
Re: comparing file times [-M Vs. stat()]
by EigenFunctions (Beadle) on Jul 09, 2024 at 16:36 UTC

    I often want to determine if a file is stale based on the date of it's source file. There are two ways to do that. One is with the stat() function while another is with the file test operator '-M'.

    Using stat()

    use constant MTIME => 9; # File modification time if ((stat("$BulletinSrc"))[MTIME] > (stat("$BulletinPDF"))[MTIME]) { $Text .= " PDF is stale \"$BulletinPDF\"\n"; $Errors++; } else { $Text .= " PDF is UP-TO-DATE \"$BulletinPDF\"\n"; }

    Using '-M'

    if (-M "$BulletinSrc" < -M "$BulletinPDF") { $Text .= " PDF is stale \"$BulletinPDF\"\n"; $Errors++; } else { $Text .= " PDF is UP-TO-DATE \"$BulletinPDF\"\n"; }

    Notice that in one 'if' statement the comparison is greater than while the other is less than. That's because stat() returns the date of the file while '-M' returns the length of time since the file was modified.

    Start of program TDS : : : V V |<----- A ----->| |---------------: : TDS : : : V : |-----------: |<--- B --->| length(A) < length(B) means A is stale date(A) > date(B) means A is stale (notice start of program is + irrelevant!) i.e., stat() returns a TDS (time date stamp) -M returns a length since the start of the program

    For me, I have struggled with this until I understood the details.

    I hope this helps.

    Thanks,
      EigenFunctions
      OpSys: Ubuntu 18.04; Perl: Perl 5, version 26, subversion 1 (v5.26.1) built for x86_64-linux-gnu-thread-multi (with 71 registered patches)

      With error checking:

      stat( $qfn ) or die( "stat: $!\n" ); my $mtime = ( stat _ )[9];
      stat( $qfn ) or die( "stat: $!\n" ); my $mtime = -M _;
      defined( -M $qfn ) or die( "stat: $!\n" );