[librecat-dev] custom fix function with emit

Klee, Carsten Carsten.Klee at sbb.spk-berlin.de
Fri Jun 2 07:19:08 CEST 2017


Thank you Nicolas and Nicolas for your help. It works now!

Anyhow I would suggest to write a little more documentation on the emit_* functions. Simple code examples would also be helpful. I’d really like to help. But honestly these emit_* functions are still some kind of black box for me.

Cheers!

Carsten

Von: Nicolas Steenlant [mailto:Nicolas.Steenlant at UGent.be]
Gesendet: Donnerstag, 1. Juni 2017 12:01
An: Klee, Carsten
Cc: librecat-dev at lists.uni-bielefeld.de
Betreff: Re: [librecat-dev] custom fix function with emit

Hi Carsten,

you weren’t capturing all the emitted perl code.
This should work:

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;

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

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

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

        });
    });

    $perl;
}

Regarding your earlier question about closures vs code generation. It’s indeed no fun and more error prone to write these emitting fixes. Early versions of Catmandu used closures, but we found out after benchmarking that the method calling overhead became substantial in large fixe files. We're now looking into middle of the road solutions, like generating efficient helper code or analyzing and optimizing your perl code, that would avoid writing emitting code for fix authors.

Regards,
Nicolas

--
Nicolas Steenlant
software developer at University Library Ghent

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:

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

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


More information about the librecat-dev mailing list