perlfilehandle

Is there a way to get the current file handle that would be used with the <> operator in perl?


I've seen that close ARGV can close the currently processed file, but it would seem that ARGV isn't actually a file handle, so I can't use it in a read call. Is there any way to get the current file handle, or am I going to have to explicitly open the files myself?


Solution

  • ... but it would seem that ARGV isn't actually a file handle, so I can't use it in a read call

    ARGV is a filehandle and it can be used within read.

    To cite from perlvar:

    ... a plain filehandle corresponding to the last file opened by <>"*

    So it is a filehandle and it can be used within read. But you need to have to use <> first so that the file gets actually opened. And it will not magically continue with the next file as <> would do.

    To test simply do (UNIX shell syntax, you might need to adapt this for Windows):

    perl -e '<>; read(ARGV, my $buf, 10); print $buf' file
    

    The <> will open the given file and read the first line. The read then will read the next 10 bytes from the same file.