-
Notifications
You must be signed in to change notification settings - Fork 31
Fix packages
Patrick Hochstenbach edited this page Jun 3, 2014
·
9 revisions
For an extended introduction into creating Fix packages read the two blog posts at:
The easiest way to create a new 'Fix' is by creating a Perl package in the Catmandu::Fix namespace that has a 'fix' instance method. For example:
package Catmandu::Fix::foo;
use Moo;
sub fix {
my ($self,$data) = @_;
# Fix your data here...
$data;
}
1;
When this code is installed in your PERL5LIB path as Catmandu/Fix/foo.pm then you fix scripts can execute this fix by running:
foo()
Or, from the command line:
$ catmandu convert JSON --fix "foo()" < data.json
If you want pass arguments to your fix, you can make use of Moo and Catmandu::Fix::Has to read in required and optional parameters.
package Catmandu::Fix::foo;
use Moo;
has greeting => (fix_arg => 1); # required first argument
has message => (fix_arg => 1); # required second argument
has eol => (fix_opt => '!'); # optional argument , default '!'
sub fix {
my ($self,$data) = @_;
print STDERR $self->greeting . ", " . $self->message . $self->eol. "\n"
# Fix your data here...
$data;
}
1;
Now you can write log messages in your Fixes:
$ echo '{}' | catmandu convert --fix 'foo(Hello,World)'
Hello, World!
{}
$ echo '{}' | catmandu convert --fix 'foo(Hello,World, eol: ?)'
Hello, World?
{}