| 1 | package Catmandu::Fix::perlcode; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use Catmandu::Sane; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 1 | our $VERSION = '1.0201_02'; | |||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use namespace::clean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use Catmandu::Fix::Has; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 1 | with 'Catmandu::Fix::Base'; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | our %CACHE; | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 1 | has file => (fix_arg => 1); | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | has code => ( | ||||||||||||||||||||
| 18 | is => 'lazy', | ||||||||||||||||||||
| 19 | builder => sub { | ||||||||||||||||||||
| 20 | 3 | ▶ | my $file = $_[0]->file; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 21 | 3 | ▶ | $CACHE{$file} //= do $_[0]->file; | ||||||||||||||||||
|
Condition66%2/3
$Catmandu::Fix::perlcode::CACHE{$file} //= do $_[0]->file
MC/DC0%0/2
$Catmandu::Fix::perlcode::CACHE{$file} //= do $_[0]->file
$Catmandu::Fix::perlcode::CACHE{$file}
do $_[0]->file
Truth table66%2/3
A //= B
A= $Catmandu::Fix::perlcode::CACHE{$file}B= do $_[0]->file
| |||||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | 1 | ); | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | sub emit { | ||||||||||||||||||||
| 26 | 3 | ▶ | my ($self, $fixer) = @_; | ||||||||||||||||||
|
Subroutine
emit: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 3 | my $code = $fixer->capture($self->code); | |||||||||||||||||||
| 29 | 3 | my $var = $fixer->var; | |||||||||||||||||||
| 30 | 3 | my $reject = $fixer->capture({}); | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 3 | "if (${code}->(${var},${reject}) == ${reject}) {" | |||||||||||||||||||
| 33 | . $fixer->emit_reject . "}"; | ||||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 1 | 1; | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | __END__ | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | =pod | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | =head1 NAME | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | Catmandu::Fix::perlcode - execute Perl code as fix function | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | =head1 DESCRIPTION | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | Use this fix in the L<Catmandu> fix language to make use of a Perl script: | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | perlcode(myscript.pl) | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | The script (here C<myscript.pl>) must return a code reference: | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | sub { | ||||||||||||||||||||
| 55 | my $data = shift; | ||||||||||||||||||||
| 56 | ... | ||||||||||||||||||||
| 57 | return $data; | ||||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | When not using the fix language this | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | my $fixer = Catmandu::Fix->new( fixes => [ do 'myscript.pl' ] ); | ||||||||||||||||||||
| 63 | $fixer->fix( $item ); | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | is roughly equivalent to: | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | my $code = do 'myscript.pl'; | ||||||||||||||||||||
| 68 | $item = $code->( $item ) | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | All scripts are cached based on their filename, so using this fix multiple | ||||||||||||||||||||
| 71 | times will only load each given script once. | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | The code reference gets passed a second value to reject selected items such as | ||||||||||||||||||||
| 74 | possible with see L<Catmandu::Fix::reject>: | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | sub { | ||||||||||||||||||||
| 77 | my ($data, $reject) = @_; | ||||||||||||||||||||
| 78 | return rejection_criteria($data) ? $reject : $data; | ||||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | To indicate the end processing, return C<undef>. | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | =head1 SEE ALSO | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | L<Catmandu::Fix::code>, L<Catmandu::Fix::cmd> | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | =cut | ||||||||||||||||||||