I believe your error is rooted in not really understanding the meaning of
my
. You are using it all over the place, but when you do that you are creating a new variable in the enclosing block. You should really go back and check
every instance of
my
to see if that is what you really want to do.
Specifically in (but not limited to) the line:
$csv->print (my $fh, [my $name, my $location, my $comment]);
You are:
- creating a new variable
$fh
(masking the $fh
from your open
) where print
is expecting you to give it an IO handle - creating three new variables
$name, $location, $comment
where print
is expecting to get an arrayref
and note that none of these new variables have values, so no wonder nothing is being printed. The only reason your
close
is not giving you a warning is that you mistyped the
$fh
as
$fhl
. Just fix that and you should see the warning
"my" variable $fh masks earlier declaration in same scope
.
The CSV section would be better as something like this (untested):
my @names = qw(name location comment)
$csv->column_names(@names);
open my $fh, ">", "punter.csv" or die "new.csv $!";
while ( my $l = $csv->getline_hr($fh)) {
next if ($l->{'name'} =~ /xxx/);
for(@names) { print "$_: ",$l->{$_} }
$csv->print ($fh, $l);
}
close $fh or die "$!";
This takes advantage of the column naming feature also, and should cope with any number of fields. By the way, I'd suggest never using
$l
as a variable name in Perl as it looks too much like
$1
in many fonts, which of course has a special regex meaning.