Filename Globbing Operator
You might wonder what happens to a line input operator if you put something fancier inside the angle brackets. What happens is that it mutates into a different operator. If the string inside the angle brackets is anything other than a filehandle name or a scalar variable (even if there are just extra spaces), it is interpreted as a filename pattern to be “globbed”.[56] The pattern is matched against the files in the current directory (or the directory specified as part of the fileglob pattern), and the filenames so matched are returned by the operator. As with line input, names are returned one at a time in scalar context, or all at once in list context. The latter usage is more common; you often see things like:
@files = <*.xml>;
As with other kinds of pseudoliterals, one level of variable
interpolation is done first, but you can’t say <$foo> because that’s an indirect
filehandle as explained earlier. In older versions of Perl,
programmers would insert braces to force interpretation as a
fileglob: <${foo}>. These
days, it’s considered cleaner to call the internal function directly
as glob($foo), which is probably
the right way to have invented it in the first place. So you’d
write:
@files = glob("*.xml");if you despise overloading the angle operator for this. Which you’re allowed to do.
Whether you use the glob
function or the old angle-bracket form, the fileglob operator also
does while magic like the line
input operator, assigning the result to $_. (That ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access