[OT] Perl OO Question.

McGlinchy, Alistair Alistair.McGlinchy at marks-and-spencer.com
Thu Oct 5 17:10:20 BST 2006

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?



package MainframeFile;
use MainframeFile::SNI;
use MainframeFile::CCU;
# Etc..

sub new {
    my $class = shift;
    my $self = bless {}, $class;

    my $file_name= shift;

    # Check for special cases 
    my $sub_self;
    $sub_self =  MainframeFile::SNI->new($self) and return $sub_self;   
    $sub_self =  MainframeFile::CCU->new($self) and return $sub_self;

    # Not a special case. Return the normal class
    return $self;  

sub compress {}
sub archive {}

Registered Office:
Marks and Spencer plc
Waterside House
35 North Wharf Road
W2 1NW

Registered No. 214436 in England and Wales.

Telephone (020) 7935 4422
Facsimile (020) 7487 2670


Please note that electronic mail may be monitored.

This e-mail is confidential. If you received it by mistake, please let us know and then delete it from your system; you should not copy, disclose, or distribute its contents to anyone nor act in reliance on this e-mail, as this is prohibited and may be unlawful.

More information about the london.pm mailing list