in reply to Can the special underline filehandle (stat cache) be cleared?

$ perl -le' print defined -e _ ? "defined" : "undef"; lstat q!test.txt!; print defined -e _ ? "defined" : "undef"; lstat q!/not_a_real_file!; print defined -e _ ? "defined" : "undef"; ' undef defined undef
It appears that stating a nonexistant file will clear _ and that a file test on _ will be undefined if _ is cleared. HTH

Replies are listed 'Best First'.
Re^2: Can the special underline filehandle (stat cache) be cleared?
by ammon (Sexton) on Oct 04, 2006 at 03:00 UTC
    Yes, as mentioned in the OP, my current method of clearing _ is to do an lstat() on the empty string. I was hoping there was a "more correct" way to clear the stat cache, because, while some implementations of the lstat() system call do optimize a stat of the empty string to not bother going to the file system, I'm skeptical about that in the general case.

    It is interesting to note that the file tests, such as -e do not exhibit the same behaviour, with respect to warnings, as stat() and lstat():

    $ perl -we'-e _' $ perl -we'stat _' stat() on unopened filehandle _ at -e line 1.

    That's a better solution than what I was using before, but I wonder if, as above, there's a "more correct" way to do it.

    Cheers,

      Note that calling lstat on an empty string on some systems will give you the same results as lstat("."). So this solution isn't portable.1

      - tye        

      1 Luckily, it isn't needed. Unluckily, I didn't notice that you were doing this before I replied with my better (IMO, at least) solution.