PrePAN

Sign in to PrePAN

Data::Record::Serialize encode and write records with record munging

Good

Synopsis

    use Data::Record::Serialize;

    # simple output to json
    $s = Data::Record::Serialize->new( encode => 'json', \%attr );
    $s->send( \%record );

    # cleanup record before sending
    $s = Data::Record::Serialize->new( encode => 'json',
	fields => [ qw( obsid chip_id phi theta ) ],
	format_types => { N => '%0.4f' },
	format_fields => { obsid => '%05d' },
        format => 1,
	rename_fields => { chip_id => 'CHIP' },
	types => { obsid => 'I', chip_id => 'S', phi => 'N', theta => 'N' },
    );
    $s->send( \%record );


    # send to an SQLite database
    $s = Data::Record::Serialize->new(
	encode => 'dbi',
	dsn => [ 'SQLite', [ dbname => $dbname ] ],
	table => 'stuff',
	fields => [ qw( obsid chip_id phi theta ) ],
        format => 1,
	format_types => { N => '%0.4f' },
	format_fields => { obsid => '%05d' },
	rename_fields => { chip_id => 'CHIP' },
	types => { obsid => 'I', chip_id => 'S', phi => 'N', theta => 'N' },
    );
    $s->send( \%record );

Description

I found myself in the situation where I needed to output a stream of records (where a record is a set of fields composed of key, scalar value pairs) in a variety of formats (json, yaml, sqlite db) and needed to programmatically select a subset of the fields, sometimes format the values for human readability, etc.

This module stands out from others (well, I found one, Data::Serializer) in that it

  • allows one to rename fields
  • provides type information to encoders that can use it (such as writing to a database)
  • formats fields based on name or type
  • numify values that have been inadvertently not numified
  • allows encoders to write enclosing metadata (such as header records)

The module is only usable with "flat" data structures (no nested hashes or arrays) which are homogeneous in structure.

Comments

Please sign up to post a review.