On Jul 3, mark crowe (JIC) said:

>       open FILEHANDLE "file1" + "file2";
>       while (<FILEHANDLE>) {do stuff}

This is specifically what @ARGV and <> are good for. ;)

>Test code that duplicates the problem:
>       @ARGV = qw(test1 test2);
>       for $i(0..9) {$array[$i] = <>}
>       print @array;

Yes, if there are less than 10 lines in the combined count of test1 and
test2, this program *MIGHT* hang.  It won't hang if the number of lines in
the files, combined, is ONE LESS than the number of lines you're looking
for.  That's because after <> has exhausted @ARGV, it returns undef on the
next call for a line.  After that, <> goes back to doing what it always
does -- read from the files in @ARGV, or set @ARGV to "-" and read from
that (STDIN).

So the solution is to use the eof() function in its "no-arguments" form.
(To see why this is important, read 'perldoc -f eof' to find how
eof() checks differently, if it's called as 'eof', 'eof()', or 'eof(FH)'.)

So I would write:

  for (0 .. 9) {
    push @array, scalar <>;
    last if eof();
  }

And it works as expected.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter.         Brother #734
**      Manning Publications, Co, is publishing my Perl Regex book      **

Reply via email to