Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-19 23:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found