It's hard to see the need for a module to do that. Especially as the '----' line is very specific to your data format; thus would need a special case or option.
And because it is very simple to do:
#! perl -slw
use strict;
use Data::Dump qw[ pp ];
my @keys = split ' ', scalar <DATA>;
<DATA>; ## discard -----
my @data = map{
my %hash;
@hash{ @keys } = split ' ';
\%hash;
} <DATA>;
pp \@data;
__END__
Name UPP Age Career Terms
-------- ------ --- ---------- -----
Rejnaldi 765987 38 Citizen 6
Lisandra 6779AA 34 Noble 4
Kuran 899786 42 Marine 8
Produces: C:\test>junk
[
{ Age => 38, Career => "Citizen", Name => "Rejnaldi", Terms => 6, UP
+P => 765987 },
{ Age => 34, Career => "Noble", Name => "Lisandra", Terms => 4, UPP
+=> "6779AA" },
{ Age => 42, Career => "Marine", Name => "Kuran", Terms => 8, UPP =>
+ 899786 },
]
Of course, someone will complain that it doesn't handle names with spaces, and so you need to switch to fixed field record processing: #! perl -slw
use strict;
use Data::Dump qw[ pp ];
my @keys = unpack 'A8xA6xA3xA10xA5', scalar <DATA>;
<DATA>; ## discard
my @data = map{
my %hash;
@hash{ @keys } = unpack 'A8xA6xA3xA10xA5', $_;
\%hash;
} <DATA>;
pp \@data;
__END__
Name UPP Age Career Terms
-------- ------ --- ---------- -----
Rejnaldi 765987 38 Citizen 6
Lisandra 6779AA 34 Noble 4
Kuran 899786 42 Marine 8
Which produces the same output. But ... they'll say: what if you want to read lots of different files in the same format?
Then you need to determine the fields sizes from the data: #! perl -slw
use strict;
use Data::Dump qw[ pp ];
my @keys = scalar( <DATA> ) =~ m[(\S+\s*)\s]g;
my $templ = join 'x', map{ 'A' . length() } @keys;
@keys = map{ $_ =~ s[\s+$][]; $_ } @keys;
<DATA>; ## discard
my @data = map{
my %hash;
@hash{ @keys } = unpack $templ, $_;
\%hash;
} <DATA>;
pp \@data;
__END__
Again, same output.
But what if the keys can contain spaces? In which case you'll need to use a heuristic approach to locating the field boundaries
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
|