On Tue, Jun 03, 2008 at 12:49:57PM +0100, Paul LeoNerd Evans wrote:
> Of course, now we have the simplest of hooklist aggregations - the
> "repeat while false" one. What if we wanted something else? Suppose we
> wanted to sum some quantity?

>   my $hooklist = Hooklist->new(
>      aggregate => sub { $a + $b }, # accumulate in $a using the per-hook 
> value $b
>   );

Actually, I've thought about it some more. We don't strictly need these,
if we're prepared to do just a little bit more work in the handler or
invokation time:

  my $hooklist = Hooklist->new();

  $hooklist->register_sync( sub {
     my %params = @_;
     $params{args}->( $count );
  } );

  my $total = 0;
  $hooklist->fire_sync( sub { $total += shift } );

Or maybe even simpler

  $hooklist->register_sync( sub {
     my %params = @_;
     $params{args}[0] += $count;
  } );

  my @args = ( 0 );
  $hooklist->fire_sync( [EMAIL PROTECTED] );
  my $total = $args[0];

We can also do looping control in sync. handlers by just doing the
"last HOOK" trick there.

It does mean putting more intelligence in the specific handlers and
callsites of the hook list, rather than in the Hooklist object itself,
which is where I originally wanted it. The advantage of being in the
list itself is to keep good modularity - the hooked functions don't need
to consider it so much any more.

Opinions either way?

-- 
Paul "LeoNerd" Evans

[EMAIL PROTECTED]
ICQ# 4135350       |  Registered Linux# 179460
http://www.leonerd.org.uk/

Attachment: signature.asc
Description: Digital signature

Reply via email to