Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comparing modification times like make

by tomazos (Deacon)
on Oct 27, 2008 at 12:42 UTC ( [id://719727]=perlquestion: print w/replies, xml ) Need Help??

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 = "wee.foo"; my $dest = "wee.bar"; 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  |  andrew@tomazos.com  |  www.tomazos.com

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

    Krambambuli
    ---
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://719727]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-24 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found