Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.

In reply to Re: Binary data structure to moose class. by jandrew
in thread Binary data structure to moose class. by chrestomanci

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (10)
    As of 2018-10-15 13:30 GMT
    Find Nodes?
      Voting Booth?
      When I need money for a bigger acquisition, I usually ...

      Results (82 votes). Check out past polls.