I am writing a module which reads in flat text files that are made of fields which contain a tag followed by a value (ex: Time 16:43:25; ). I know ahead of time what tags are allowed, but there are quite a few of them.
I can think of two options to write this:
1.) Write an individual accessor for each tag
2.) Do this (which is what I have done but don't know if it is alright to do>:
after 'file' => sub { my $self = shift; open my $FILE, "<", $self->{file}; HEADER: while ( <$FILE> ){ my ( $tag, $value ) = $_ =~ /\s*(\w+)\s(.+)/; my $accessor = lc $tag; #create an accessor for each header value has $accessor => ( is =>'ro' ); $self->{$accessor} = $value; } close $FILE; };
Maybe I have the wrong idea completely, but I was just wondering if I should choose one way or the other. The Moose tutorial says you should try to __PACKAGE__->meta->make_immutable; which my current method doesn't allow me to do...

In reply to Creating accessors on the fly, or not... by ~~David~~

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!
  • 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, details, 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, summary, 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.