#!/usr/bin/perl use strict; use warnings; use Data::Dumper; package Stat; use Moose; has 'val' => (isa => 'HashRef', is => 'rw'); has 'position' => (is => 'rw'); sub add_kv { my $self = shift; my %to_add = @_; my ($k, $v); $self->{val}->{$k} = $v while (($k, $v) = each %to_add); } sub to_s { my $self = shift; my ($k, $v, $s); $s .= $k . "\t" . $v . "\n" while (($k, $v) = each %{$self->{val}}); return $s; } package StatSet; use Moose; has 'stats' => (isa => 'ArrayRef', is => 'rw'); sub add_stat { my $self = shift; my $stat = shift; push @{$self->{stats}}, $stat if $stat; } sub report { my $self = shift; my $s; $s .= $_->to_s . "\n" for (sort { $a->position <=> $b->position } @{$self->stats}); return $s; } package main; my $set = new StatSet; my $stat1 = new Stat; my $stat2 = new Stat; $stat1->add_kv( color => 'green', size => 'huge' ); $stat1->add_kv( amount => 'some' ); $stat1->position(1); $stat2->add_kv( color => 'blue', size => 'large', amount => 'many' ); $stat2->position(2); $set->add_stat($stat1); $set->add_stat($stat2); print $set->report;