-
Notifications
You must be signed in to change notification settings - Fork 31
Exporters
Jakob Voss edited this page Sep 26, 2014
·
9 revisions
Exporters are Catmandu packages to export data from an application. See Importers for the opposite action.
As input exporters can 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 qw(importer exporter);
my $exporter = exporter('YAML');
$exporter = add_many( importer('Mock', 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 qw(exporter);
my $data = [
{ name => { first => 'James' , last => 'Bond' } , occupation => 'Secret Agent' } ,
{ name => { first => 'Ernst' , last => 'Blofeld' } , occupation => 'Supervillain' } ,
];
exporter('Template', template => '/home/phochste/example.tt')->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>