From 832c828a90de79439022061e36c94a8dc7319498 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Tue, 27 Jun 2023 11:29:28 +0200 Subject: [PATCH] parse .log files delimited by space --- lib/MojoFacets/Import/Log.pm | 47 ++++++++++++++++++++++++++++++++++++ t/MojoFacets-Import-Log.t | 19 +++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 lib/MojoFacets/Import/Log.pm create mode 100755 t/MojoFacets-Import-Log.t diff --git a/lib/MojoFacets/Import/Log.pm b/lib/MojoFacets/Import/Log.pm new file mode 100644 index 0000000..49be8fd --- /dev/null +++ b/lib/MojoFacets/Import/Log.pm @@ -0,0 +1,47 @@ +package MojoFacets::Import::Log; + +use warnings; +use strict; + +use base 'Mojo::Base'; + +use Data::Dump qw(dump); + +__PACKAGE__->attr('full_path'); + +sub ext { '.log' } + +sub data { + my $self = shift; + + my $path = $self->full_path; + + my $data = { items => [] }; + my $need_header = 1; + + open(my $fh, $path) || die "$path: $!"; + while(<$fh>) { + chomp; + warn "## $_\n"; + my @header; + + chomp; + my @v = split(/\s/, $_); + + my $item; + foreach my $i ( 0 .. $#v ) { + $item->{ $header[$i] || "f_$i" } = [ $v[$i] ]; + } + push @{ $data->{items} }, $item; + + if ( $need_header ) { + $data->{header} = [ @header ]; + $need_header = 0; + } + } + + return $data; + +} + +1 diff --git a/t/MojoFacets-Import-Log.t b/t/MojoFacets-Import-Log.t new file mode 100755 index 0000000..4950336 --- /dev/null +++ b/t/MojoFacets-Import-Log.t @@ -0,0 +1,19 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use Test::More tests => 3; +use Data::Dump qw(dump); + +use lib 'lib'; + +use_ok('MojoFacets::Import::Log'); + +my $path = $ARGV[0] || (glob 'data/log/*.log')[0]; +diag "using $path"; + +ok( my $o = MojoFacets::Import::Log->new( full_path => $path ), 'new' ); + +ok( my $data = $o->data, 'data' ); +diag dump($data); -- 2.20.1