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

Suppose there is a function that generates one file based on another:

sub compile($$) { my ($source_filename, $output_filename) = @_; ... # compile source into output }

and now we only want to execute the function if $source_filename has been modified more recently than $output_filename...

sub compare_modification_times($$) { my ($file_a, $file_b) = @_; ??? } my $src = ""; my $dest = ""; if (compare_modification_times($src, $dest) < 0) { compile($src, $dest); }

I would like recommendations on the most robust and portable way to implement compare_modification_times. Any ideas?

Thanks for your time.

Andrew Tomazos  |  |

Replies are listed 'Best First'.
Re: Comparing modification times like make
by Anonymous Monk on Oct 27, 2008 at 12:52 UTC
Re: Comparing modification times like make
by Krambambuli (Curate) on Oct 27, 2008 at 12:57 UTC
    The Perl Cookbook recommends

    Recipe 9.1. Getting and Setting Timestamps

    9.1.1. Problem

    You need to retrieve or alter when a file was last modified (written or changed) or accessed (read).

    9.1.2. Solution

    Use stat to get those times and utime to set them. Both functions are built into Perl:

    ($READTIME, $WRITETIME) = (stat($filename))[8,9];

    utime($NEWREADTIME, $NEWWRITETIME, $filename);

    9.1.3. Discussion

    But I do not know how well this does on Windows.

    Updated formatting error, to show the code correctly - thanks Perlbotics

Re: Comparing modification times like make
by JavaFan (Canon) on Oct 27, 2008 at 12:54 UTC
    You want the "stat" function. perldoc -f stat.