NAME

Catmandu::Importer - Namespace for packages that can import

SYNOPSIS

use Catmandu::Importer::JSON;

my $importer = Catmandu::Importer::JSON->new(file => "/foo/bar.json");

my $n = $importer->each(sub {
    my $hashref = $_[0];
    # ...
});

DESCRIPTION

A Catmandu::Importer is a stub for Perl packages that can import data from an external source (a file, the network, ...).

Every Catmandu::Importer is a Catmandu::Fixable and thus provides a 'fix' parameter that can be set in the constructor. For every item returned by the generator the given fixes will be applied first.

METHODS

new(file => $file , encoding => $encoding )

Create a new importer reading input from a local file: $file is a string containing the path to the file.

new(fh => $fh , encoding => $encoding)

Create a new importer by reading from a IO::Handle. Optionally use Catmandu::Util::io to create IO::Handles.

count

each(&callback)

...

Every Catmandu::Importer is a Catmandu::Iterable all its methods are inherited.

log

Return the current logger. Can be used when creating your own Importers.

E.g.

package Catmandu::Importer::Mock;

use namespace::clean;
use Catmandu::Sane;
use Moo;

with 'Catmandu::Importer';

has size => (is => 'ro');

sub generator {
    my ($self) = @_;
    my $n = 0;
    sub {
        $self->log->debug("generating record $n");
        return if defined $self->size && $n == $self->size;
        return { n => $n++ };
    };
}

See also: Catmandu for activating the logger in your main code.

SEE ALSO

Catmandu::Iterable , Catmandu::Util