I've been working on a project that has recently turned into something I'd like to attempt to convert to OOP. I've messed with OOP in the past, but most I've put together in an attempt at it were pretty wretched. In the past I've tried to work things out on my own, but I've spent far too long writing bad code. I turn to my fellow monks for assistance.

Here is the general idea and the implementation (or my excuse of one).

Monitor port ###
Get packet
Parse packet
Compare packet information with hash (or maybe an external DB later)
** Send packet out, do other things, general action based on packet content
(It looks so much simpler when I type it out ;-)

Thats the goal. So here is the clencher. I want it to be a somewhat modular system for adding new events (at the **'s part.) Right now I've got a Monitor package that I would like to have run things, and a Function package to hold all of the "functions" (the ** code.) Here is some relevant code (constructors and other vital code omitted):

my $monitor = new Monitor; $monitor->add_function("3","blah"); package Function; sub blah { if(wantarray){return(qr{blah ?(.+)?})} # Useful code will eventually go here } package Monitor; sub add_function { my($self,$access,$name) = @_; my $ref = \&{"Function::$name"}; my($regexp) = $ref->(); $FUNC{$regexp}{access} = $access; $FUNC{$regexp}{code} = $ref; }
Then I would get packets, loop through the $FUNC keys, applying the regexes and sending the data to the appropriate subs when the regex was a match.

For some reason, I think there is a much better way to do this. The name space is also really messy. Ideally, I'd want several hashes and a few object refs to be globally accessible across packages; no doubt that is an obvious sign of bad code.

Hopefully I have done an adequate job at showing what I am trying to achieve. I would really appreciate any suggestions or comments anyone could contribute. Thanks.

-billyak


In reply to A Not-as-Clumsy OOP Implementation? by billyak

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.