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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Modularising code?


New User

May 18, 2009, 2:24 PM

Post #1 of 4 (486 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:

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?

my $parser = new My::Parser; 

# My::Parser magically detects and calls My::Parser::ABC and My::Parser::XYZ

Can I somehow maintain a list, like:

my $parser = new My::Parser; 

# Parse the data using My::Parser::ABC and My::Parser::XYZ

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.


May 18, 2009, 4:11 PM

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

One possible way:

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


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

New User

May 19, 2009, 7:09 AM

Post #3 of 4 (469 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/ and Parser/ and both have a 'parse' subroutine, I do not see how I get round this.

Veteran / Moderator

May 19, 2009, 7:43 AM

Post #4 of 4 (468 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

(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