The <>
thingies are all iterators. All of these variants have common behaviour:
- Used in list context, all remaining elements are returned.
- Used in scalar context, only the next element is returned.
- Used in scalar context, it returns
undef
once the iterator is exhausted.
These last two properties make it suitable for use as a condition in while
loops.
There are two kinds of iterators that can be used with <>
:
- Filehandles. In this case
<$fh>
is equivalent toreadline $fh
. - Globs, so
<* .*>
is equivalent toglob '* .*'
.
The <>
is parsed as a readline when it contains either nothing, a bareword, or a simple scalar. More complex expression can be embedded like <{ ... }>
.
It is parsed as a glob in all other cases. This can be made explicit by using quotes: <"* .*">
but you should really be explicit and use the glob
function instead.
Some details differ, e.g. where the iterator state is kept:
- When reading from a file handle, the file handle holds that iterator state.
- When using the glob form, each glob expression has its own state.
Another part is if the iterator can restart:
- glob restarts after returning one
undef
. - filehandles can only be restarted by seeking – not all FHs support this operation.
If no file handle is used in <>
, then this defaults to the special ARGV
file handle. The behaviour of <ARGV>
is as follows:
- If
@ARGV
is empty, thenARGV
isSTDIN
. Otherwise, the elements of
@ARGV
are treated as file names. The following pseudocode is executed:$ARGV = shift @ARGV; open ARGV, $ARGV or die ...; # careful! no open mode is used
The
$ARGV
scalar holds the filename, and theARGV
file handle holds that file handle.- When
ARGV
would beeof
, the next file from@ARGV
is opened. - Only when
@ARGV
is completely empty can<>
returnundef
.
This can actually be used as a trick to read from many files:
local @ARGV = qw(foo.txt bar.txt baz.txt);
while (<>) {
...;
}