sub separate_stores
{
my (@li_stores, @ff_stores);
my $stores_file = File::HomeDir->my_home . "/example/data/example.cs
+v";
open my $fh, "<", $stores_file or die "$stores_file: $!";
# So at this point we know that you were able to successfully
# open example.csv, because you tested your open.
my $csv = Text::CSV->new ({
binary => 1,
auto_diag => 1,
});
# Also know that if your parsing of CSV generates an error,
# that error will be spat to the screen for us. Good.
my $count_li = 0;
my $count_ff = 0;
$csv ->getline ($fh);
# We don't know if the preceding line returned 'undef',
# indicating an empty CSV file. We also don't know if
# it really stripped away a header row, or something useful
# instead. We also don't know if there is any more CSV remaining
# after processing that first line.
#################
# Change the preceding line "$csv ->getline ($fh);" to the following,
# for better diagnostics:
#
my $head = $csv->getline($fh) or die "Empty CSV file $!\n";
warn "CSV header line contained (@{$head})\n" if $ENV{MYTEST_DEBUG};
die "Nothing found after CSV header line.\n" if $csv->eof;
#
# Now set an environment variable "MYTEST_DEBUG" true, and re-run your
+ test.
#################
while (my $row = $csv->getline ($fh)) {
# Your 'while' loop will never be entered if you're already at
# the end of file. We don't know if that's an issue, because you
# didn't explicitly test what happened when you stripped the first
# CSV line.
if ($row->[1] eq "li") {
$li_stores[$count_li] = $row->[0];
$count_li ++;
} else {
$ff_stores[$count_ff] = $row->[0];
$count_ff ++;
}
}
close $fh;
return (\@li_stores, \@ff_stores);
}
It's almost certain that if this subroutine is failing in the big script, one of those assertions will also fail in the big script. And that will give you better information on why your subroutine is failing to do what you want.
|