BackPAN::Index::Role::AsHash - Role to dump object data as a hash


BackPAN-Index documentation Contained in the BackPAN-Index distribution.

Index


Code Index:

NAME

Top

BackPAN::Index::Role::AsHash - Role to dump object data as a hash

SYNOPSIS

Top

    use BackPAN::Index::Role::AsHash;

    sub data_methods { return qw(list of data methods) }

DESCRIPTION

Top

A role to implement <as_hash> in result objects.

Requires

The receiving class must implement...

data_methods

    my @methods = $self->data_methods;

Returns a list of methods which get data about the object.

Implements

as_hash

    my $hash = $self->as_hash;

Produces a hash reference representing the object's data based on <$self-data_methods>>. Each key is a method name, the value is <$self-$method>>.


BackPAN-Index documentation Contained in the BackPAN-Index distribution.

package BackPAN::Index::Role::AsHash;

use strict;
use warnings;

use base 'Exporter';
our @EXPORT   = qw(as_hash);
our @REQUIRED = qw(data_methods);

use CLASS;

sub as_hash {
    my $self = shift;

    my %data;
    for my $method ($self->data_methods) {
        $data{$method} = $self->$method;
    }

    return \%data;
}


1;