| 1 | package Catmandu::Fix::cmd; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Catmandu::Sane; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use IO::Pty::Easy; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use JSON::XS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | =head1 NAME | ||||||||||
| 10 | |||||||||||
| 11 | Catmandu::Fix::cmd - pipe data to be fixed through an external process | ||||||||||
| 12 | |||||||||||
| 13 | =head1 VERSION | ||||||||||
| 14 | |||||||||||
| 15 | Version 0.0201 | ||||||||||
| 16 | |||||||||||
| 17 | =cut | ||||||||||
| 18 | |||||||||||
| 19 | 1 | our $VERSION = '0.0201'; | |||||||||
| 20 | |||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||
| 22 | |||||||||||
| 23 | This fix passes data as a JSON object to an external process over stdin and | ||||||||||
| 24 | reads a JSON object from it's stdout. | ||||||||||
| 25 | |||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||
| 27 | |||||||||||
| 28 | my $fixer = Catmandu::Fix->new(fixes => [ | ||||||||||
| 29 | # pipe data through the jq command-line json processor | ||||||||||
| 30 | # keeping only the title field | ||||||||||
| 31 | 'cmd("jq -c -M {title}")', | ||||||||||
| 32 | # ... | ||||||||||
| 33 | ]); | ||||||||||
| 34 | |||||||||||
| 35 | # a canonical external program in perl | ||||||||||
| 36 | use JSON; | ||||||||||
| 37 | while (<STDIN>) { | ||||||||||
| 38 | my $data = decode_json($_); | ||||||||||
| 39 | # ... | ||||||||||
| 40 | print encode_json($data); | ||||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | =cut | ||||||||||
| 44 | |||||||||||
| 45 | 1 | has cmd => (is => 'ro'); | |||||||||
| 46 | 1 | has pty => (is => 'ro', lazy => 1, builder => 1, predicate => 1); | |||||||||
| 47 | 1 | has json => (is => 'ro', lazy => 1, builder => 1); | |||||||||
| 48 | |||||||||||
| 49 | 0 | ▶ | around BUILDARGS => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 50 | 0 | my ($orig, $class, @cmd) = @_; | |||||||||
| 51 | 0 | $orig->($class, | |||||||||
| 52 | cmd => [@cmd], | ||||||||||
| 53 | ); | ||||||||||
| 54 | 1 | }; | |||||||||
| 55 | |||||||||||
| 56 | 0 | ▶ | sub _build_json { | ||||||||
|
Subroutine
_build_json: not called CC=1 SCAR=6.9
| |||||||||||
| 57 | 0 | JSON::XS->new->utf8(1); | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 0 | ▶ | sub _build_pty { | ||||||||
|
Subroutine
_build_pty: not called CC=1 SCAR=6.9
| |||||||||||
| 61 | 0 | my ($self) = @_; | |||||||||
| 62 | 0 | my $cmd = $self->cmd; | |||||||||
| 63 | 0 | my $pty = IO::Pty::Easy->new; | |||||||||
| 64 | 0 | $pty->spawn(@$cmd); | |||||||||
| 65 | 0 | $pty; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | sub fix { | ||||||||
|
Subroutine
fix: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 69 | 0 | my ($self, $data) = @_; | |||||||||
| 70 | 0 | my $json = $self->json; | |||||||||
| 71 | 0 | my $line = $json->encode($data)."\n"; | |||||||||
| 72 | 0 | $self->pty->write($line); | |||||||||
| 73 | 0 | for (;;) { | |||||||||
| 74 | 0 | ▶ | if ($data = $json->incr_parse) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 0 | last; | |||||||||
| 76 | } | ||||||||||
| 77 | 0 | $line = $self->pty->read; | |||||||||
| 78 | 0 | chomp $line; | |||||||||
| 79 | 0 | $json->incr_parse($line); | |||||||||
| 80 | } | ||||||||||
| 81 | 0 | $data; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | =head1 SEE ALSO | ||||||||||
| 85 | |||||||||||
| 86 | L<Catmandu::Fix> | ||||||||||
| 87 | |||||||||||
| 88 | =head1 AUTHOR | ||||||||||
| 89 | |||||||||||
| 90 | Nicolas Steenlant, C<< <nicolas.steenlant at ugent.be> >> | ||||||||||
| 91 | |||||||||||
| 92 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 93 | |||||||||||
| 94 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 95 | under the terms of either: the GNU General Public License as published | ||||||||||
| 96 | by the Free Software Foundation; or the Artistic License. | ||||||||||
| 97 | |||||||||||
| 98 | See http://dev.perl.org/licenses/ for more information. | ||||||||||
| 99 | |||||||||||
| 100 | =cut | ||||||||||
| 101 | |||||||||||
| 102 | 1 | 1; | |||||||||