Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Comment on

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

Greeting wise brothers.

I am looking a Moose class reprentation of a binary file format. (The binary file is the metadata file format used by Humax Digital satelite TV recevers. Documented here: http://foxsatdisk.wikispaces.com/.hmt+file+format)

Seeing as the Moose BestPractices discorage overiding new(), the approach I am taking is to insert the raw data as a field in the class, and then have a large number of lazy builders to extract fields. My API currenlty looks like this:

my $hmt_data = new Binary::Humax::hmt_data(); # Object will be us +eless as it contains no data $hmt_data->raw_from_file($path_name); # At this point onl +y the 'rawData' field is populated my $field = $hmt_data->startTime(); # Invokes a lazy bu +ilder to extract the field.

Some of the code in the class looks like this

use DateTime; use Moose; # The raw data that all the fields are extracted from. has 'rawDataBlock' => ( is => 'rw', isa => 'Str', ); # All the fields extracted from the raw data block are lazy using buil +ders below. has 'lastPlay' => ( lazy_build => 1, is=>'rw', isa=>'Int'); has 'ChanNum' => ( lazy_build => 1, is=>'rw', isa=>'Int'); has 'startTime' => ( lazy_build => 1, is=>'rw', isa=>'DateTime') +; has 'fileName' => ( lazy_build => 1, is=>'rw', isa=>'Str'); [...] sub _build_lastPlay { my $self = shift @_; return unpack('@5 S', $self->rawDataBlock() ); } sub _build_ChanNum { my $self = shift @_; return unpack('@17 S', $self->rawDataBlock() ); } sub _build_startTime { my $self = shift @_; my $epoch = unpack('@25 N', $self->rawDataBlock() ); return DateTime->from_epoch( epoch => $epoch, time_zone => 'GMT' ) +; } sub _build_fileName { my $self = shift @_; return unpack('@33 A512', $self->rawDataBlock() ); }

My problem is that there are rather a lot of those lazy builders, and they are all similar and repetitive. Is there a way I can make all the fields share the same lazy builder, perhaps by putting the unpack information as an attribute to the field definition?

Secondly, when complete, I plan to upload the module to CPAN. Have I chosen a good name for it, or should it live in a different name space?


In reply to 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 taking refuge in the Monastery: (10)
    As of 2014-08-20 09:36 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (109 votes), past polls