Hi everyone,

I've been looking through CPAN trying to find some examples of a Perl module that does this, but I haven't come across one that seems like a fit for my specific application. Basically, I'm writing a module that will read a file, slurp in it's contents, parse through the contents until it finds a FILETYPE, load a module based on the FILETYPE, and then continue processing the rest of the file. The end goal is to return a data structure ($self) that contains the important parts of the file (important parts are identified by regexs).

The files I'm reading look something like this:

...A couple of header lines... FILETYPE=XXXXXX ...The useful information I'm after...

The module I've created thus far looks like this:

package My::Package; use strict; use warnings; ...other module stuff... sub read_slurp { # "worst-case-scenario" way of splitting up the # file into its separate lines foreach ( split /(?:\015{1,2}\012|\015|\012)/, shift ) { # Skip comments if ( /^(?:\#|\!|$)/ ) { next; } ###################################### # Parse the Filetype info ###################################### if ( /^FILETYPE=(.*)\s*$/ ) { $self->{'FILETYPE'} = "$1"; next; } ... LOAD THE APPROPRIATE MODULE ... ... AND CONTINUE PARSING ...

The individual modules to be loaded would be lists of if/then regexs that would identify info and save it to the data structure like so:

###################################### # Parse the blockname ###################################### if ( /^blockname\s+(.*)\s*$/ ) { $self->{'blockname'} = "$1"; next; } ###################################### # Parse the list info ###################################### if ( /^list\s+(\d+)\s+(.*)\s*$/ ) { $self->{'list'}->{"$1"}->{"$3"} = "$2"; next; } ...ETC ETC ETC...

Any suggestions of places to look or other modules that do something similar? I've been looking over the source code of tons of other modules (a useful exercise anyway) but I figured having some external input from experts wouldn't hurt.

Cheers and thanks!


In reply to Writing a Perl module that dynamically loads other modules by mgad

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.