X-Git-Url: http://git.rot13.org/?p=webpac2;a=blobdiff_plain;f=lib%2FWebPAC%2FInput%2FCSV.pm;h=9f105794bab924f9d55580064e91434fc6a4c0cb;hp=26dab49fda5d42697187803a4261693093d2d5c6;hb=HEAD;hpb=e60b14a6092e1341c96d6d586b0d6968502b1dae diff --git a/lib/WebPAC/Input/CSV.pm b/lib/WebPAC/Input/CSV.pm index 26dab49..9f10579 100644 --- a/lib/WebPAC/Input/CSV.pm +++ b/lib/WebPAC/Input/CSV.pm @@ -26,6 +26,7 @@ Returns new low-level input API object my $input = new WebPAC::Input::CSV( path => '/path/to/records.csv', + header_first => 1, ); Options: @@ -40,6 +41,8 @@ path to CSV file Default encoding of input file is C +C will use first line as header names. + =cut sub new { @@ -57,6 +60,12 @@ sub new { $self->{size} = 0; + if ( $self->{header_first} ) { + my $line = $csv->getline( $fh ); + $self->{header_names} = $line; + $self->debug( "header_names = ",dump( $self->{header_names} ) ); + } + while ( 1 ) { my $line = $csv->getline( $fh ); last if $csv->eof; @@ -67,6 +76,8 @@ sub new { $rec->{'000'} = [ ++$self->{size} ]; my $col = 'A'; + my $header_pos = 0; + foreach my $cell ( @$line ) { my $str = eval { Encode::decode_utf8( $cell ) }; if ( $@ ) { @@ -81,6 +92,11 @@ sub new { } $rec->{ $col++ } = $str; + + if ( $self->{header_names} ) { + $rec->{ $self->{header_names}->[$header_pos] } = $str; + $header_pos++; + } } push @{ $self->{_rec} }, $rec;