line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Fix::perlcode; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
574
|
use Catmandu::Sane; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
11
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.2020'; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
11
|
use Moo; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
8
|
1
|
|
|
1
|
|
360
|
use namespace::clean; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
7
|
|
9
|
1
|
|
|
1
|
|
759
|
use Catmandu::Fix::Has; |
|
1
|
|
|
|
|
39
|
|
|
1
|
|
|
|
|
7
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
with 'Catmandu::Fix::Base'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our %CACHE; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has file => (fix_arg => 1); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has code => ( |
18
|
|
|
|
|
|
|
is => 'lazy', |
19
|
|
|
|
|
|
|
builder => sub { |
20
|
3
|
|
|
3
|
|
30
|
my $file = $_[0]->file; |
21
|
3
|
|
66
|
|
|
1055
|
$CACHE{$file} //= do $_[0]->file; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub emit { |
26
|
3
|
|
|
3
|
0
|
7
|
my ($self, $fixer) = @_; |
27
|
|
|
|
|
|
|
|
28
|
3
|
|
|
|
|
61
|
my $code = $fixer->capture($self->code); |
29
|
3
|
|
|
|
|
51
|
my $var = $fixer->var; |
30
|
3
|
|
|
|
|
30
|
my $reject = $fixer->capture({}); |
31
|
|
|
|
|
|
|
|
32
|
3
|
|
|
|
|
16
|
"if (${code}->(${var},${reject}) == ${reject}) {" |
33
|
|
|
|
|
|
|
. $fixer->emit_reject . "}"; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
$data->{testing} = 1 ; # modify the item |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
return $data; # and return the data |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
When not using the fix language this |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
my $fixer = Catmandu::Fix->new( fixes => [ do 'myscript.pl' ] ); |
65
|
|
|
|
|
|
|
$fixer->fix( $item ); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
is roughly equivalent to: |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
my $code = do 'myscript.pl'; |
70
|
|
|
|
|
|
|
$item = $code->( $item ) |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
All scripts are cached based on their filename, so using this fix multiple |
73
|
|
|
|
|
|
|
times will only load each given script once. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
The code reference gets passed a second value to reject selected items such as |
76
|
|
|
|
|
|
|
possible with see L<Catmandu::Fix::reject>: |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub { |
79
|
|
|
|
|
|
|
my ($data, $reject) = @_; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
if ($data->{my_field} eq 'OK') { |
82
|
|
|
|
|
|
|
return $data; # return the data and continue processing |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
else { |
85
|
|
|
|
|
|
|
return $reject; # return the reject flag to ignore this record |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head1 SEE ALSO |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
L<Catmandu::Fix::code>, L<Catmandu::Fix::cmd> |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=cut |