NAME

Hash::Convert - Rule based Hash converter.

SYNOPSIS

#!/usr/bin/env perl
use strict;
use warnings;
use Hash::Convert;

my $rules = {
    visit   => { from => 'created_at' },
    count   => { from => 'count', via => sub { $_[0] + 1 }, default => 1 },
    visitor => {
        contain => {
            name => { from => 'name' },
            mail => { from => 'mail' },
        },
        default => {
            name => 'anonymous',
            mail => 'anonymous',
        }
    },
    price => {
        from => [qw/item.cost item.discount/],
        via => sub {
            my $cost     = $_[0];
            my $discount = $_[1];
            return $cost * ( (100 - $discount) * 0.01 );
        },
    },
};
my $opts = { pass => 'locate' };

my $converter = Hash::Convert->new($rules, $opts);

my $before = {
    created_at => time,
    count      => 1,
    name       => 'hixi',
    mail       => 'hixi@cpan.org',
    locate     => 'JP',
    item => {
        name     => 'chocolate',
        cost     => 100,
        discount => 10,
    },
};
my $after = $converter->convert($before);
print Dumper $after;
#{
#    'visitor' => {
#        'mail' => 'hixi@cpan.org',
#        'name' => 'hixi'
#    },
#    'count' => 2,
#    'visit' => '1377019766',
#    'price' => 90,
#    'locate' => 'JP'
#}

DESCRIPTION

Hash::Convert is can define hash converter based on the rules.

Function

convert

Convert hash structure from before value.

my $rules = {
    mail => { from => 'email' }
};
my $converter = Hash::Convert->new($rules);
my $before = { email => 'hixi@cpan.org' };
my $after  = $converter->convert($before);
#{
#  mail => 'hixi@cpan.org',
#}

Rules

Command

Others expression

LICENSE

Copyright (C) Hiroyoshi Houchi.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

Hiroyoshi Houchi hixi@cpan.org