Class-Trigger-0.15000755000765000024 013642522400 14166 5ustar00miyagawastaff000000000000README100644000765000024 1362113642522400 15152 0ustar00miyagawastaff000000000000Class-Trigger-0.15NAME Class::Trigger - Mixin to add / call inheritable triggers 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/). Q. This module lets me add subs to be run before/after a specific subroutine is run. Yes? A. You put various call_trigger() method in your class. Then your class users can call add_trigger() method to add subs to be run in points just you specify (exactly where you put call_trigger()). Q. Are you aware of the perl-aspects project and the Aspect module? Very similar to Class::Trigger by the look of it, but its not nearly as explicit. Its not necessary for foo() to actually say "triggers go *here*", you just add them. A. Yep ;) But the difference with Aspect would be that Class::Trigger is so simple that it's easy to learn, and doesn't require 5.6 or over. Q. How does this compare to Sub::Versive, or Hook::LexWrap? A. Very similar. But the difference with Class::Trigger would be the explicitness of trigger points. In addition, you can put hooks in any point, rather than pre or post of a method. Q. It looks interesting, but I just can't think of a practical example of its use... A. (by Tony Bowden) I originally added code like this to Class::DBI to cope with one particular case: auto-upkeep of full-text search indices. So I added functionality in Class::DBI to be able to trigger an arbitary subroutine every time something happened - then it was a simple matter of setting up triggers on INSERT and UPDATE to reindex that row, and on DELETE to remove that index row. See Class::DBI::mysql::FullTextSearch and its source code to see it in action. AUTHORS Original idea by Tony Bowden in Class::DBI. Code by Tatsuhiko Miyagawa . 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 Changes100644000765000024 615313642522400 15547 0ustar00miyagawastaff000000000000Class-Trigger-0.15Revision history for Perl extension Class::Trigger. 0.15 2020-04-05 20:41:50 PDT - Repackaged to avoid build issues with modern perls 0.14 Sun Oct 11 15:55:14 PDT 2009 - Make this a non-dev release 0.13_01 Wed Oct 7 21:50:42 PDT 2009 - Disable the cache on the recursive fetch triggers to fix bugs in inheritance https://rt.cpan.org/Public/Bug/Display.html?id=37299 (Michael Schout) 0.13 Thu Mar 6 12:55:09 PST 2008 - Added a cache to fetch_triggers code so as it won't suck down CPU at runtime. RT:30279. Thanks to Alex Vandiver and Michael Schwern 0.12 Mon Aug 20 16:06:50 PDT 2007 - Make this a release 0.11_03 Wed Jun 20 12:12:36 PDT 2007 - Fixed a bug in backward-compatiblity code to handle multiple hooks in one add_trigger() call. (Thanks to mark addison) 0.11_02 Sun Jun 10 18:09:58 PDT 2007 - Added a test to make sure $_ is not clobbered (Thanks to mark addison) - Killed the hidden feature to add multiple hooks in one add_trigger() call because it interfers with named parameter style calls. (Thanks to clkao) 0.11_01 Mon May 28 21:03:03 PDT 2007 - Added support for abortable triggers. - Added named-parameters calling convention for add_trigger - Added new last_trigger_results method and began capturing trigger return values. - FUNCTIONALITY CHANGE: When no triggers are called for a trigger point, the return value is now '0', not undef. (Thanks to Jesse Vincent) 0.11 Thu Feb 22 19:02:09 PST 2007 - Fixed a bug due to Class::Data::Inheritable, where triggers added to the superclass after trigger is added in the child class are ignored in the child class. See t/03_inherit.t for example. (Thanks to Brad Choate, Yann Kerherve, Ben Trott and Fumiaki Yoshimatsu) 0.10 Tue Aug 23 22:11:21 UTC 2005 - Documentation fix 0.09 Fri Feb 18 14:13:22 JST 2005 - Devel::Cover compliant code. Now 100% coverage. (Thanks to Leif Eriksen and Phalanx folks) 0.08 Thu Feb 27 15:51:31 JST 2003 - Performance boostup and document clarification (Thanks to Tim Bunce) 0.07 Sat Feb 15 01:01:36 JST 2003 - s/add_hook/add_trigger/ in pod. (Thanks to Tony Bowden) * now you can pass @args to call_trigger - Class->call_trigger() works (Thanks to Tim Bunce) 0.06 Wed Oct 23 19:13:20 JST 2002 * Fixed bug that trigger disappears when trigger sub modifies $_. (Thanks to Tony Bowden and Barry Hoggard) 0.05 Tue Apr 30 14:06:06 JST 2002 - Example tweak - remove untie() lines to shut up bleadperl 0.04 Fri Dec 28 12:43:32 JST 2001 - Added FAQ section in pod (Thanks to people at POOP mailing list) 0.03 Tue Dec 25 03:30:31 JST 2001 - renamed to Class::Trigger from Class::Hookable * Added object based triggers - fixed bug that subclass shares arrayref of triggers with parent 0.02 Tue Dec 25 02:18:27 JST 2001 - removed evil eval() to inherit Class::Data::Inheritable - makes copy of trigger table in add_trigger() for inheritance (Thanks to Tony Bowden ) 0.01 Mon Dec 24 11:47:13 2001 - original version; created by h2xs 1.19 LICENSE100644000765000024 4407213642522400 15303 0ustar00miyagawastaff000000000000Class-Trigger-0.15This software is copyright (c) 2020 by Original idea by Tony Bowden in Class::DBI. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2020 by Original idea by Tony Bowden in Class::DBI. This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2020 by Original idea by Tony Bowden in Class::DBI. This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End cpanfile100644000765000024 27113642522400 15733 0ustar00miyagawastaff000000000000Class-Trigger-0.15requires 'perl', '5.008001'; on build => sub { requires 'ExtUtils::MakeMaker', '6.59'; requires 'IO::Scalar'; requires 'IO::WrapTie'; requires 'Test::More', '0.32'; }; dist.ini100644000765000024 3713642522400 15653 0ustar00miyagawastaff000000000000Class-Trigger-0.15[@Milla] installer = MakeMaker META.yml100644000765000024 217513642522400 15525 0ustar00miyagawastaff000000000000Class-Trigger-0.15--- abstract: 'Mixin to add / call inheritable triggers' author: - 'Original idea by Tony Bowden in Class::DBI.' build_requires: ExtUtils::MakeMaker: '6.59' IO::Scalar: '0' IO::WrapTie: '0' Test::More: '0.32' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 0 generated_by: 'Dist::Milla version v1.0.20, Dist::Zilla version 6.012, CPAN::Meta::Converter version 2.143240' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Class-Trigger no_index: directory: - eg - examples - inc - share - t - xt requires: perl: '5.008001' resources: bugtracker: https://github.com/miyagawa/Class-Trigger/issues homepage: https://github.com/miyagawa/Class-Trigger repository: https://github.com/miyagawa/Class-Trigger.git version: '0.15' x_contributors: - 'miyagawa ' - 'Tatsuhiko Miyagawa ' - 'tokuhirom ' x_generated_by_perl: v5.20.1 x_serialization_backend: 'YAML::Tiny version 1.73' x_static_install: 0 MANIFEST100644000765000024 64513642522400 15365 0ustar00miyagawastaff000000000000Class-Trigger-0.15# This file was automatically generated by Dist::Zilla::Plugin::Manifest v6.012. Changes LICENSE MANIFEST META.json META.yml Makefile.PL README cpanfile dist.ini lib/Class/Trigger.pm t/00_compile.t t/01_trigger.t t/02_valid.t t/03_inherit.t t/04_object.t t/05_args.t t/06_coverage.t t/07_abortable_callbacks.t t/08_dollar_underscore.t t/09_inherit.t t/author-pod-syntax.t t/lib/Foo.pm t/lib/Foo/Bar.pm t/lib/Foo/Valid.pm META.json100644000765000024 365313642522400 15677 0ustar00miyagawastaff000000000000Class-Trigger-0.15{ "abstract" : "Mixin to add / call inheritable triggers", "author" : [ "Original idea by Tony Bowden in Class::DBI." ], "dynamic_config" : 0, "generated_by" : "Dist::Milla version v1.0.20, Dist::Zilla version 6.012, CPAN::Meta::Converter version 2.143240", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "Class-Trigger", "no_index" : { "directory" : [ "eg", "examples", "inc", "share", "t", "xt" ] }, "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : "6.59", "IO::Scalar" : "0", "IO::WrapTie" : "0", "Test::More" : "0.32" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "develop" : { "requires" : { "Dist::Milla" : "v1.0.20", "Test::Pod" : "1.41" } }, "runtime" : { "requires" : { "perl" : "5.008001" } } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/miyagawa/Class-Trigger/issues" }, "homepage" : "https://github.com/miyagawa/Class-Trigger", "repository" : { "type" : "git", "url" : "https://github.com/miyagawa/Class-Trigger.git", "web" : "https://github.com/miyagawa/Class-Trigger" } }, "version" : "0.15", "x_contributors" : [ "miyagawa ", "Tatsuhiko Miyagawa ", "tokuhirom " ], "x_generated_by_perl" : "v5.20.1", "x_serialization_backend" : "Cpanel::JSON::XS version 4.09", "x_static_install" : 0 } t000755000765000024 013642522400 14352 5ustar00miyagawastaff000000000000Class-Trigger-0.1505_args.t100644000765000024 62413642522400 16121 0ustar00miyagawastaff000000000000Class-Trigger-0.15/tuse strict; use Test::More tests => 3; use IO::Scalar; use lib './t/lib'; use Foo; ok(Foo->add_trigger(foo => sub { print "foo: $_[1]\n" }), 'add_trigger in Foo'); { my $foo = Foo->new; tie *STDOUT, 'IO::Scalar', \my $out; $foo->call_trigger(foo => 1); is $out, "foo: 1\n"; } { tie *STDOUT, 'IO::Scalar', \my $out; Foo->call_trigger(foo => 2); is $out, "foo: 2\n"; } Makefile.PL100644000765000024 226413642522400 16225 0ustar00miyagawastaff000000000000Class-Trigger-0.15# This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v6.012. use strict; use warnings; use 5.008001; use ExtUtils::MakeMaker; my %WriteMakefileArgs = ( "ABSTRACT" => "Mixin to add / call inheritable triggers", "AUTHOR" => "Original idea by Tony Bowden in Class::DBI.", "BUILD_REQUIRES" => { "ExtUtils::MakeMaker" => "6.59", "IO::Scalar" => 0, "IO::WrapTie" => 0, "Test::More" => "0.32" }, "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => 0 }, "DISTNAME" => "Class-Trigger", "LICENSE" => "perl", "MIN_PERL_VERSION" => "5.008001", "NAME" => "Class::Trigger", "PREREQ_PM" => {}, "VERSION" => "0.15", "test" => { "TESTS" => "t/*.t" } ); my %FallbackPrereqs = ( "ExtUtils::MakeMaker" => "6.59", "IO::Scalar" => 0, "IO::WrapTie" => 0, "Test::More" => "0.32" ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) { delete $WriteMakefileArgs{TEST_REQUIRES}; delete $WriteMakefileArgs{BUILD_REQUIRES}; $WriteMakefileArgs{PREREQ_PM} = \%FallbackPrereqs; } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); 02_valid.t100644000765000024 67713642522400 16271 0ustar00miyagawastaff000000000000Class-Trigger-0.15/tuse strict; use Test::More tests => 4; use lib './t/lib'; use Foo::Valid; # should be use() ok(Foo::Valid->add_trigger(before_foo => sub { })); ok(Foo::Valid->add_trigger(before_foo => sub { })); eval { Foo::Valid->add_trigger(invalid => sub { }); }; like $@, qr/invalid is not valid triggerpoint for Foo::Valid/, $@; my $foo = Foo::Valid->new; eval { $foo->bar; }; like $@, qr/invalid is not valid triggerpoint for Foo::Valid/, $@; lib000755000765000024 013642522400 15120 5ustar00miyagawastaff000000000000Class-Trigger-0.15/tFoo.pm100644000765000024 31313642522400 16316 0ustar00miyagawastaff000000000000Class-Trigger-0.15/t/libpackage Foo; use Class::Trigger; sub new { bless {}, shift; } sub foo { my $self = shift; $self->call_trigger('before_foo'); print "foo\n"; $self->call_trigger('after_foo'); } 1; 04_object.t100644000765000024 161113642522400 16447 0ustar00miyagawastaff000000000000Class-Trigger-0.15/tuse strict; use Test::More tests => 5; use IO::Scalar; use lib './t/lib'; use Foo; ok(Foo->add_trigger(before_foo => sub { print "before_foo\n" }), 'add_trigger in Foo'); { my $foo = Foo->new; tie *STDOUT, 'IO::Scalar', \my $out; $foo->add_trigger(after_foo => sub { print "after_foo\n" }); $foo->foo; is $out, "before_foo\nfoo\nafter_foo\n"; } { my $foo = Foo->new; tie *STDOUT, 'IO::Scalar', \my $out; $foo->foo; untie *STDOUT; is $out, "before_foo\nfoo\n"; } { my $foo = Foo->new; tie *STDOUT, 'IO::Scalar', \my $out; $foo->add_trigger(after_foo => sub { print "after_foo1\n" }); $foo->add_trigger(after_foo => sub { print "after_foo2\n" }); $foo->foo; is $out, "before_foo\nfoo\nafter_foo1\nafter_foo2\n"; } { my $foo = Foo->new; tie *STDOUT, 'IO::Scalar', \my $out; $foo->foo; is $out, "before_foo\nfoo\n"; } 00_compile.t100644000765000024 11213642522400 16600 0ustar00miyagawastaff000000000000Class-Trigger-0.15/tuse strict; use Test::More tests => 1; BEGIN { use_ok 'Class::Trigger' } 01_trigger.t100644000765000024 265713642522400 16654 0ustar00miyagawastaff000000000000Class-Trigger-0.15/tuse strict; use Test::More qw(no_plan); use IO::Scalar; use lib './t/lib'; use Foo; ok(Foo->add_trigger(before_foo => sub { print "before_foo\n" }), 'add_trigger in Foo'); ok(Foo->add_trigger(after_foo => sub { print "after_foo\n" }), 'add_trigger in foo'); my $foo = Foo->new; { my $out; tie *STDOUT, 'IO::Scalar', \$out; $foo->foo; is $out, "before_foo\nfoo\nafter_foo\n"; } ok(Foo->add_trigger(after_foo => sub { print "after_foo2\n" }), 'add_trigger in Foo'); { tie *STDOUT, 'IO::Scalar', \my $out; $foo->foo; is $out, "before_foo\nfoo\nafter_foo\nafter_foo2\n"; } ok(Foo->add_trigger(after_foo => sub { print ref $_[0] }), 'add_trigger in Foo'); { tie *STDOUT, 'IO::Scalar', \my $out; $foo->foo; is $out, "before_foo\nfoo\nafter_foo\nafter_foo2\nFoo", 'class name'; } # coverage tests { # pass a non-code ref and catch the carp my @die; eval { local $SIG{__DIE__} = sub {push @die, @_}; Foo->add_trigger(wrong_type => []); }; like(pop(@die), qr(add_trigger[(][)] needs coderef at ), 'check for right callback param'); } { # pass a multiple triggers and catch the carp my @die; eval { local $SIG{__DIE__} = sub {push @die, @_}; Foo->add_trigger(hello => sub{}, world => sub{}); }; like(pop(@die), qr(mutiple trigger registration in one add_trigger[(][)] call is deprecated.), 'check for depricated multi-trigger add'); } 03_inherit.t100644000765000024 145113642522400 16644 0ustar00miyagawastaff000000000000Class-Trigger-0.15/tuse strict; use Test::More tests => 6; use IO::Scalar; use lib './t/lib'; use Foo; use Foo::Bar; ok(Foo->add_trigger(before_foo => sub { print "before_foo\n" }), 'add_trigger in Foo'); ok(Foo::Bar->add_trigger(after_foo => sub { print "after_foo\n" }), 'add_trigger in Foo::Bar'); ok(Foo::Bar->add_trigger(before_foo => sub { print "before_foo2\n" }), 'add_trigger in Foo::Bar'); ok(Foo->add_trigger(before_foo => sub { print "before_foo3\n" }), 'add_trigger in Foo'); my $foo = Foo::Bar->new; { tie *STDOUT, 'IO::Scalar', \my $out; $foo->foo; is $out, "before_foo\nbefore_foo3\nbefore_foo2\nfoo\nafter_foo\n"; } my $foo_parent = Foo->new; { tie *STDOUT, 'IO::Scalar', \my $out; $foo_parent->foo; is $out, "before_foo\nbefore_foo3\nfoo\n", 'Foo not affected'; } 09_inherit.t100644000765000024 120213642522400 16644 0ustar00miyagawastaff000000000000Class-Trigger-0.15/tuse strict; use Test::More tests => 3; my @data; package Foo; use Class::Trigger qw(init); sub init { my $class = shift; my $self = bless {},$class; $self->call_trigger('init'); return $self; } __PACKAGE__->add_trigger(init => sub { push @data, 'foo' }); package Bar; use base 'Foo'; __PACKAGE__->add_trigger(init => sub { push @data, 'bar' }); package Baz; use base 'Bar'; __PACKAGE__->add_trigger(init => sub { push @data, 'baz' }); package main; Foo->init; is join(':', @data), 'foo'; @data = (); Bar->init; is join(':', @data), 'foo:bar'; @data = (); Baz->init; is join(':', @data), 'foo:bar:baz'; @data = (); 06_coverage.t100644000765000024 22213642522400 16753 0ustar00miyagawastaff000000000000Class-Trigger-0.15/tuse strict; use Test::More tests => 1; use lib './t/lib'; use Foo; my $foo = Foo->new(); is($foo->call_trigger(), 0, 'no triggers, no action'); Foo000755000765000024 013642522400 15643 5ustar00miyagawastaff000000000000Class-Trigger-0.15/t/libBar.pm100644000765000024 5113642522400 17001 0ustar00miyagawastaff000000000000Class-Trigger-0.15/t/lib/Foopackage Foo::Bar; use base qw(Foo); 1; Valid.pm100644000765000024 23713642522400 17362 0ustar00miyagawastaff000000000000Class-Trigger-0.15/t/lib/Foopackage Foo::Valid; use base qw(Foo); use Class::Trigger qw(before_foo after_foo); sub bar { my $self = shift; $self->call_trigger('invalid'); } 1; Class000755000765000024 013642522400 15722 5ustar00miyagawastaff000000000000Class-Trigger-0.15/libTrigger.pm100644000765000024 2473313642522400 20054 0ustar00miyagawastaff000000000000Class-Trigger-0.15/lib/Classpackage Class::Trigger; use 5.008_001; use strict; use vars qw($VERSION); $VERSION = "0.15"; use Carp (); my (%Triggers, %TriggerPoints); my %Fetch_All_Triggers_Cache; sub import { my $class = shift; my $pkg = caller(0); $TriggerPoints{$pkg} = { map { $_ => 1 } @_ } if @_; # export mixin methods no strict 'refs'; my @methods = qw(add_trigger call_trigger last_trigger_results); *{"$pkg\::$_"} = \&{$_} for @methods; } sub add_trigger { my $proto = shift; my $triggers = __fetch_triggers($proto); my %params = @_; my @values = values %params; if (@_ > 2 && (grep { ref && ref eq 'CODE' } @values) == @values) { Carp::croak "mutiple trigger registration in one add_trigger() call is deprecated."; } if ($#_ == 1 && ref($_[1]) eq 'CODE') { @_ = (name => $_[0], callback => $_[1]); } my %args = ( name => undef, callback => undef, abortable => undef, @_ ); my $when = $args{'name'}; my $code = $args{'callback'}; my $abortable = $args{'abortable'}; __validate_triggerpoint( $proto, $when ); Carp::croak('add_trigger() needs coderef') unless ref($code) eq 'CODE'; push @{ $triggers->{$when} }, [ $code, $abortable ]; # Clear the cache when class triggers are added. Because triggers are # inherited adding a trigger to one class may effect others. Simplest # thing to do is to clear the whole thing. %Fetch_All_Triggers_Cache = () unless ref $proto; 1; } sub last_trigger_results { my $self = shift; my $result_store = ref($self) ? $self : ${Class::Trigger::_trigger_results}->{$self}; return $result_store->{'_class_trigger_results'}; } sub call_trigger { my $self = shift; my $when = shift; my @return; my $result_store = ref($self) ? $self : ${Class::Trigger::_trigger_results}->{$self}; $result_store->{'_class_trigger_results'} = []; if (my @triggers = __fetch_all_triggers($self, $when)) { # any triggers? for my $trigger (@triggers) { my @return = $trigger->[0]->($self, @_); push @{$result_store->{'_class_trigger_results'}}, \@return; return undef if ($trigger->[1] and not $return[0]); # only abort on false values. } } else { # if validation is enabled we can only add valid trigger points # so we only need to check in call_trigger() if there's no # trigger with the requested name. __validate_triggerpoint($self, $when); } return scalar @{$result_store->{'_class_trigger_results'}}; } sub __fetch_all_triggers { my ($obj, $when, $list, $order, $nocache) = @_; $nocache = 0 unless defined $nocache; my $class = ref $obj || $obj; my $return; my $when_key = defined $when ? $when : ''; unless ($nocache) { return __cached_triggers($obj, $when) if $Fetch_All_Triggers_Cache{$class}{$when_key}; } unless ($list) { # Absence of the $list parameter conditions the creation of # the unrolled list of triggers. These keep track of the unique # set of triggers being collected for each class and the order # in which to return them (based on hierarchy; base class # triggers are returned ahead of descendant class triggers). $list = {}; $order = []; $return = 1; } no strict 'refs'; my @classes = @{$class . '::ISA'}; push @classes, $class; foreach my $c (@classes) { next if $list->{$c}; # if (UNIVERSAL::can($c, 'call_trigger')) { if ($c->can('call_trigger')) { $list->{$c} = []; __fetch_all_triggers($c, $when, $list, $order, 1) unless $c eq $class; if (defined $when && $Triggers{$c}{$when}) { push @$order, $c; $list->{$c} = $Triggers{$c}{$when}; } } } if ($return) { my @triggers; foreach my $class (@$order) { push @triggers, @{ $list->{$class} }; } # Only cache the class triggers, object triggers would # necessitate a much larger cache and they're cheap to # get anyway. $Fetch_All_Triggers_Cache{$class}{$when_key} = \@triggers; return __cached_triggers($obj, $when); } } sub __cached_triggers { my($proto, $when) = @_; my $class = ref $proto || $proto; return @{ $Fetch_All_Triggers_Cache{$class}{$when || ''} }, @{ __object_triggers($proto, $when) }; } sub __object_triggers { my($obj, $when) = @_; return [] unless ref $obj && defined $when; return $obj->{__triggers}{$when} || []; } sub __validate_triggerpoint { return unless my $points = $TriggerPoints{ref $_[0] || $_[0]}; my ($self, $when) = @_; Carp::croak("$when is not valid triggerpoint for ".(ref($self) ? ref($self) : $self)) unless $points->{$when}; } sub __fetch_triggers { my ($obj, $proto) = @_; # check object based triggers first return ref $obj ? $obj->{__triggers} ||= {} : $Triggers{$obj} ||= {}; } 1; __END__ =head1 NAME Class::Trigger - Mixin to add / call inheritable triggers =head1 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 =head1 DESCRIPTION Class::Trigger is a mixin class to add / call triggers (or hooks) that get called at some points you specify. =head1 METHODS By using this module, your class is capable of following methods. =over 4 =item 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 L. Return values will be captured in I. If add_trigger is called with named parameters and the C parameter is passed a true value, a false return value from trigger code will stop processing of this trigger point and return a C value to the calling code. If C is called without the C flag, return values will be captured by call_trigger, but failures will be ignored. If C 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; =item call_trigger $foo->call_trigger($triggerpoint, @args); Calls triggers for trigger point, which were added via C method. Each triggers will be passed a copy of the object as the first argument. Remaining arguments passed to C will be passed on to each trigger. Triggers are invoked in the same order they were defined. If there are no C triggers or no C trigger point returns a false value, C will return the number of triggers processed. If an C trigger returns a false value, call trigger will stop execution of the trigger point and return undef. =item 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. =back =head1 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 C. package Foo; use Class::Trigger qw(foo bar baz); package main; Foo->add_trigger(foo => \&sub1); # okay Foo->add_trigger(hoge => \&sub2); # exception =head1 FAQ B Thanks to everyone at POOP mailing-list (http://poop.sourceforge.net/). =over 4 =item Q. This module lets me add subs to be run before/after a specific subroutine is run. Yes? =item A. You put various call_trigger() method in your class. Then your class users can call add_trigger() method to add subs to be run in points just you specify (exactly where you put call_trigger()). =item Q. Are you aware of the perl-aspects project and the Aspect module? Very similar to Class::Trigger by the look of it, but its not nearly as explicit. Its not necessary for foo() to actually say "triggers go *here*", you just add them. =item A. Yep ;) But the difference with Aspect would be that Class::Trigger is so simple that it's easy to learn, and doesn't require 5.6 or over. =item Q. How does this compare to Sub::Versive, or Hook::LexWrap? =item A. Very similar. But the difference with Class::Trigger would be the explicitness of trigger points. In addition, you can put hooks in any point, rather than pre or post of a method. =item Q. It looks interesting, but I just can't think of a practical example of its use... =item A. (by Tony Bowden) I originally added code like this to Class::DBI to cope with one particular case: auto-upkeep of full-text search indices. So I added functionality in Class::DBI to be able to trigger an arbitary subroutine every time something happened - then it was a simple matter of setting up triggers on INSERT and UPDATE to reindex that row, and on DELETE to remove that index row. See L and its source code to see it in action. =back =head1 AUTHORS Original idea by Tony Bowden Etony@kasei.comE in Class::DBI. Code by Tatsuhiko Miyagawa Emiyagawa@bulknews.netE. Jesse Vincent added a code to get return values from triggers and abortable flag. =head1 LICENSE This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO L =cut author-pod-syntax.t100644000765000024 45413642522400 20270 0ustar00miyagawastaff000000000000Class-Trigger-0.15/t#!perl BEGIN { unless ($ENV{AUTHOR_TESTING}) { print qq{1..0 # SKIP these tests are for testing by the author\n}; exit } } # This file was automatically generated by Dist::Zilla::Plugin::PodSyntaxTests. use strict; use warnings; use Test::More; use Test::Pod 1.41; all_pod_files_ok(); 08_dollar_underscore.t100644000765000024 51413642522400 20674 0ustar00miyagawastaff000000000000Class-Trigger-0.15/tuse strict; use Test::More tests => 2; package Foo; use Class::Trigger; sub foo { my $self = shift; $_ = "foo"; $self->call_trigger('before_foo'); ::is $_, "foo"; } { my $foo = bless {}, "Foo"; $foo->foo; } { Foo->add_trigger(before_foo => sub { }); my $foo = bless {}, "Foo"; $foo->foo; } 07_abortable_callbacks.t100644000765000024 474513642522400 21151 0ustar00miyagawastaff000000000000Class-Trigger-0.15/tuse strict; use Test::More qw(no_plan); use IO::Scalar; use lib './t/lib'; use Foo; ok( Foo->add_trigger( name => 'before_foo', callback => sub { print "before_foo\n" } ), 'add_trigger in Foo' ); ok( Foo->add_trigger( callback => sub { print "after_foo\n" }, name => 'after_foo', # change the param order to test from hash -> list ), 'add_trigger in foo' ); my $foo = Foo->new; { my $out; tie *STDOUT, 'IO::Scalar', \$out; $foo->foo; is $out, "before_foo\nfoo\nafter_foo\n"; } ok( Foo->add_trigger( name => 'after_foo', callback => sub { print "after_foo2\n" } ), 'add_trigger in Foo' ); { tie *STDOUT, 'IO::Scalar', \my $out; $foo->foo; is $out, "before_foo\nfoo\nafter_foo\nafter_foo2\n"; } ok( Foo->add_trigger( name => 'after_foo', callback => sub { print ref $_[0] } ), 'add_trigger in Foo' ); { tie *STDOUT, 'IO::Scalar', \my $out; $foo->foo; is $out, "before_foo\nfoo\nafter_foo\nafter_foo2\nFoo", 'class name'; } ok( Foo->add_trigger( name => 'after_foo', callback => sub { print "\ngets_here"; return 'YAY'; }, abortable => 1 ), 'add_trigger in Foo' ); { tie *STDOUT, 'IO::Scalar', \my $out; is( $foo->foo, 4, "Success returned" ); is_deeply ($foo->last_trigger_results->[-1], ['YAY']); is $out, "before_foo\nfoo\nafter_foo\nafter_foo2\nFoo\ngets_here", 'class name'; } ok( Foo->add_trigger( name => 'after_foo', callback => sub { print "\nstopping_after"; return undef; }, abortable => 1 ), 'add_trigger in Foo' ); ok( Foo->add_trigger( name => 'after_foo', callback => sub { print "should not get here" } ), 'add_trigger in Foo' ); { tie *STDOUT, 'IO::Scalar', \my $out; is( $foo->foo, undef, "The lat thing we ran was 'stopping_after', then returned failure " ); is $out, "before_foo\nfoo\nafter_foo\nafter_foo2\nFoo\ngets_here\nstopping_after", 'class name'; unlike( $out, qr/should not get here/ ); } # coverage tests { # pass a non-code ref and catch the carp my @die; eval { local $SIG{__DIE__} = sub { push @die, @_ }; Foo->add_trigger( name => 'wrong_type', callback => [] ); }; like( pop(@die), qr(add_trigger[(][)] needs coderef at ), 'check for right callback param' ); }