Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

while(<@array>) effects for perl

while(<@thisArray>)

Does anyone know what this would do exactly? We were just having a discussion on it as it's the code is usually something like:

while(<STDIN>)
like image 575
Psyllex Avatar asked Aug 06 '12 18:08

Psyllex


3 Answers

It'll iterate through files names matched to patterns in @thisArray.

Result of perl -MO=Deparse -e '1 while(<@thisArray>)' shows that <> is converted to glob:

use File::Glob ();
'???' while defined($_ = glob(join($", @thisArray)));

From glob manual:

In scalar context, glob iterates through such filename expansions, returning undef when the list is exhausted.

Default value for $" is space, therefore multiple patterns from @thisArray will be joined into single string and then splitted back by space inside glob:

Note that glob splits its arguments on whitespace and treats each segment as separate pattern.

like image 196
Ivan Nevostruev Avatar answered Nov 05 '22 07:11

Ivan Nevostruev


<@thisArray> works as glob(@thisArray). So it gives a list of all files matching the members of the array.

If an element of the array doesn't match a file, the element itself is returned.

like image 6
pavel Avatar answered Nov 05 '22 08:11

pavel


See perlop:

If what's within the angle brackets is neither a filehandle nor a simple scalar variable containing a filehandle name, typeglob, or typeglob reference, it is interpreted as a filename pattern to be globbed

like image 5
choroba Avatar answered Nov 05 '22 07:11

choroba