API-Eulerian
view release on metacpan or search on metacpan
lib/API/Eulerian/EDW/Parser/CSV.pm view on Meta::CPAN
# @return API::Eulerian::EDW::Json Parser.
#
sub new
{
my ( $class, $path, $uuid ) = @_;
my $self;
my $file;
my $fd;
if( open( $file, '<:encoding(utf8)', $path ) ) {
$self = $class->SUPER::new( $path, $uuid );
$self->{ _FILE } = $file;
$self->{ _PARSER } = Text::CSV->new( {
binary => 1,
auto_diag => 1,
sep_char => ',',
} );
}
return $self;
}
#
# @brief
#
# @param $self
#
# @return
#
sub file
{
return shift->{ _FILE };
}
#
# @brief
#
# @param $self
#
# @return
#
sub parser
{
return shift->{ _PARSER };
}
#
# @brief
#
# @param $self - API::Eulerian::EDW::Parser
#
sub do
{
my ( $self, $hooks ) = @_;
my $parser = $self->parser();
my $file = $self->file();
my $uuid = $self->uuid();
my @headers = ();
my $start = 0;
my $end = 0;
my @rows;
my $line;
# in case of Noop - do not do any treatment on returned data - exit
if ( $hooks eq 'API::Eulerian::EDW::Hook::Noop' ) {
return;
}
# Process Headers line
$line = <$file>; chomp $line;
if( $parser->parse( $line ) ) {
foreach my $field ( $parser->fields() ) {
push @headers, [ 'UNKNOWN', $field ];
}
$hooks->on_headers( $uuid, $start, $end, \@headers );
}
# Process Next lines
while( my $line = <$file> ) {
chomp $line;
if( $parser->parse( $line ) ) {
@rows = [ $parser->fields() ];
$hooks->on_add( $uuid, \@rows );
}
}
$hooks->on_status( $uuid, '', 0, 'Success', 0 );
}
#
# Endup module properly
#
1;
( run in 2.367 seconds using v1.01-cache-2.11-cpan-98e64b0badf )