man > Class::Trigger(3pm)

πŸ“‹ NAME

Class::Trigger - Mixin to add / call inheritable triggers

πŸš€ Quick Reference

Use CaseCommandDescription
Add a trigger to a classFoo->add_trigger($point => \&sub)Register a callback for a named trigger point on the class
Add an abortable triggerFoo->add_trigger(name => $point, callback => sub {...}, abortable => 1)Trigger stops execution if it returns false
Call a trigger point$self->call_trigger($point, @args)Invoke all registered triggers for the point
Add an object‑specific trigger$obj->add_trigger($point => \&sub)Trigger only applies to that object instance
Retrieve last trigger resultsmy @results = @{ $obj->last_trigger_results }Get return values from the most recent trigger execution
Declare allowed trigger pointsuse Class::Trigger qw(foo bar baz);Restrict trigger points to a predefined list

πŸ“– SYNOPSIS

 package Foo;
 use Class::Trigger;

 sub foo {
     my $self = shift;
     $self->call_trigger('before_foo');
     # some code ...
     $self->call_trigger('middle_of_foo');
     # some code ...
     $self->call_trigger('after_foo');
 }

 package main;
 Foo->add_trigger(before_foo => \&sub1);
 Foo->add_trigger(after_foo => \&sub2);

 my $foo = Foo->new;
 $foo->foo;            # then sub1, sub2 called

 # triggers are inheritable
 package Bar;
 use base qw(Foo);

 Bar->add_trigger(before_foo => \&sub);

 # triggers can be object based
 $foo->add_trigger(after_foo => \&sub3);
 $foo->foo;            # sub3 would appply only to this object

πŸ“ DESCRIPTION

Class::Trigger is a mixin class to add / call triggers (or hooks) that get called at some points you specify.

πŸ› οΈ METHODS

By using this module, your class is capable of following methods.

πŸ”§ add_trigger

Foo->add_trigger($triggerpoint => $sub);
$foo->add_trigger($triggerpoint => $sub);

Foo->add_trigger( name => $triggerpoint,
                  callback => sub {return undef},
                  abortable => 1);

# no further triggers will be called. Undef will be returned.

Adds triggers for trigger point. You can have any number of triggers for each point. Each coderef will be passed a reference to the calling object, as well as arguments passed in via call_trigger. Return values will be captured in list context.

If add_trigger is called with named parameters and the abortable parameter is passed a true value, a false return value from trigger code will stop processing of this trigger point and return a false value to the calling code.

If add_trigger is called without the abortable flag, return values will be captured by call_trigger, but failures will be ignored.

If add_trigger is called as object method, whole current trigger table will be copied onto the object and the new trigger added to that. (The object must be implemented as hash.)

my $foo = Foo->new;

# this trigger ($sub_foo) would apply only to $foo object
$foo->add_trigger($triggerpoint => $sub_foo);
$foo->foo;

# And not to another $bar object
my $bar = Foo->new;
$bar->foo;

πŸ”§ call_trigger

$foo->call_trigger($triggerpoint, @args);

Calls triggers for trigger point, which were added via add_trigger method. Each triggers will be passed a copy of the object as the first argument. Remaining arguments passed to call_trigger will be passed on to each trigger. Triggers are invoked in the same order they were defined.

If there are no abortable triggers or no abortable trigger point returns a false value, call_trigger will return the number of triggers processed.

If an abortable trigger returns a false value, call trigger will stop execution of the trigger point and return undef.

πŸ”§ last_trigger_results

my @results = @{ $foo->last_trigger_results };

Returns a reference to an array of the return values of all triggers called for the last trigger point. Results are ordered in the same order the triggers were run.

🎯 TRIGGER POINTS

By default you can make any number of trigger points, but if you want to declare names of trigger points explicitly, you can do it via import.

package Foo;
use Class::Trigger qw(foo bar baz);

package main;
Foo->add_trigger(foo  => \&sub1); # okay
Foo->add_trigger(hoge => \&sub2); # exception

❓ FAQ

Acknowledgement: Thanks to everyone at POOP mailing-list (http://poop.sourceforge.net/).

πŸ‘€ AUTHORS

Original idea by Tony Bowden <tony@kasei.com> in Class::DBI.

Code by Tatsuhiko Miyagawa <miyagawa@bulknews.net>.

Jesse Vincent added a code to get return values from triggers and abortable flag.

πŸ“„ LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

πŸ”— SEE ALSO

Class::DBI

perl v5.30.0 2020-04-08 Class::Trigger(3pm)

Class::Trigger(3pm)
πŸ“‹ NAME πŸš€ Quick Reference πŸ“– SYNOPSIS πŸ“ DESCRIPTION πŸ› οΈ METHODS
πŸ”§ add_trigger πŸ”§ call_trigger πŸ”§ last_trigger_results
🎯 TRIGGER POINTS ❓ FAQ πŸ‘€ AUTHORS πŸ“„ LICENSE πŸ”— SEE ALSO

Generated by phpman v4.9.29-dirty · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-25 03:24 @216.73.216.203
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format

^_top_^