Skip to content
Jakob Voss edited this page Sep 26, 2014 · 9 revisions

For an extended introduction into creating Fix packages read the two blog posts at:

Create a Fix (short version)

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() in the fix language:

$ catmandu convert JSON --fix "foo()" < data.json

The following instruction seems to be outdated:

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?
{}
Clone this wiki locally