Module-Generic
view release on metacpan or search on metacpan
lib/Module/Generic/File.pm view on Meta::CPAN
if( $buffer =~ s/^($re)// )
{
$seek_offset = CORE::length($1);
$enc = $e;
last;
}
}
if( !CORE::length( $buffer ) )
{
return( $self->error( "This file ${file} is an empty CSV file with a BOM nonetheless." ) );
}
if( defined( $enc ) )
{
$enc = '' if( $enc eq 'utf-ebcdic' );
}
$io->seek( $seek_offset, SEEK_SET ) ||
return( $self->error( "Unable to seek in file ${file}: $!" ) );
if( CORE::length( $enc // '' ) )
{
# Mark the file handle with the right perl IO layer.
$io->binmode( ":encoding($enc)" );
}
# Default to $opts->{encoding} if it was provided, and we have no encoding found from the BOM.
elsif( CORE::length( $opts->{binmode} // '' ) )
{
$io->binmode( ":encoding($opts->{binmode})" );
}
eval
{
$first_row = $csv->getline($io);
};
if( $@ )
{
return( $self->error( "Unable to parse CSV header line: $@" ) );
}
elsif( !$first_row )
{
# Make sure error_diag() is called in scalar context.
return( $self->error( "Unable to parse CSV header line in file ${file}: ", scalar( $csv->error_diag ) ) );
}
}
# If we detected the BOM, we will have an $opened (i,e, $io) value set.
if( !$opened )
{
$io = $self->open( '<', $opts ) || return( $self->pass_error );
}
elsif( !$self->can_read )
{
return( $self->error( "CSV file \"${file}\" is opened, but not in read mode. Cannot load data from it." ) );
}
my $cols = $opts->{cols} // $opts->{columns};
lib/Module/Generic/File.pm view on Meta::CPAN
}
}
else
{
push( @$all, $row );
}
}
return(1);
};
# If we have a initial first raw from our detect BOM operation earlier, then we use that line of data now.
$process->( $first_row ) if( $first_row );
# Read each line of CSV data until the end.
my $row;
# while( my $row = $csv->getline( $io ) )
while( $io->opened && ( $row = eval{ $csv->getline( $io ) } ) )
{
$n++;
my $rv = $process->( $row );
if( !$rv )
lib/Module/Generic/File.pm view on Meta::CPAN
my $array_object = $file->load_csv(
cols => [qw( name age locale zipcode )],
) || die( $file->error );
Note that you could also achieve the same result with the C<headers> option.
=item * C<detect_bom>
Optional. Defaults to true.
If set to a true value, this will check the CSV file for a C<BOM> (Byte-order mark), remove it by seeking past it, and use it to determine the necessary underlying encoding of the file to read the rest of the data. The encoding is then applied to the...
Using the C<BOM> detection, it recognises C<bocu-1>, C<gb-18030>, C<utf-ebcdic>, C<utf-1>, C<utf-8>, C<utf-16be>, C<utf-16le>, C<utf-32be>, C<utf-32le>, C<scsu>
=item * C<expects>
Optional. Determines the expected return format.
If not specified, defaults to hash references if C<cols> (or C<columns>) or C<headers> option value provides column names, otherwise array references.
=over 8
=item * C<array>
( run in 0.332 second using v1.01-cache-2.11-cpan-e9daa2b36ef )