So, I needed a function that would compare the timestamp of one file to a directory full of files. If the one first was newer than all the others, the function would continue; otherwise, it would return.

I tried to be clever about this and got the following:

sub chk_snapshot { my $dir=shift; return unless -f "$dir/.snapshot"; my $ft=(stat("$dir/.snapshot"))[9]; my @fts=map { (stat($_))[9] } grep -f $_,($0,glob "$dir/*"); ## return if .snapshot is older than any for my $x (@fts) { return if $x > $ft; } warn "snapshot is newer"; snarf_file("$dir/.snapshot"); }
What I wanted to do is get rid of the for my $x (@fts) construct and somehow combine it with the map statement (getting rid of the @fts variable at the same time). I had tried something like
return if map { (stat($_))[9] > $ft } grep -f $_,($0,glob "$dir/*");
But that didn't work because map returns (in this case) an array of undef's, not an empty array; so the return in that construct is always executed.

So my question is, does anyone know how to achieve the goal of gathering the filestamps AND checking them in a single line (like my non-functional return if map ...)?

TIA!


In reply to Clever timestamp comparison by mr.nick

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.