in reply to Re: Re: Tracking processing by returning objects?
in thread Tracking processing by returning objects?
I guess my explaination left something to be desired, so here it is in slightly expanded pseudo-code.
The basic idea is that rather than each operation on an instance of Foo returning an instance of some State class, it returns a new instance of itself. To perform any further processing on that state of the data, he invokes the operation directly on returned object.
That way there are no seperate objects to be passed back to you wrongly.
package Foo; sub new{ my ($class, filespec) = @_; # check for existance, other init stuff return bless {file=>$filespec }, $class; } sub sort{ $self = shift; system( 'sort', $self->$filespec, '>', "$self->filespec.sorted" ); return $self->new( "$self->$filespec.sorted" ); } sub munge{ $self = shift; system( 'munge', $self->$filespec, '>', "$self->filespec.munged" ) +; return $self->new( "$self->$filespec.munged" ); } 1; package main; # Start with a object created by the user. my $data = Foo->new( 'datafile' );; # Munge it two different ways and get two seperate objects back repres +enting the two new states my $sorted = $data->sort(); my $munged = $data->munged(); # Further processing on the new state is invoked directly on the new o +bject my $sorted'n'munged = $sorted->munge(); my $munged'n'sorted = $munged->sort(); # Or even pipeline them my $sorted'n'munged'n'sorted_again = $data->sort()->munge()->sort();
Anyways, if I understood your question correctly, that's how I would do it.
HTH. Sorry, if it doesn't.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Tracking processing by returning objects?
by BazB (Priest) on May 18, 2003 at 17:00 UTC | |
by BrowserUk (Patriarch) on May 19, 2003 at 03:33 UTC | |
by BazB (Priest) on May 19, 2003 at 08:17 UTC |