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?
Is there a way to get the current file handle that would be used with the <> operator in perl?
71 Views Asked by Adrian At
2
There are 2 best solutions below
0
On
<> is short for readline( ARGV ).
The file handle used is ARGV.
However, readline has special code to open/reopen ARGV which read doesn't have.
You can, however, achieve a read using readline by manipulating $/.
$ echo abcdef | perl -Mv5.14 -e'local $/ = \2; $_ = <>; say "<<$_>>";'
<<ab>>
$ perl -Mv5.14 -e'local $/ = \2; $_ = <>; say "<<$_>>";' <( echo abcdef )
<<ab>>
ARGVis a filehandle and it can be used withinread.To cite from perlvar:
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):
The
<>will open the given file and read the first line. Thereadthen will read the next 10 bytes from the same file.