mgad has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Writing a Perl module that dynamically loads other modules
by stephen (Priest) on Mar 01, 2013 at 20:17 UTC | |
by mgad (Novice) on Mar 01, 2013 at 20:52 UTC | |
Re: Writing a Perl module that dynamically loads other modules
by karlgoethebier (Abbot) on Mar 01, 2013 at 20:29 UTC | |
by mgad (Novice) on Mar 01, 2013 at 20:57 UTC | |
by karlgoethebier (Abbot) on Mar 01, 2013 at 21:23 UTC | |
by mgad (Novice) on Mar 04, 2013 at 17:20 UTC | |
by karlgoethebier (Abbot) on Mar 04, 2013 at 18:33 UTC |