Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Sort files into date-labeled subfolders using Perl' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Sort files into date-labeled subfolders using Perl
Authored by: CarlRJ on Feb 25, '08 10:35:02AM

Hmm, for what it's worth, the stat function in the script needlessly performs a second stat(2) system call; the previous "-f" has just done a stat and left the results conveniently sitting around, all error-checked and safe and ready for our use, via the special "_" filehandle. Quoting perlfunc(1):

If any of the file tests (or either the "stat" or "lstat" operators) are given the special filehandle consisting of a solitary underline, then the stat structure of the previous file test (or stat operator) is used, saving a system call.

Thus:

my $mtime = (stat $name)[9] or next;

can become:

my $mtime = (stat _)[9];

And despite what the article says, the script does have error checking on all the file IO, so it looks decently safe to me. It will, of course, cheerfully mass-change whatever directory you tell it to, but then... you told it to, and Unix assumes you know what you're doing.



[ Reply to This | # ]
Sort files into date-labeled subfolders using Perl
Authored by: merlyn on Feb 25, '08 12:22:38PM

I avoided the bizarre "_" mostly because it isn't really saving that much time on this one-off script. It's also one of the few things I invented for Perl, so I can avoid it when I want. :)

Also, the biggest risk of loss is that you might rename a file onto the same name in the subdir, losing one of the two files in the process, since it's perfectly legal to rename(2) to an existing name. I didn't check for that, but it would take only one more statement.



[ Reply to This | # ]