-
Notifications
You must be signed in to change notification settings - Fork 35
Binds
Patrick Hochstenbach edited this page May 16, 2014
·
25 revisions
Bind function are a new addition to Catmandu version 0.91 [?]. They are used to provide processing hooks around Fix functions.
As a reminder, Fix functions are used to manipulate data. Perl hashes get in and Perl hashes get out. The 'meow' fix below will add a field to an incoming Perl hash:
package Catmandu::Fix::meow;
use Moo;
sub fix {
my ($self,$data) = @_;
$data->{'meow'} = 'Prrr';
$data;
}
1;
Using the Fix you can do something like:
$ echo "{}" | catmandu convert --fix 'meow();'
$ { "meow": "Prrr" }
Binds can be used to manipulate Fix functions. A Perl hash, a code reference (representing the Fix function) and a Fix function name get in, a manipulated Perl hash gets out. As an easy example, let's create a 'bark' Bind which barks every time a 'meow' Fix gets executed.
package Catmandu::Fix::Bind::bark;
use Moo;
with 'Catmandu::Fix::Bind';
sub bind {
my ($self,$data,$coderef,$fix_name) = @_;
if ($fix_name eq 'meow') {
warn "woof! woof!";
}
# execute the fix
return $coderef->($data);
}
1;
We can execute this 'bark' bind by using a new 'do' keyword in the Fix language:
$ echo "{}" | catmandu convert --fix 'do bark(); meow(); end'
woof! woof!
{ "meow": "Prrr" }
$ echo "{}" | catmandu convert --fix 'do bark(); meow(); meow(); meow(); end'
woof! woof!
woof! woof!
woof! woof!
{ "meow": "Prrr" }