Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi, you didn't supply sample input or output, or describe what the external program does, so the following is untested, but here's something to try using MCE.

#!/usr/bin/env perl -w use strict; use warnings; use MCE::Loop; use Capture::Tiny 'capture_stdout'; use Text::CSV 'csv'; use Try::Tiny; use JSON; MCE::Loop::init { max_workers => 8, use_slurpio => 1 }; my $data_filename = 'all.txt'; my $fail_filename = 'failed.tsv'; my $cmd = 'cfn_nag_scan'; my @args = qw/ -o json --input-path /; my @results = mce_loop_f { my ($mce, $slurp_ref, $chunk_id) = @_; my @failures; while ( $$slurp_ref =~ /([^\n]+\n)/mg ) { my $line = $1; my $json = try { return decode_json(capture_stdout { system($cmd, @args, $ +line) }); } catch { warn "processing $line failed: $_"; return; }; next unless $json; my $failure_count = $json->{file_results}{failure_count}; push @failures, [ $json->{filename} => $failure_count ] if $fa +ilure_count; } # Gather results MCE->gather(@failures); } $data_filename; unshift @results, [qw/ Filename Failures /]; csv (in => \@results, out => $fail_filename, sep_char=> "\t"); __END__

update: added json decoding and exception handling; changed return to next (thx marioroy)

Hope this helps!


The way forward always starts with a minimal test.



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-26 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found