perl-oop-hints

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



Newsgroups: comp.lang.perl.moderated

From: "Ned Konz" <ned@bike-nomad.com>
Subject: Re: Trouble With OO Structure
Date: Fri, 21 Jul 2000 09:48:06 -0800

In article <20000720233428.B3503@namodn.com>, Nick Jennings
<nick@namodn.com> wrote:

> 	Having trouble thinking of a way to implement this class I've been
>    working on. I'd _like_ to keep this all in one class

Why would you want this to be all in one class?
You have more than one kind of thing here, so there should be
more than one class. There's no overhead to speak of per class.
You can put more than one package (class) in a module (file).

> my $conf = package::->load_config();

So $conf could be returned as a package::Configuration.

(BTW, if you use upper case for
the first letter of your class name you won't need the trailing ::).

> my $single_obj = $conf->get_next_object();

So package::Configuration understands get_next_object(), and returns
a 'package' object.

Your structure then looks like (this can be all in one file MyStuff.pm):

package MyStuff::Config;

sub loadConfig {
	my $class = shift;
	my $self = {};	# or [] or whatever you need
	# read config
	bless($self, $class);
}

sub getNextObject {
	my $self = shift;
	# do stuff
	return MyStuff->new();	# passing whatever you need
}

package MyStuff;

sub new {
	my $class = shift;
	my $self = bless {}, $class;
	# do stuff
	return $self;
}
	

And you would call it like this:

use MyStuff;

my $config = MyStuff::Config->loadConfig( ... );
my $obj = $config->getNextObject();	# obj is a MyStuff

===


the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu