Changeset 2285

Show
Ignore:
Timestamp:
06/11/07 10:00:21
Author:
miyagawa
Message:

killed multiple hooks reg using one add_trigger. Fixed the coderef check so it works when named parameters passed as a hash

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • Class-Trigger/trunk/Changes

    r2272 r2285  
    11Revision history for Perl extension Class::Trigger. 
     2 
     30.11_02 
     4    - Added a test to make sure $_ is not clobbered 
     5      (Thanks to mark addison) 
     6    - Killed the hidden feature to add multiple hooks in one 
     7      add_trigger() call because it interfers with named parameter style 
     8      calls. (Thanks to clkao) 
    29 
    3100.11_01  Mon May 28 21:03:03 PDT 2007 
  • Class-Trigger/trunk/lib/Class/Trigger.pm

    r2284 r2285  
    2626    my $triggers = __fetch_triggers($proto); 
    2727 
    28     if (ref($_[1]) eq 'CODE') {  
    29  
    30     while (my($when, $code) = splice @_, 0, 2) { 
    31         __validate_triggerpoint($proto, $when); 
    32         Carp::croak('add_trigger() needs coderef') unless ref($code) eq 'CODE'; 
    33         push @{$triggers->{$when}}, [$code, undef]; 
    34     } 
    35     } 
    36     elsif (grep {'name'} @_) { 
    37         my %args = ( name => undef, callback => undef, abortable => undef, @_); 
    38         my $when= $args{'name'}; 
    39         my $code = $args{'callback'}; 
    40         my $abortable = $args{'abortable'}; 
    41         __validate_triggerpoint($proto, $when); 
    42         Carp::croak('add_trigger() needs coderef') unless ref($code) eq 'CODE'; 
    43         push @{$triggers->{$when}}, [$code, $abortable]; 
    44  
    45  
    46     } else { 
    47         Carp::croak('add_trigger() needs coderef'); 
    48  
    49     } 
     28    my %params = @_; 
     29    my @values = values %params; 
     30    if (@_ > 2 && grep { ref && ref eq 'CODE' } @values == @values) { 
     31        Carp::croak "mutiple trigger registration in one add_trigger() call is deprecated."; 
     32    } 
     33 
     34    if ($#_ == 1 && ref($_[1]) eq 'CODE') { 
     35        @_ = (name => $_[0], callback => $_[1]); 
     36    } 
     37 
     38    my %args = ( name => undef, callback => undef, abortable => undef, @_ ); 
     39    my $when = $args{'name'}; 
     40    my $code = $args{'callback'}; 
     41    my $abortable = $args{'abortable'}; 
     42    __validate_triggerpoint( $proto, $when ); 
     43    Carp::croak('add_trigger() needs coderef') unless ref($code) eq 'CODE'; 
     44    push @{ $triggers->{$when} }, [ $code, $abortable ]; 
     45 
    5046    1; 
    5147} 
     
    335331=back 
    336332 
    337 =head1 AUTHOR 
     333=head1 AUTHORS 
    338334 
    339335Original idea by Tony Bowden E<lt>tony@kasei.comE<gt> in Class::DBI. 
    340336 
    341337Code by Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>. 
     338 
     339Jesse Vincent added a code to get return values from triggers and 
     340abortable flag. 
    342341 
    343342This library is free software; you can redistribute it and/or modify 
  • Class-Trigger/trunk/t/07_abortable_callbacks.t

    r2271 r2285  
    1414); 
    1515ok( Foo->add_trigger( 
    16         name     => 'after_foo'
    17         callback => sub { print "after_foo\n" } 
     16        callback => sub { print "after_foo\n" }
     17        name     => 'after_foo', # change the param order to test from hash -> list 
    1818    ), 
    1919    'add_trigger in foo'