http://www.perlmonks.org?node_id=1011374


in reply to Binary data structure to moose class.

tobyink++ is a far superior Moose programmer but you may simplify your code some without going completely meta by using trigger

use Moose; use MyTypes qw( my_date_time ); has 'rawDataBlock' => ( is => 'rw', isa => 'Str', trigger => \&_unpack_data, ); has 'lastPlay' => ( is =>'rw', isa =>'Int', writer => '_set_lastPlay', ); has 'ChanNum' => ( is=>'rw', isa=>'Int', writer => '_set_ChanNum', ); has 'startTime' => ( is => 'rw', isa => my_date_time, writer => '_set_startTime', coerce => 1, ); has 'fileName' => ( is => 'rw', isa => 'Str', writer => '_set_fileName', ); sub _unpack_data{ my ( $self, $new_data ) = @_; my @results = unpack( $my_template, $new_data); $self->_set_lastPlay( $results[0] ); $self->_set_ChanNum( $results[1] ); $self->_set_startTime( $results[2] ); $self->_set_fileName( $results[3] ); }

This is sample code and not tested. Note the use of type coersion (my_date_time) to get the data into the right format after it has been unpacked. I always liked the merlyn tutorials The Moose is flying I and the Moose is flying II. MyTypes would use MooseX::Types

Update: fixed some typos and added the coerce flag to startTime.