Subject: | getline() does not return documented value at end of filehandle |
Date: | Sat, 16 Sep 2006 22:19:40 -0400 (EDT) |
To: | bug-Text-CSV_XS [...] rt.cpan.org |
From: | Cat Okita <cat [...] reptiles.org> |
The documentation at http://search.cpan.org/~jwied/Text-CSV_XS-0.23/CSV_XS.pm
states:
getline
$columns = $csv->getline($io);
This is the counterpart to print, like parse is the counterpart
to combine: It reads a row from the IO object $io using $io->getline()
and parses this row into an array ref. This array ref is returned by
the function or undef for failure.
getline() does not return undef at eof - it returns an empty array when
the filehandle hits eol. By perl5 syntax, an empty arrayref is always
true, which is a notably different result from undef.
As a result, the following code will loop endlessly, rather than exiting
at the end of the file:
#!/usr/bin/perl -w
use strict;
use Carp;
use Text::CSV_XS;
open my $fh, "<", $ARGV[0];
my $csv = Text::CSV_XS->new({'binary' => 1});
my $linenumber = 0;
LINE: while (my $row = $csv->getline($fh)) {
my $line = join ', ', @$row;
$linenumber++;
print "$linenumber: $line\n";
}
print "LINES: $linenumber\n";
undef $fh;
==========================================================================
"A cat spends her life conflicted between a deep, passionate and profound
desire for fish and an equally deep, passionate and profound desire to
avoid getting wet. This is the defining metaphor of my life right now."