One way to handle this is to use a Factory pattern, where the product of the Factory is an object whose type depends on the packet content.

You can use polymorphism to get the varying behavior of these objects.

One simple way to do it is this:

package Packet; my %patterns; # could add some in declaration sub addType { my $regex = shift; my $className = shift; $patterns{$className} = $regex; } sub removeType { my $className = shift; delete $patterns{$className}; } sub makeOnePacket { # get a packet my $packet = getAPacket(); return undef unless $packet; my $newObject; my ( $className, $regex ); while ( ( $className, $regex ) = each(%patterns) ) { my @fields; if ( @fields = ( $packet =~ $regex ) ) { $newObject = $className->new( $packet, @fields ); last; } } $newObject; } # These two must be defined by classes that want # to work with this system. sub newFromPacket { my $class = shift; my $packet = shift; my @fields = @_; # initialize as needed, return object. bless { packet => $packet, fields => \@fields }, $class; } sub process { my $self = shift; # now do whatever is necessary } package main; Packet::addType( qr/^(abc)(.*)/, 'SomeType' ); Packet::addType( qr/^(def)(.*)/, 'SomeOtherType' ); while ( my $newPacket = makeOnePacket() ) { $newPacket->process(); }

Note that now you can add new packages that work with this system by just including them. Stick the following in NewPacket.pm:

package NewPacket; use Packet; # Could have inherited from Packet, but no # reason to. Luckily, this isn't Java or C++. sub newFromPacket { my $class = shift; my $packet = shift; my @fields = @_; # initialize as needed, return object. bless { packet => $packet, fields => \@fields }, $class; } sub process { my $self = shift; } # you could also use __PACKAGE__ here: Packet::addType( qr/^(something)(.*)/, 'NewPacket' ); Packet::addType( qr/^(somethingElse)(.*)/, 'NewPacket' ); # returns + true

Then a simple use NewPacket; in your main will add the new functionality.

update: added explanation about adding new types.


In reply to Re: A Not-as-Clumsy OOP Implementation? by bikeNomad
in thread 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.