| 1 | #!/usr/bin/env perl | ||||||||||
| 2 | |||||||||||
| 3 | # Copyright (c) 2015 Christian Jaeger, copying@christianjaeger.ch | ||||||||||
| 4 | # This is free software. See the file COPYING.md that came bundled | ||||||||||
| 5 | # with this file. | ||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | # find modules from functional-perl working directory (not installed) | ||||||||||
| 12 | 1 | ▶ | use Cwd 'abs_path'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | our ($mydir, $myname); | |||||||||
| 14 | |||||||||||
| 15 | 1 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=11.7
| |||||||||||
| 16 | 1 | ▶ | my $location = (-l $0) ? abs_path($0) : $0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | 1 | ▶ | $location =~ /(.*?)([^\/]+?)_?\z/s or die "?"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 1 | ($mydir, $myname) = ($1, $2); | |||||||||
| 19 | } | ||||||||||
| 20 | 1 | ▶ | use lib "$mydir/../lib"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | 0 | ▶ | sub usage { | ||||||||
|
Subroutine
usage: not called CC=2 SCAR=17.9
| |||||||||||
| 23 | 0 | print "usage: $myname [-o xmlfile] csvfile(s) | |||||||||
| 24 | |||||||||||
| 25 | Open and read csvfile containing CSV with 4 columns, and write it to | ||||||||||
| 26 | xmlfile or stdout as XML. Does streaming, i.e. only loads one row | ||||||||||
| 27 | (plus some buffering) into memory at a time. | ||||||||||
| 28 | |||||||||||
| 29 | "; | ||||||||||
| 30 | 0 | ▶ | exit(@_ ? 1 : 0); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 1 | ▶ | use Getopt::Long; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 1 | our $verbose = 0; | |||||||||
| 35 | 1 | our $outpath; | |||||||||
| 36 | GetOptions( | ||||||||||
| 37 | "verbose" => \$verbose, | ||||||||||
| 38 | 0 | ▶ | "help" => sub {usage}, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 39 | 1 | ▶ | "outpath=s" => \$outpath, | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | ) or exit 1; | ||||||||||
| 41 | 1 | ▶ | usage unless @ARGV; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | |||||||||||
| 43 | 1 | ▶ | use FP::Text::CSV qw(csv_file_to_rows); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | |||||||||||
| 45 | use PXML::Tags | ||||||||||
| 46 | |||||||||||
| 47 | # create tag functions for the following XML tag names. Casing is | ||||||||||
| 48 | # preserved for the output, but the tag functions are all-uppercase | ||||||||||
| 49 | # (to try to avoid name conflicts and for better visibility) and | ||||||||||
| 50 | # replace the minus with the underscore. | ||||||||||
| 51 | 1 | ▶ | qw(myexample | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 52 | protocol-version | ||||||||||
| 53 | records | ||||||||||
| 54 | record | ||||||||||
| 55 | a | ||||||||||
| 56 | b | ||||||||||
| 57 | c | ||||||||||
| 58 | 1 | d); | |||||||||
| 59 | |||||||||||
| 60 | 1 | ▶ | use PXML::Serialize qw(pxml_print); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 61 | |||||||||||
| 62 | 1 | ▶ | sub print_csv_as_xml { | ||||||||
|
Subroutine
print_csv_as_xml: called CC=1 SCAR=0.0
| |||||||||||
| 63 | 1 | my ($fh, $path) = @_; | |||||||||
| 64 | |||||||||||
| 65 | 1 | my $rows = csv_file_to_rows $path, +{ eol => "\n", sep_char => ";" }; | |||||||||
| 66 | |||||||||||
| 67 | # $rows is a lazily-computed linked list of the rows | ||||||||||
| 68 | |||||||||||
| 69 | # skip the header row from the CSV file: | ||||||||||
| 70 | 1 | $rows = rest $rows; | |||||||||
| 71 | |||||||||||
| 72 | # create a partially lazily computed PXML data structure | ||||||||||
| 73 | my $xmlstream = MYEXAMPLE( | ||||||||||
| 74 | PROTOCOL_VERSION("0.123"), | ||||||||||
| 75 | RECORDS # here we embed the lazily computed part: | ||||||||||
| 76 | ( | ||||||||||
| 77 | $rows->map ( | ||||||||||
| 78 | 2 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||
| 79 | 2 | my ($row) = @_; | |||||||||
| 80 | 2 | ▶ | @$row == 4 or die "row doesn't contain 4 columns: @$row"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 2 | my ($a, $b, $c, $d) = @$row; | |||||||||
| 82 | |||||||||||
| 83 | 2 | RECORD(A($a), B($b), C($c), D($d)) | |||||||||
| 84 | } | ||||||||||
| 85 | ) | ||||||||||
| 86 | 1 | ) | |||||||||
| 87 | ); | ||||||||||
| 88 | |||||||||||
| 89 | # walk and print the PXML data structure, forcing its evaluation | ||||||||||
| 90 | # as needed | ||||||||||
| 91 | 1 | pxml_print $xmlstream, $fh; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 1 | ▶ | use Chj::xtmpfile; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 95 | 1 | ▶ | use Chj::xopen qw(glob_to_fh); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 96 | |||||||||||
| 97 | 1 | ▶ | my $fh = defined $outpath ? xtmpfile $outpath : glob_to_fh * STDOUT; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 1 | print_csv_as_xml $fh, $_ for @ARGV; | |||||||||
| 99 | 1 | $fh->xclose; | |||||||||
| 100 | 1 | ▶ | $fh->xputback(0666 & ~umask) if defined $outpath; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | |||||||||||