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.

Replies are listed 'Best First'.
Re^2: Binary data structure to moose class.
by chrestomanci (Priest) on Jan 04, 2013 at 17:34 UTC

    Thanks jandrew For the tip on using triggers. It is certainly a step in the right direction as it will allow me to move all the binary unpacking code into one method, that will populate all the fields in the class at once.

    For my project, I am only relay interested in reading binary data, not writing it. If I where trying to write it as well, then I am not sure that using writers would be helpful, as it looks like that will just create the same problem of lots of very similar methods with very similar boilerplate code. I think an easier approach would be to create a custom reader method on rawDataBlock that will re-construct the binary data by packing all the field members.

    Also, thanks for the link to merlyn's tutorials. I will certainly read them. One problem I have with Moose is there is too much documentation, and it can be hard to find a simplified introduction.

      chrestomanci I personally found that a combination of the Moose::Cookbook followed with a good dose of the Moose::Manual was the most concise way to kick-start my Moose learning. However the Moose presentation from Ricardo Signes is a good read. I'm at $work so I can't check that last link.

      The goal of the writers is not to write binary data in the attributes but to write the unpacked data to each attribute from the object so you can use it as follows. (Note the writers are mostly hidden using the _ prefix convention) Assume the Class is called Data::Humanx and that the MooseX::Types coersion worked. Example not tested

      use Data::Humanx my $instance = Data::Humanx-new( rawDataBlock => $content ); print $instance->startTime->ymd( '/' );

      This should print out the start time value in ymd format from the Humanx binary that was input. (This assumes a DateTime object in the startTime attribute)

      Update: The link is good