[librecat-dev] custom fix function with emit

Nicolas Franck Nicolas.Franck at UGent.be
Thu Jun 1 11:42:01 CEST 2017


Every emit function returns perl-code as a string, so also $fixer->emit_walk_path.

"$perl" at the end should be removed, so that the return of "$fixer->emit_walk_path" is used as the return of this fix.
"$perl .= " should be removed

e.g.

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

should be

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



On 1 Jun 2017, at 07:42, Klee, Carsten <Carsten.Klee at sbb.spk-berlin.de<mailto:Carsten.Klee at sbb.spk-berlin.de>> wrote:

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};" .
            "}";

        });
    });
    $perl;
}

1;

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?

Thanks!

Carsten
---
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<mailto:carsten.klee at sbb.spk-berlin.de>
www.staatsbibliothek-berlin.de<http://www.staatsbibliothek-berlin.de/>

_______________________________________________
librecat-dev mailing list
- send list mails to librecat-dev at lists.uni-bielefeld.de<mailto:librecat-dev at lists.uni-bielefeld.de>
- to unsubscribe or change options, visit https://lists.uni-bielefeld.de/mailman2/cgi/unibi/listinfo/librecat-dev
- project website: http://librecat.org/

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


More information about the librecat-dev mailing list