Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

Title:
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (10)
    As of 2014-07-30 07:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (229 votes), past polls