in reply to Is it possible to localize the stat/lstat cache?

I like your version with %Stat (I wouldn't capitalize it in my style, but do what fits in your program's style of course).

A single call to stat though will return your mode, size, atime, mtime, ctime, and more. It's not from the current user's point of view like the -X functions, and it's not directly yes/no answers either. It may be handy to get all that information at once.

What I'd like to know is why you have an issue with building %Stat this way. Do you really want to localize the magical _ rather than put its results in the hash? What are you wanting that to buy you?

Replies are listed 'Best First'.
Re^2: Is it possible to localize the stat/lstat cache?
by Anonymous Monk on Apr 20, 2015 at 12:34 UTC

    It turns out that the use of %Stat is very similar to what File::stat does under the covers, but my version only does those tests that I know I will need (saving a few cycles of processor time, most likely). In my case (since I know in advance which tests I need to worry about), this is sufficient.

    I don't have an issue with building %Stat this way. It actually is (almost) the best solution to my need. I like to know alternatives, though, and realized that there was no stat equivalent to local $_; or local *FileHandle; in Perl. This seems like an oversight to me, as it should be a best practice to localize any global variables that you use in a function (especially one that is part of a module) so that you don't have to worry about side effects for the caller.

    As for my comment above about %Stat being almost the best solution, I teaked my solution to drop the hash entirely (which avoids construction of the hash object, looking up the key in the hash, etc.). I ended up using one variable per test that I needed. Since these variables were declared outside the function and set right after the call to stat/lstat, they had a one-time construction cost plus the assignment per file and then multiple uses. In my specific case, this was the most efficient and simple solution.

      Ooops. Wasn't logged in when I said that.