| 1 | =pod | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | ETL::Pipeline::Output::Perl - Execute arbitrary Perl code against every record | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use ETL::Pipeline; | ||
| 10 | ETL::Pipeline->new( { | ||
| 11 | input => ['UnitTest'], | ||
| 12 | mapping => {First => 'Header1', Second => 'Header2'}, | ||
| 13 | output => ['Perl', code => sub { say $_->{First} }] | ||
| 14 | } )->process; | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | B<ETL::Pipeline::Output::Perl> runs arbitrary Perl code for every record. It | ||
| 19 | comes in useful when debugging data issues or prototyping a new technique. | ||
| 20 | |||
| 21 | B<ETL::Pipeline::Output::Perl> stores the record in a hash (see L</current>). | ||
| 22 | The class passes that hash reference into your subroutine. | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | package ETL::Pipeline::Output::Perl; | ||
| 27 | use Moose; | ||
| 28 | |||
| 29 | use 5.14.0; | ||
| 30 | use warnings; | ||
| 31 | |||
| 32 | use Carp; | ||
| 33 | use String::Util qw/hascontent/; | ||
| 34 | |||
| 35 | |||
| 36 | our $VERSION = '2.00'; | ||
| 37 | |||
| 38 | |||
| 39 | =head1 METHODS & ATTRIBUTES | ||
| 40 | |||
| 41 | =head2 Arguments for L<ETL::Pipeline/output> | ||
| 42 | |||
| 43 | =head3 code | ||
| 44 | |||
| 45 | Assign this attribute your code reference. Your code receives two parameters. | ||
| 46 | The first one is the L<ETL::Pipeline> object. The second is the hash reference | ||
| 47 | with the data record. | ||
| 48 | |||
| 49 | The code reference can do anything. It should return a boolean. B<True> means | ||
| 50 | success and B<false> means failure. You determine what I<success> or I<failure> | ||
| 51 | really means. | ||
| 52 | |||
| 53 | B<WARNING:> Do not save the hash reference. Make a copy of the hash instead. | ||
| 54 | B<ETL::Pipeline::Output::Perl> re-uses the same hash reference. The second | ||
| 55 | record will overwrite the first, etc. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | has 'code' => ( | ||
| 60 | is => 'ro', | ||
| 61 | isa => 'CodeRef', | ||
| 62 | required => 1, | ||
| 63 | ); | ||
| 64 | |||
| 65 | |||
| 66 | =head2 Called from L<ETL::Pipeline/process> | ||
| 67 | |||
| 68 | =head3 write_record | ||
| 69 | |||
| 70 | Passes the subroutine in L</code> to L<ETL::Pipeline/execute_code_ref>. | ||
| 71 | L</current> is passed to the subroutine as a parameter. | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | sub write_record { | ||
| 76 | my $self = shift; | ||
| 77 | return $self->pipeline->execute_code_ref( $self->code, $self->current ); | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | =head3 configure | ||
| 82 | |||
| 83 | B<configure> doesn't actually do anything. But it is required by | ||
| 84 | L<ETL::Pipeline/process>. | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | sub configure {} | ||
| 89 | |||
| 90 | |||
| 91 | =head3 finish | ||
| 92 | |||
| 93 | B<finish> doesn't actually do anything. But it is required by | ||
| 94 | L<ETL::Pipeline/process>. | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | sub finish {} | ||
| 99 | |||
| 100 | |||
| 101 | =head2 Other methods and attributes | ||
| 102 | |||
| 103 | =head3 default_fields | ||
| 104 | |||
| 105 | Initialize L</current> for the next record. | ||
| 106 | |||
| 107 | =cut | ||
| 108 | |||
| 109 | sub default_fields { () } | ||
| 110 | |||
| 111 | |||
| 112 | =head1 SEE ALSO | ||
| 113 | |||
| 114 | L<ETL::Pipeline>, L<ETL::Pipeline::Output>, | ||
| 115 | L<ETL::Pipeline::Output::Storage::Hash> | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | with 'ETL::Pipeline::Output::Storage::Hash'; | ||
| 120 | with 'ETL::Pipeline::Output'; | ||
| 121 | |||
| 122 | |||
| 123 | =head1 AUTHOR | ||
| 124 | |||
| 125 | Robert Wohlfarth <robert.j.wohlfarth@vanderbilt.edu> | ||
| 126 | |||
| 127 | =head1 LICENSE | ||
| 128 | |||
| 129 | Copyright 2016 (c) Vanderbilt University | ||
| 130 | |||
| 131 | This program is free software; you can redistribute it and/or modify it under | ||
| 132 | the same terms as Perl itself. | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | no Moose; | ||
| 137 | __PACKAGE__->meta->make_immutable; |