in reply to Re^2: OOP first-timer seeks feedback; likes long walks on the beach.
in thread OOP first-timer seeks feedback; likes long walks on the beach.
The disapproved manner ... check this thread A few Perl OOP questions. for more info. I'll leave it at that. ;-)
The caller thing - I'm not sure about speed. But I don't see caller used very often, so it's probably not well understood. That said, I merely mentioned the way I'd normally approach the problem and did not mean to imply that yours was inferior. There's "clever" and "too clever". Where the line is drawn, however, depends on one's experience ;-)
Here's what I think a reasonable usage of this paradigm would look like from the caller's perspective. Note that this is not intended to be the only reasonable usage, just an example of one reasonable usage:
The only purpose of having a pre and a post is when you have loops inside of loops - so the pre and post are executed prior to the loop commencing, and after the loop is completed, respectively.# here's the pre portion... my $count; my %data; IterateOver::file({ loop => sub { if (/^(.*?)\s*=\s*(.*?)\s*(?:#.*)$/) { $data{$1} = $2; } }, $some_file ); # post goes here...
Thus, the example:
Of course, this is completely not an object. To be honest, right-tool-for-right-job does not tell me that OO is the best way to do this in perl (and I'm an OO bigot ... at least from my C++ days!). Closures are way easier to use for this.my @files = ( ... ); my $count; my %data; IterateOver::files({ pre => sub { print "Starting on file $_.\n"; $count = 0; }, loop => sub { if (/^[^#]/ and /^(.*?)\s*=\s*(.*?)\s*(?:#.*)$/) { $data{$1} = $2; ++$count; } }, post => sub { print "Found $count keys in $_.\n"; print "Total unique keys so far: ", scalar keys %data, "\n"; }, @files );
Don't get me wrong, I've done this in an object in perl. But that's because I was part of a framework, and my framework was already object oriented (oddly enough, this is the third time today I've talked about TaskManagers and Tasks). I have a package that does iteration over some common data, and then calls object methods (not passed in but predefined) as hooks into various parts of the loop - including pre-filter, filter, and post-filter. There's no need to pass in callbacks because the loop function is inside another module dedicated to this task. It looks something like this (see above for the layout of Task):
package TaskFilter; use base 'Task'; sub prefilter { 1 } # default pre is to do nothing and succeed at it. sub postfilter { 1 } # same thing sub filter { die "need to implement filter in subclass '" . ref $_[0] . "'\n"; } sub get_object_list { # generic implementation common to all my tasks derived from here. } sub sort_object_list { # to allow subclass to change order if needed. sort @_ } sub perform_task { my $self = shift; $self->prefilter() or die "prefilter failed."; my @records = $self->sort_object_list($self->get_object_list()); for my $r (@records) { $self->filter($r); } $self->postfilter() or die "postfilter failed."; 1; } 1;
Does that help?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: OOP first-timer seeks feedback; likes long walks on the beach.
by eff_i_g (Curate) on Sep 09, 2005 at 00:49 UTC | |
|
Re^4: OOP first-timer seeks feedback; likes long walks on the beach.
by eff_i_g (Curate) on Sep 09, 2005 at 19:46 UTC | |
by Tanktalus (Canon) on Sep 09, 2005 at 21:27 UTC | |
by eff_i_g (Curate) on Sep 12, 2005 at 21:17 UTC | |
by eff_i_g (Curate) on Sep 09, 2005 at 21:54 UTC | |
by eff_i_g (Curate) on Sep 10, 2005 at 00:17 UTC |