File: perlcode.pm

package info (click to toggle)
libcatmandu-perl 1.2024-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,552 kB
  • sloc: perl: 17,037; makefile: 24; sh: 1
file content (93 lines) | stat: -rw-r--r-- 1,841 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package Catmandu::Fix::perlcode;

use Catmandu::Sane;

our $VERSION = '1.2024';

use Moo;
use namespace::clean;
use Catmandu::Fix::Has;

with 'Catmandu::Fix::Base';

our %CACHE;

has file => (fix_arg => 1);

has code => (
    is      => 'lazy',
    builder => sub {
        my $file = $_[0]->file;
        $CACHE{$file} //= do $_[0]->file;
    }
);

sub emit {
    my ($self, $fixer) = @_;

    my $code   = $fixer->capture($self->code);
    my $var    = $fixer->var;
    my $reject = $fixer->capture({});

    "if (${code}->(${var},${reject}) == ${reject}) {"
        . $fixer->emit_reject . "}";
}

1;

__END__

=pod

=head1 NAME

Catmandu::Fix::perlcode - execute Perl code as fix function

=head1 DESCRIPTION

Use this fix in the L<Catmandu> fix language to make use of a Perl script:

    perlcode(myscript.pl)

The script (here C<myscript.pl>) must return a code reference:

    sub {
        my $data = shift;

        $data->{testing} = 1 ; # modify the item

        return $data;          # and return the data
    }

When not using the fix language this

    my $fixer = Catmandu::Fix->new( fixes => [ do 'myscript.pl' ] );
    $fixer->fix( $item );

is roughly equivalent to:

    my $code = do 'myscript.pl';
    $item = $code->( $item )

All scripts are cached based on their filename, so using this fix multiple
times will only load each given script once.

The code reference gets passed a second value to reject selected items such as
possible with see L<Catmandu::Fix::reject>:

    sub {
        my ($data, $reject) = @_;

        if ($data->{my_field} eq 'OK') {
            return $data;    # return the data and continue processing
        }
        else {
            return $reject;  # return the reject flag to ignore this record
        }
    }

=head1 SEE ALSO

L<Catmandu::Fix::code>, L<Catmandu::Fix::cmd>

=cut