CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Modularising code?

 



baffled
New User

May 18, 2009, 2:24 PM

Post #1 of 4 (449 views)
Modularising code? Can't Post

I have some code that reads in lots of files and parses them. First they are converted to HTTP::Request and HTTP::Response objects, then a bunch of processing is done over them.

At the moment, this processing is all in one large nasty file, which is over 1000 lines. The processing is broken up into "signatures" which look for certain data depending on the site. As an example:


Code
if ($request->uri eq 'abc') { 
# process
} elsif ($request->uri eq 'xyz') {
# process
}


It would be nicer if I could split this into a separate module, e.g. My::Parser. If I could go even further and split it out, e.g.: My::Parser::ABC and My::Parser::XYZ that would make even more sense. Each module could inspect the data given to it and reject or process accordingly. All modules would return the same output.

Is there an easy way to wrap this up? Can I call My::Parser and it somehow magically call all available My::Parser::ABC/XYZ?


Code
my $parser = new My::Parser; 

# My::Parser magically detects and calls My::Parser::ABC and My::Parser::XYZ
$parser->parse($mydata);


Can I somehow maintain a list, like:


Code
my $parser = new My::Parser; 
$parser->add('ABC');
$parser->add('XYZ');

# Parse the data using My::Parser::ABC and My::Parser::XYZ
$parser->parse($mydata);


Obviously I am solving my own problem here and probably not in the best way. But I have a lot of nasty if/then/else statements which would be much better split into manageable and extendible code!

The alternative seems to be rebuilding/installing My::Parser each time a new parser is added, but that does not seem sensible.


KevinR
Veteran


May 18, 2009, 4:11 PM

Post #2 of 4 (446 views)
Re: [baffled] Modularising code? [In reply to] Can't Post

One possible way:


Code
my $parser = $request->uri; 
eval {require "Parser/$parser"};
if ($@) {
print qq{
Error including the required file "Parser/$parser"\n
$@\n};
exit;
}

-------------------------------------------------


(This post was edited by KevinR on May 18, 2009, 4:11 PM)


baffled
New User

May 19, 2009, 7:09 AM

Post #3 of 4 (432 views)
Re: [KevinR] Modularising code? [In reply to] Can't Post

That's good, but if each is to have a common set of functions then how do I benefit?

For example if I include Parser/ABC.pl and Parser/XYZ.pl and both have a 'parse' subroutine, I do not see how I get round this.


FishMonger
Veteran / Moderator

May 19, 2009, 7:43 AM

Post #4 of 4 (431 views)
Re: [baffled] Modularising code? [In reply to] Can't Post

It comes down to how you handle the inheritance. As a starting point, I recommend all of the perldoc's that relate to modules, and pick up a copy of:
Object Oriented Perl by Damian Conway
http://www.manning.com/conway


(This post was edited by FishMonger on May 19, 2009, 7:44 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives