[librecat-dev] custom fix function with emit

Klee, Carsten Carsten.Klee at sbb.spk-berlin.de
Thu Jun 1 07:42:14 CEST 2017

Hi Catmanduers!

I'm trying to create a custom fix. Let's call it my_add for a start.

The first thing I want to do is simply get a value ('bar') from a document:

{"foo": ["bar"]}

This is my fix:

package Catmandu::Fix::my_add;

use Catmandu::Sane;
use Moo;

use Catmandu::Fix::Has;

has value     => ( fix_arg => 1 );

sub emit {
   my ( $self, $fixer ) = @_;
    my $add_value      = $fixer->generate_var;
    my $perl                   = $fixer->emit_declare_vars( $add_value );

    my $path = $fixer->split_path( $self->value );
    my $key   = pop @$path;

    $fixer->emit_walk_path($fixer->var, $path, sub {
        my $var = shift;

        $fixer->emit_get_key($var, $key, sub {
            my $var = shift;

            $perl      .= "if (is_string(${var})) {" .
                "${add_value} = ${var};" .



Now fixing:

echo '{"foo": ["bar"]}'|catmandu -I lib  convert JSON --fix "my_add(foo.0)"

But this ends with an error:

Oops! Global symbol "$__6" requires explicit package name at (eval 151) line 1.

DataDumper prints the Perl code as:

"my $__5;if (is_string($__6->{"0"})) {$__5 = $__6->{"0"};}if (is_string($__6->[0])) {$__5 = $__6->[0];}"

Which looks fine for me.

I also tried something different:

echo '{"foo": ["bar"]}'|catmandu -I lib  convert JSON --fix "my_add(foo.$first)"

Now there is no error, but $key is now 'foo' and @$path is an empty list. It looks like '$first' vanishes on the way to the fix.

Has anybody an idea what I'm doing wrong?


Carsten Klee
Zeitschriftendatenbank (ZDB)
Staatsbibliothek zu Berlin - Preußischer Kulturbesitz

Potsdamer Straße 33
10785 Berlin

Tel.:   +49 30 266-43 44 02
Fax:    +49 30 266-33 44 01
carsten.klee at sbb.spk-berlin.de

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.uni-bielefeld.de/mailman2/unibi/public/librecat-dev/attachments/20170601/5d7d7205/attachment.html>

More information about the librecat-dev mailing list