in reply to Did that file change?

No need to to dupicate all the code (now to make changes it's one place, not two)..
sub FileMeta { my $fh = shift; my ($dev, $inode, $size, $mtime) = ($fh ? stat($fh) : stat(_))[0,1,7 +,9]; return join ":", $size, $dev, $inode, $mtime; }
(i also assumed the ';' in the return string was a typo)

Could also do it w/o temp vars:
sub FileMeta { my $fh = shift; # glue together the size, dev, inode, and mtime return join ":", ($fh ? stat($fh) : stat(_))[7,0,1,9]; }
Another approach (though you noted avoiding modules) would be to MD5 the string and return that so that .. or to include in the unique string the MD5 of the contents (of course that's more overhead as well).

Also have to toss in the obligatory mention of Tripwire for really robust monitoring/reporting (though of course a little snippet like this is handy for certain cases).

Replies are listed 'Best First'.
Re^2: Did that file change?
by whio (Beadle) on Jun 01, 2006 at 09:03 UTC
    You could also replace $fh ? stat($fh) : stat(_) with stat($fh || *_) .
      cool! I was hoping someone would provide something like that since my original attempt of stat($fh || _) didn't work; I also abandoned an ugly eval attempt.

      So how does *_ work/what does the asterisk in that context mean?
      Sweet! Thanks! Then the whole function boils down to a one liner:
      # note: order of the joined parts is not particularily important sub FileMeta { join ":", (stat(shift || *_))[0,1,7,9] }
Re^2: Did that file change?
by ruzam (Curate) on Jun 01, 2006 at 03:10 UTC
    Nice! Thanks!

    I didn't like duping the code, but the stat(_) came as an after thought so my brain was stuck wondering how I would pass _ into the sub. Good call.

    The ';' actually wasn't a typo. It came from a previous use where it made splitting the string easier (to do what I can't remember). But in all honesty I can't think of any valid reason to do it now (or ever again). Skipping the temp variables is a nice touch too.