I will only comment on the flaws in script 1, as that deals with Text::CSV
- my @names = $csv->getline ($io); assigns an array ref to a list. That's not what you want.
my @names = @{$csv->getline ($io)}; is the coreect mantra.
- always_quote is void on parsing. That is a generator option.
- make it a habit to always pass binary => 1
- Use auto_diag => 1 on csv initiation or $csv->oef or $csv->error_diag () after parsing
use strict;
use warnings;
use Text::CSV;
use Data::Dumper;
open my $io, "<", "player_characters.csv" or die "player_characters.cs
+v: $!";
my $csv = Text::CSV_XS->new ({
sep_char => "|",
allow_whitespace => 1,
blank_is_undef => 1,
binary => 1,
auto_diag => 1,
});
my @names = @{$csv->getline ($io)};
$csv->column_names (@names);
while (defined (my $hr = $csv->getline_hr ($io))) {
print Dumper ($hr);
}
If you do not use the @names after you set it, other than to assign to to column names, you can combine the in one call, as column_names () accepts both a list or a list ref
$csv->column_names ($csv->getline ($io));
Enjoy, Have FUN! H.Merijn