App::Hachero::Plugin::Output::CSV - Outputs result to STDOUT as CSV


App-Hachero documentation Contained in the App-Hachero distribution.

Index


Code Index:

NAME

Top

App::Hachero::Plugin::Output::CSV - Outputs result to STDOUT as CSV

SYNOPSYS

Top

  ---
  plugins:
    - module: Output::CSV

DESCRIPTION

Top

outpus result to STDOUT as CSV

implemented hooks

* output

AUTHOR

Top

Nobuo Danjou <nobuo.danjou@gmail.com>

SEE ALSO

Top

App::Hachero


App-Hachero documentation Contained in the App-Hachero distribution.

package App::Hachero::Plugin::Output::CSV;
use strict;
use warnings;
use base qw(App::Hachero::Plugin::Base);
use Text::CSV_XS;

sub output : Hook {
    my ($self, $context, $args) = @_;
    my $fh = \*STDOUT;
    my $csv = Text::CSV_XS->new($self->config->{config}->{csv} || {binary => 1, eol => "\n"});
    for my $output (@{$self->config->{config}->{output}}) {
        my ($key, $fields);
        if (ref $output eq 'HASH') {
            ($key, $fields) = %$output;
        } else {
            $key = $output;
        }
        my $result = $context->result->{$key};

        for my $data ($result->values) {
            my @cols;
            for my $key ($fields ? @$fields : $data->keys) {
                push @cols, $data->value($key);
            }
            $csv->print($fh, \@cols);
        }
    }
}

1;
__END__