[OT] Perl OO Question.
Jeff Anderson
captvanhalen at gmail.com
Thu Oct 5 18:34:50 BST 2006
I'm with Zach on this one ... Factory pattern all the way.
http://search.cpan.org/~phred/Class-Factory/
On 10/5/06, Matt Lawrence <matt.lawrence at virgin.net> wrote:
>
> McGlinchy, Alistair wrote:
> > Hi All,
> >
> > I have a noddy OO question.
> >
> > I am trying to OO-ify some file grepping code. Every so often files
> > appear in a directory and they all need the same generic tasks performed
> > on them (scan for useful data, export stats into a standard format,
> > maybe update RRD files, compress and move it to an archive). However the
> > actual file formats varies enormously so I need bespoke code for each
> > file type. I have this stub below from a working mock-up, but it just
> > seems wrong to me.
> >
> > Surely a call to MainframeFile->new() must return a MainframeFile
> > object; or is it ok to return a subclass of MainframeFile?
> > I'm not even sure that MainframeFile should be allowed to "use"
> > MainframeFile::SNI and MainframeFile::CCU at all. It seems backward.
> > Perhaps I need a separate class that knows about MainframeFile,
> > MainframeFile::SNI and MainframeFile::CCU and does the choice for me.
> > If so what would it look like?
> >
> Sounds like a job for Module::Pluggable.
>
> use Module::Pluggable require => 1;
>
> sub new {
> my $file_name = shift;
> for my $plugin (__PACKAGE__->plugins) {
> if ($plugin->looks_like_my_kind_of_file($file_name) {
> return $plugin->new;
> }
> }
> croak "No plugin found to handle file '$file_name'";
> }
>
> 100% untested pseudo code.
>
> Matt
>
--
jeffa
More information about the london.pm
mailing list