-
Notifications
You must be signed in to change notification settings - Fork 35
Exporters
Exporters are Catmandu packages to export data from an application. As input they can get native Perl hashes or arrays but also Iterators to stream huge data sets.
Here is an example using our Mock importer to stream 1 million Perl hashes through an Exporter:
use Catmandu::Exporter::YAML;
my $exporter = Catmandu::Exporter::YAML->new();
$exporter->add_many(Catmandu::Importer::Mock->new(size => 1000000));
Catmandu provides exporters for BibTeX, CSV, JSON, RIS, XLS and YAML. If you need a special exporter for your own format you could use the Template exporter which uses Template Toolkit.
As an example lets create an exporter for an Perl array of hashes $data using a template:
use Catmandu::Exporter::Template; my $data = [ { name => { first => 'James' , last => 'Bond' } , occupation => 'Secret Agent' } , { name => { first => 'Ernst' , last => 'Blofeld' } , occupation => 'Supervillain' } , ]; my $exporter = Catmandu::Exporter::Template->new(template => '/home/phochste/example.tt'); $exporter->add_many($data);
The template example.tt will be rendered for every hash in the array $data
(or for every item in an Iterable $data
).
<character>
<name>[% name.last %], [% name.first %]</name>
<occupation>[% occupation %]</occupation>
</character>