| 1 | package App::PerlGlue; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use feature qw(say); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use JSON::PP qw(encode_json decode_json); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Text::ParseWords qw(parse_line); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 8 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 8 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=23 SCAR=39.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 8 | my ($class, @argv) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 13 | 8 | ▶ | my $cmd = shift @argv // 'help'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift @argv // "help"
MC/DC0%0/1
shift @argv // "help"
shift @argv
Truth table50%1/2
A
A= shift @argv
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 8 | ▶ | return _help() if $cmd eq 'help' || $cmd eq '--help' || $cmd eq '-h'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$cmd eq "help" or $cmd eq "--help" or $cmd eq "-h"
$cmd eq "help" or $cmd eq "--help"
MC/DC0%0/3
$cmd eq "help" or $cmd eq "--help" or $cmd eq "-h"
$cmd eq "help"
$cmd eq "--help"
$cmd eq "-h"
Truth table25%1/4
A or B or C
A= $cmd eq "help"B= $cmd eq "--help"C= $cmd eq "-h"
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 8 | ▶ | if ($cmd eq 'version' || $cmd eq '--version' || $cmd eq '-v') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$cmd eq "version" or $cmd eq "--version" or $cmd eq "-v"
$cmd eq "version" or $cmd eq "--version"
MC/DC33%1/3
$cmd eq "version" or $cmd eq "--version" or $cmd eq "-v"
$cmd eq "version"
$cmd eq "--version"
$cmd eq "-v"
Truth table50%2/4
A or B or C
A= $cmd eq "version"B= $cmd eq "--version"C= $cmd eq "-v"
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | say "perlglue $VERSION"; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 6 | ▶ | return _cmd_command_help($cmd) if @argv && ($argv[0] eq '--help' || $argv[0] eq '-h'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
@argv and $argv[0] eq "--help" || $argv[0] eq "-h"
$argv[0] eq "--help" or $argv[0] eq "-h"
MC/DC66%2/3
@argv and $argv[0] eq "--help" || $argv[0] eq "-h"
@argv
$argv[0] eq "--help"
$argv[0] eq "-h"
Truth table75%3/4
A and B or C
A= @argvB= $argv[0] eq "--help"C= $argv[0] eq "-h"
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 5 | ▶ | return _cmd_upper() if $cmd eq 'upper'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 4 | ▶ | return _cmd_lower() if $cmd eq 'lower'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 3 | ▶ | return _cmd_lines(@argv) if $cmd eq 'lines'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 2 | ▶ | return _cmd_lines(@argv) if $cmd eq 'where'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | ▶ | return _cmd_replace(@argv) if $cmd eq 'replace'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 2 | ▶ | return _cmd_pick(@argv) if $cmd eq 'pick'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | ▶ | return _cmd_convert(@argv) if $cmd eq 'convert' || $cmd eq 'csv' || $cmd eq 'from-csv'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$cmd eq "convert" or $cmd eq "csv" or $cmd eq "from-csv"
$cmd eq "convert" or $cmd eq "csv"
MC/DC0%0/3
$cmd eq "convert" or $cmd eq "csv" or $cmd eq "from-csv"
$cmd eq "convert"
$cmd eq "csv"
$cmd eq "from-csv"
Truth table25%1/4
A or B or C
A= $cmd eq "convert"B= $cmd eq "csv"C= $cmd eq "from-csv"
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | return _cmd_jsonl(@argv) if $cmd eq 'jsonl'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | return _cmd_template(@argv) if $cmd eq 'template'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | return _cmd_rename(@argv) if $cmd eq 'rename'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | warn "Unknown command: $cmd\n\n"; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | _help(); | |||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | return 2; | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | ▶ | sub _cmd_upper { while (my $line = <STDIN>) { print uc $line } return 0 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cmd_upper: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | ▶ | sub _cmd_lower { while (my $line = <STDIN>) { print lc $line } return 0 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cmd_lower: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 3 | ▶ | sub _open_in { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_open_in: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 3 | my ($file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 3 | ▶ | return *STDIN unless defined $file && length $file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $file and length $file
MC/DC50%1/2
defined $file and length $file
defined $file
length $file
Truth table66%2/3
A and B
A= defined $fileB= length $file
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 2 | ▶ | open my $fh, '<', $file or die "Cannot open $file: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | return $fh; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | ▶ | sub _cmd_lines { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cmd_lines: called CC=8 SCAR=22.2
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | my (@argv) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | my ($file, $expr); | |||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | while (@argv) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | my $arg = shift @argv; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | ▶ | if ($arg eq '--where') { $expr = shift @argv; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | elsif (!defined $file) { $file = $arg; } | |||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | elsif (!defined $expr) { $expr = $arg; } | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | my $fh = _open_in($file); | |||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | local $_ = $line; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | ▶ | if (defined $expr) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 2 | my $ok = eval $expr; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 2 | ▶ | next unless $ok; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | print $line; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub _cmd_replace { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cmd_replace: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | my (@argv) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | my $expr = shift @argv // die "replace requires perl substitution expression\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift @argv // die("replace requires perl substitution expression\n")
MC/DC0%0/1
shift @argv // die("replace requires perl substitution expression\n")
shift @argv
Truth table0%0/2
A
A= shift @argv
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my $fh = _open_in(shift @argv); | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | local $_ = $line; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | eval $expr; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | print $_; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | ▶ | sub _parse_csv_rows { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_csv_rows: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 2 | my ($fh) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 2 | my @rows; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 2 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 6 | chomp $line; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 6 | ▶ | next if $line eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 6 | push @rows, [ parse_line(',', 1, $line) ]; | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 2 | return @rows; | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | ▶ | sub _cmd_pick { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cmd_pick: called CC=8 SCAR=21.6
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | my (@argv) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | ▶ | my $file = shift @argv // die "pick requires file\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift @argv // die("pick requires file\n")
MC/DC0%0/1
shift @argv // die("pick requires file\n")
shift @argv
Truth table50%1/2
A
A= shift @argv
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | my $flag = shift @argv // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift @argv // ""
MC/DC0%0/1
shift @argv // ""
shift @argv
Truth table50%1/2
A
A= shift @argv
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 1 | ▶ | die "pick expects --csv name,email\n" unless $flag eq '--csv'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | ▶ | my @wanted = split /,/, (shift(@argv) // ''); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift @argv // ""
MC/DC0%0/1
shift @argv // ""
shift @argv
Truth table50%1/2
A
A= shift @argv
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 1 | my $fh = _open_in($file); | |||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | my @rows = _parse_csv_rows($fh); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 1 | ▶ | my $header = shift @rows // []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift @rows // []
MC/DC0%0/1
shift @rows // []
shift @rows
Truth table50%1/2
A
A= shift @rows
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 1 | my %idx; @idx{@$header} = (0 .. $#$header); | |||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 1 | say join ',', @wanted; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 1 | for my $r (@rows) { | |||||||||||||||||||||||||||||||||||||||||
| 108 | 2 | ▶ | my @out = map { defined $idx{$_} ? $r->[ $idx{$_} ] : '' } @wanted; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 2 | say join ',', @out; | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 1 | ▶ | sub _cmd_convert { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cmd_convert: called CC=8 SCAR=21.6
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 1 | my (@argv) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 1 | ▶ | my $file = shift @argv // die "convert requires file\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift @argv // die("convert requires file\n")
MC/DC0%0/1
shift @argv // die("convert requires file\n")
shift @argv
Truth table50%1/2
A
A= shift @argv
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 1 | my $to; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 1 | ▶ | while (@argv) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | my $a = shift @argv; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 1 | ▶ | $to = shift @argv if $a eq '--to'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 1 | ▶ | die "convert only supports --to jsonl\n" unless defined $to && $to eq 'jsonl'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $to and $to eq "jsonl"
MC/DC0%0/2
defined $to and $to eq "jsonl"
defined $to
$to eq "jsonl"
Truth table33%1/3
A and B
A= defined $toB= $to eq "jsonl"
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 1 | my $fh = _open_in($file); | |||||||||||||||||||||||||||||||||||||||||
| 125 | 1 | my @rows = _parse_csv_rows($fh); | |||||||||||||||||||||||||||||||||||||||||
| 126 | 1 | ▶ | my $header = shift @rows // []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift @rows // []
MC/DC0%0/1
shift @rows // []
shift @rows
Truth table50%1/2
A
A= shift @rows
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | for my $r (@rows) { | |||||||||||||||||||||||||||||||||||||||||
| 128 | 2 | my %obj; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 2 | @obj{@$header} = @$r; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 2 | say encode_json(\%obj); | |||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | sub _cmd_jsonl { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cmd_jsonl: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | my (@argv) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | my ($file, $expr, $where); | |||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | while (@argv) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | my $a = shift @argv; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if ($a eq '--where') { $where = shift @argv; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | elsif (!defined $file && $a !~ /^\$_\-/) { $file = $a; } | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
defined $file || $a =~ /^\$_\-/
MC/DC0%0/2
defined $file || $a =~ /^\$_\-/
defined $file
$a =~ /^\$_\-/
Truth table0%0/3
A || B
A= defined $fileB= $a =~ /^\$_\-/
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | elsif (!defined $expr) { $expr = $a; } | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | $expr = $where if defined $where; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | my $fh = _open_in($file); | |||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | chomp $line; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | next if $line eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | local $_ = decode_json($line); | |||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | if (defined $expr) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | my $ok = eval $expr; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | next unless $ok; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | say encode_json($_); | |||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | sub _cmd_template { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cmd_template: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | my (@argv) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | ▶ | my $file = shift @argv // die "template requires file\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift @argv // die("template requires file\n")
MC/DC0%0/1
shift @argv // die("template requires file\n")
shift @argv
Truth table0%0/2
A
A= shift @argv
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | ▶ | my $tpl = shift @argv // die "template requires template string\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift @argv // die("template requires template string\n")
MC/DC0%0/1
shift @argv // die("template requires template string\n")
shift @argv
Truth table0%0/2
A
A= shift @argv
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | my $fh = _open_in($file); | |||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | my @rows = _parse_csv_rows($fh); | |||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | my $header = shift @rows // []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift @rows // []
MC/DC0%0/1
shift @rows // []
shift @rows
Truth table0%0/2
A
A= shift @rows
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | for my $r (@rows) { | |||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | my %obj; | |||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | @obj{@$header} = @$r; | |||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | (my $out = $tpl) =~ s/\{\{\s*(\w+)\s*\}\}/defined $obj{$1} ? $obj{$1} : ''/ge; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | say $out; | |||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | sub _cmd_rename { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cmd_rename: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | my (@argv) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | ▶ | my $expr = shift @argv // die "rename requires substitution expression\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift @argv // die("rename requires substitution expression\n")
MC/DC0%0/1
shift @argv // die("rename requires substitution expression\n")
shift @argv
Truth table0%0/2
A
A= shift @argv
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | for my $old (@argv) { | |||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | (my $new = $old); | |||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | local $_ = $new; | |||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | eval $expr; | |||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | $new = $_; | |||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | ▶ | next if $new eq $old; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | die "Target exists: $new\n" if -e $new; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | rename $old, $new or die "rename $old -> $new failed: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | say "$old -> $new"; | |||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | 1 | ▶ | sub _cmd_command_help { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cmd_command_help: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 1 | my ($cmd) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 194 | 1 | my %usage = ( | |||||||||||||||||||||||||||||||||||||||||
| 195 | upper => 'perlglue upper < input.txt', | ||||||||||||||||||||||||||||||||||||||||||
| 196 | lower => 'perlglue lower < input.txt', | ||||||||||||||||||||||||||||||||||||||||||
| 197 | lines => 'perlglue lines [file] [--where EXPR]', | ||||||||||||||||||||||||||||||||||||||||||
| 198 | where => 'perlglue where [file] [--where EXPR]', | ||||||||||||||||||||||||||||||||||||||||||
| 199 | replace => q{perlglue replace 's/foo/bar/g' [file]}, | ||||||||||||||||||||||||||||||||||||||||||
| 200 | pick => 'perlglue pick users.csv --csv name,email', | ||||||||||||||||||||||||||||||||||||||||||
| 201 | convert => 'perlglue convert users.csv --to jsonl', | ||||||||||||||||||||||||||||||||||||||||||
| 202 | csv => 'perlglue csv users.csv --to jsonl', | ||||||||||||||||||||||||||||||||||||||||||
| 203 | 'from-csv' => 'perlglue from-csv users.csv --to jsonl', | ||||||||||||||||||||||||||||||||||||||||||
| 204 | jsonl => q{perlglue jsonl logs.jsonl '\$_->{status} >= 500'}, | ||||||||||||||||||||||||||||||||||||||||||
| 205 | template => q{perlglue template users.csv 'Hello, {{name}}'}, | ||||||||||||||||||||||||||||||||||||||||||
| 206 | rename => q{perlglue rename 's/\s+/_/g' files...}, | ||||||||||||||||||||||||||||||||||||||||||
| 207 | version => 'perlglue version', | ||||||||||||||||||||||||||||||||||||||||||
| 208 | help => 'perlglue help', | ||||||||||||||||||||||||||||||||||||||||||
| 209 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | 1 | ▶ | if (exists $usage{$cmd}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 1 | say $usage{$cmd}; | |||||||||||||||||||||||||||||||||||||||||
| 213 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | warn "Unknown command: $cmd\n"; | |||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | return 2; | |||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | ▶ | sub _help { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_help: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | print <<'HELP'; | |||||||||||||||||||||||||||||||||||||||||
| 222 | perlglue - glue messy text into useful shapes | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | Usage: | ||||||||||||||||||||||||||||||||||||||||||
| 225 | perlglue help | ||||||||||||||||||||||||||||||||||||||||||
| 226 | perlglue --help | ||||||||||||||||||||||||||||||||||||||||||
| 227 | perlglue version | ||||||||||||||||||||||||||||||||||||||||||
| 228 | perlglue <command> --help | ||||||||||||||||||||||||||||||||||||||||||
| 229 | perlglue upper < input.txt | ||||||||||||||||||||||||||||||||||||||||||
| 230 | perlglue lower < input.txt | ||||||||||||||||||||||||||||||||||||||||||
| 231 | perlglue lines [file] [--where EXPR] | ||||||||||||||||||||||||||||||||||||||||||
| 232 | perlglue replace 's/foo/bar/g' [file] | ||||||||||||||||||||||||||||||||||||||||||
| 233 | perlglue pick users.csv --csv name,email | ||||||||||||||||||||||||||||||||||||||||||
| 234 | perlglue convert users.csv --to jsonl | ||||||||||||||||||||||||||||||||||||||||||
| 235 | perlglue jsonl logs.jsonl '$_->{status} >= 500' | ||||||||||||||||||||||||||||||||||||||||||
| 236 | perlglue template users.csv 'Hello, {{name}}' | ||||||||||||||||||||||||||||||||||||||||||
| 237 | perlglue rename 's/\s+/_/g' files... | ||||||||||||||||||||||||||||||||||||||||||
| 238 | HELP | ||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | 8 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | __END__ | ||||||||||||||||||||||||||||||||||||||||||