{
    "mode": "perldoc",
    "parameter": "Class::Trigger",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Class%3A%3ATrigger/json",
    "generated": "2026-06-09T17:28:26Z",
    "synopsis": "package Foo;\nuse Class::Trigger;\nsub foo {\nmy $self = shift;\n$self->calltrigger('beforefoo');\n# some code ...\n$self->calltrigger('middleoffoo');\n# some code ...\n$self->calltrigger('afterfoo');\n}\npackage main;\nFoo->addtrigger(beforefoo => \\&sub1);\nFoo->addtrigger(afterfoo => \\&sub2);\nmy $foo = Foo->new;\n$foo->foo;            # then sub1, sub2 called\n# triggers are inheritable\npackage Bar;\nuse base qw(Foo);\nBar->addtrigger(beforefoo => \\&sub);\n# triggers can be object based\n$foo->addtrigger(afterfoo => \\&sub3);\n$foo->foo;            # sub3 would appply only to this object",
    "sections": {
        "NAME": {
            "content": "Class::Trigger - Mixin to add / call inheritable triggers\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "package Foo;\nuse Class::Trigger;\n\nsub foo {\nmy $self = shift;\n$self->calltrigger('beforefoo');\n# some code ...\n$self->calltrigger('middleoffoo');\n# some code ...\n$self->calltrigger('afterfoo');\n}\n\npackage main;\nFoo->addtrigger(beforefoo => \\&sub1);\nFoo->addtrigger(afterfoo => \\&sub2);\n\nmy $foo = Foo->new;\n$foo->foo;            # then sub1, sub2 called\n\n# triggers are inheritable\npackage Bar;\nuse base qw(Foo);\n\nBar->addtrigger(beforefoo => \\&sub);\n\n# triggers can be object based\n$foo->addtrigger(afterfoo => \\&sub3);\n$foo->foo;            # sub3 would appply only to this object\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Class::Trigger is a mixin class to add / call triggers (or hooks) that get called at some points\nyou specify.\n",
            "subsections": []
        },
        "METHODS": {
            "content": "By using this module, your class is capable of following methods.\n\naddtrigger\nFoo->addtrigger($triggerpoint => $sub);\n$foo->addtrigger($triggerpoint => $sub);\n\n\nFoo->addtrigger( name => $triggerpoint,\ncallback => sub {return undef},\nabortable => 1);\n\n# no further triggers will be called. Undef will be returned.\n\nAdds triggers for trigger point. You can have any number of triggers for each point. Each\ncoderef will be passed a reference to the calling object, as well as arguments passed in via\ncalltrigger. Return values will be captured in *list context*.\n\nIf addtrigger is called with named parameters and the \"abortable\" parameter is passed a\ntrue value, a false return value from trigger code will stop processing of this trigger\npoint and return a \"false\" value to the calling code.\n\nIf \"addtrigger\" is called without the \"abortable\" flag, return values will be captured by\ncalltrigger, but failures will be ignored.\n\nIf \"addtrigger\" is called as object method, whole current trigger table will be copied onto\nthe object and the new trigger added to that. (The object must be implemented as hash.)\n\nmy $foo = Foo->new;\n\n# this trigger ($subfoo) would apply only to $foo object\n$foo->addtrigger($triggerpoint => $subfoo);\n$foo->foo;\n\n# And not to another $bar object\nmy $bar = Foo->new;\n$bar->foo;\n\ncalltrigger\n$foo->calltrigger($triggerpoint, @args);\n\nCalls triggers for trigger point, which were added via \"addtrigger\" method. Each triggers\nwill be passed a copy of the object as the first argument. Remaining arguments passed to\n\"calltrigger\" will be passed on to each trigger. Triggers are invoked in the same order\nthey were defined.\n\nIf there are no \"abortable\" triggers or no \"abortable\" trigger point returns a false value,\n\"calltrigger\" will return the number of triggers processed.\n\nIf an \"abortable\" trigger returns a false value, call trigger will stop execution of the\ntrigger point and return undef.\n\nlasttriggerresults\nmy @results = @{ $foo->lasttriggerresults };\n\nReturns a reference to an array of the return values of all triggers called for the last\ntrigger point. Results are ordered in the same order the triggers were run.\n",
            "subsections": []
        },
        "TRIGGER POINTS": {
            "content": "By default you can make any number of trigger points, but if you want to declare names of\ntrigger points explicitly, you can do it via \"import\".\n\npackage Foo;\nuse Class::Trigger qw(foo bar baz);\n\npackage main;\nFoo->addtrigger(foo  => \\&sub1); # okay\nFoo->addtrigger(hoge => \\&sub2); # exception\n",
            "subsections": []
        },
        "FAQ": {
            "content": "Acknowledgement: Thanks to everyone at POOP mailing-list (http://poop.sourceforge.net/).\n\nQ.  This module lets me add subs to be run before/after a specific subroutine is run. Yes?\n\nA.  You put various calltrigger() method in your class. Then your class users can call\naddtrigger() method to add subs to be run in points just you specify (exactly where you put\ncalltrigger()).\n\nQ.  Are you aware of the perl-aspects project and the Aspect module? Very similar to\nClass::Trigger by the look of it, but its not nearly as explicit. Its not necessary for\nfoo() to actually say \"triggers go *here*\", you just add them.\n\nA.  Yep ;)\n\nBut the difference with Aspect would be that Class::Trigger is so simple that it's easy to\nlearn, and doesn't require 5.6 or over.\n\nQ.  How does this compare to Sub::Versive, or Hook::LexWrap?\n\nA.  Very similar. But the difference with Class::Trigger would be the explicitness of trigger\npoints.\n\nIn addition, you can put hooks in any point, rather than pre or post of a method.\n\nQ.  It looks interesting, but I just can't think of a practical example of its use...\n\nA.  (by Tony Bowden)\n\nI originally added code like this to Class::DBI to cope with one particular case:\nauto-upkeep of full-text search indices.\n\nSo I added functionality in Class::DBI to be able to trigger an arbitrary subroutine every\ntime something happened - then it was a simple matter of setting up triggers on INSERT and\nUPDATE to reindex that row, and on DELETE to remove that index row.\n\nSee Class::DBI::mysql::FullTextSearch and its source code to see it in action.\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "Original idea by Tony Bowden <tony@kasei.com> in Class::DBI.\n\nCode by Tatsuhiko Miyagawa <miyagawa@bulknews.net>.\n\nJesse Vincent added a code to get return values from triggers and abortable flag.\n",
            "subsections": []
        },
        "LICENSE": {
            "content": "This library is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Class::DBI\n",
            "subsections": []
        }
    },
    "summary": "Class::Trigger - Mixin to add / call inheritable triggers",
    "flags": [],
    "examples": [],
    "see_also": []
}