#!/usr/bin/perl # vim: set sw=4 ts=4 si et: use strict; use warnings; use JSON; use Math::Decimal qw(dec_canonise); use Text::CSV_XS qw(csv); sub read_csv { my $fname = shift; open(my $fh, "<:encoding(utf8)", $fname) or die "open $fname: $!"; # skip the BOM my $firstline = <$fh>; if ($firstline =~ /^\x{FEFF}/) { seek $fh, 3, 0; } else { seek $fh, 0, 0; } my $aoh = csv({in => $fh, encoding => "utf8", headers => "auto"}); close $fh; return $aoh; } sub make_canonical { my $aoh = shift; my $canonical; my $caoh = []; for my $h (@$aoh) { my $canonical; $canonical->{reference} = $h->{Referenznummer}; $canonical->{date} = $h->{Ereignisdatum}; $canonical->{channel} = $h->{Vertriebskanal}; $canonical->{book} = $h->{Titel}; $canonical->{quantity} = $h->{Menge}; $canonical->{earnings} = dec_canonise($h->{Verdienste}); push @$caoh, $canonical; } return wantarray ? @$caoh : $caoh; } sub print_json { my $canonical = shift; my $json = JSON->new; print $json->pretty->utf8->encode($canonical); } if (0 < scalar @ARGV) { my $aoh = read_csv(shift); my $can = make_canonical($aoh); print_json($can); } else { print STDERR "usage: read_lulu \n"; exit 1; }