Class-Container-0.13/000755 000765 000024 00000000000 13176773246 014362 5ustar00kenstaff000000 000000 Class-Container-0.13/Build.PL000644 000765 000024 00000002210 13176773246 015651 0ustar00kenstaff000000 000000 use strict; use warnings; use Module::Build 0.3601; my %module_build_args = ( "build_requires" => { "Module::Build" => "0.3601" }, "configure_requires" => { "ExtUtils::MakeMaker" => "6.30", "Module::Build" => "0.3601" }, "dist_abstract" => "Glues object frameworks together transparently", "dist_author" => [ "Ken Williams " ], "dist_name" => "Class-Container", "dist_version" => "0.13", "license" => "perl", "module_name" => "Class::Container", "recommends" => {}, "recursive_test_files" => 1, "requires" => { "B::Deparse" => 0, "Carp" => 0, "Params::Validate" => 0, "Scalar::Util" => 0, "strict" => 0, "vars" => 0 }, "script_files" => [], "test_requires" => { "File::Spec" => 0, "Test" => 0 } ); my %fallback_build_requires = ( "File::Spec" => 0, "Module::Build" => "0.3601", "Test" => 0 ); unless ( eval { Module::Build->VERSION(0.4004) } ) { delete $module_build_args{test_requires}; $module_build_args{build_requires} = \%fallback_build_requires; } my $build = Module::Build->new(%module_build_args); $build->create_build_script; Class-Container-0.13/Changes000644 000765 000024 00000017016 13176773246 015662 0ustar00kenstaff000000 000000 Revision history for Perl extension Class::Container: 0.13 Thu Nov 2 23:53:03 CDT 2017 - Fix tests failing on Perl 5.26 w/o '.' in @INC [Kent Fredric] - A bit of Perl::Critic compliance - Convert to Dist::Zilla - Convert to git 0.12 Sun Jan 23 21:02:35 CST 2005 - Fixed a bug in the container() method, which was only returning valid results for delayed objects, not auto-created ones. [Spotted by Sebastian Willert] 0.11 Wed Mar 3 21:34:51 CST 2004 - Fixed a bug in the code that detects whether Scalar::Util is loadable. [Spotted by Michael Alan Dorman] 0.10 Thu Mar 6 16:06:47 CST 2003 - The dump_parameters() method will now output the default values (if any) of parameters that haven't been explicitly set. 0.09 Mon Feb 10 13:12:40 CST 2003 - Use Carp::croak() instead of die() in most places when throwing a fatal error. - Fixed a problem in dump_parameters() in which attributes from superclasses weren't getting dumped. - The valid_params() method now always returns a hashref, possibly empty (when initialization hasn't happened yet), instead of sometimes returning undef. - Added experimental support for "decorator" classes, via the decorates() method. - Fixed the credits in the AUTHOR section to better reflect reality. - Added a Module::Build-style Build.PL script for installation. 0.08 Thu Aug 29 17:11:20 EST 2002 - Added the dump_parameters() method, which returns a hash reference containing a set of parameters that should be sufficient to re-create the given object using its class's C method (under normal/simple circumstances). 0.07 Tue Jul 23 00:34:34 PDT 2002 - Fix a bug in passing contained objects rather than using the defaultly created one. [found by Ilya Martynov ] - Calling container() when Scalar::Util is not installed now triggers a fatal error instead of returning undef - Get rid off the %ALLOWED_CACHE memoization, since it wasn't working properly. It could be done, but not as easily, so maybe it's a future project. [consistent prodding by Dave Rolsky] - Replace guts of get_contained_object_spec() and validation_spec() with an _iterate_ISA() internal method. This fixes a bug in validation_spec() in which subclasses weren't overriding superclass validation_spec()s. - Make valid_params() a standard get/set accessor method. - Document that valid_params() should only be called as a class method, not object method. - Improve the output of the show_containers() method. - Calling contained_objects() twice on the same package wasn't properly clearing previous entries. 0.06 Wed Jul 17 17:06:10 EST 2002 - Memoize the get_contained_object_spec(), validation_spec(), and allowed_params() methods. This can give a big speed boost when methods are called repeatedly, for example when using factory methods. All memoization caches are cleared when valid_params() or contained_objects() is called. The only known pitfall in the caching is that a class that dynamically changes its @ISA will probably mess things up. Idea by Dave Rolsky. - Use 'scalar validate_with()' inside new(), which may be faster. Idea by Dave Rolsky. - short-circuit create_contained_objects() if there are no contained objects to create. Idea by Dave Rolsky. - return a reference from create_contained_objects() rather than a list of key/value pairs. This lets us pass it directly to validation routines. - Use the qr// format for regexes in the all_specs() method. - Improve the docs for allowed_params() - it's a class method (not an instance method), and it accepts a list of arguments that can affect the return value. - Don't copy as many hashes internally. Pass by reference. - Fixed some POD formatting problems. - Now requires Params::Validate version 0.23 0.05 Thu Jun 27 16:53:41 EST 2002 - Fixed a problem in create_contained_objects() in which a 'foo_class' parameter wouldn't get properly passsed to all the contained objects that needed to see it. - Added a documentation section "Scenario" explaining the main benefits of using the module. - Improved the output of show_containers(), notably the names of delayed classes - Merge $self->{container}{delayed} into $self->{container}{contained}, with a 'delayed' property. This allows simplification of the rest of the code in several places, notably the show_containers() routine. - Simplify show_containers() a little, and make it more accurate on contained objects' classes - Fixed a doc error in the first example - Added a bit in the first doc paragraph, saying that any of the Mason objects can be replaced by a subclass. - Added an internal comment about the strategy inside the allowed_params method. - Simplified the allowed_params method internally. - Got rid of special-casing to check for circular containment relationships. This seemed to have been added for HTML::Mason, but all Mason tests (as well as all Class::Container tests) still pass when I remove it. 0.04 Wed Jun 26 19:15:26 EST 2002 - Add the show_containers() method, which should be a godsend during debugging. - Convert contained_objects() string specs to hashes upon input, rather than checking them every time they're used later in the code. - Change " if (%args)" to " if (keys %args)", which is more officially correct (though both would work in this particular case). 0.03 Fri Jun 21 17:44:37 EST 2002 - Subclasses can now override contained_objects settings of their superclass (previously it was backwards). - Let call_method() accept arbitrary additional parameters, don't force them into a hash. - Added contained_class() method. - Use new contained_class() method inside call_method(). - delayed_object_class() shouldn't be settable, it'll mess up the parameters accepted. - The 'container' parameter shouldn't be shared among containers the way other parameters are. - Made create_delayed_object() a little more efficient by not shifting things off @_ - just pass @_ to the next new() method. - Don't check for $contained_class->can('allowed_params'), check for $contained_class->isa(__PACKAGE__). - Clarified a few error messages. - Clarified documentation and removed a couple of doc errors. 0.02 Wed Jun 19 10:52:48 AEST 2002 - Made Scalar::Util a little more optional - the container() method is just a no-op if it's not around. - Use Params::Validate 0.18 new validate_with() method to set a meaningful subroutine name in error messages - Added the delayed_object_class() method - Documented how delayed objects are declared and created [Dave Rolsky] - Added some tests for the above stuff - Various documentation spruce-ups 0.01_05 Fri May 10 15:29:46 AEST 2002 - If a container has two contained classes that both need to see a parameter of the same name, it will now be passed to both. Previously it was passed to one of them, randomly. - Added 2 tests for the above. - Added 4 tests to make sure class names can be properly overridden. - Got rid of _make_contained_object() method. - Changed the (undocumented) get_contained_objects() method to get_contained_object_spec(). - Added the container() method, to get a reference to the thingy that created you. Uses weak references if Scalar::Util is available. - Consolidated all Container metadata in $self->{container} (subject to change to {_container} or something). - Added call_method() method. 0.01 Wed Mar 20 19:33:40 2002 - original version, based on HTML::Mason::Container Class-Container-0.13/cpanfile000644 000765 000024 00000000655 13176773246 016074 0ustar00kenstaff000000 000000 requires "B::Deparse" => "0"; requires "Carp" => "0"; requires "Params::Validate" => "0"; requires "Scalar::Util" => "0"; requires "strict" => "0"; requires "vars" => "0"; on 'build' => sub { requires "Module::Build" => "0.3601"; }; on 'test' => sub { requires "File::Spec" => "0"; requires "Test" => "0"; }; on 'configure' => sub { requires "ExtUtils::MakeMaker" => "6.30"; requires "Module::Build" => "0.3601"; }; Class-Container-0.13/dist.ini000644 000765 000024 00000001417 13176773246 016031 0ustar00kenstaff000000 000000 name = Class-Container version = 0.13 author = Ken Williams license = Perl_5 copyright_holder = Ken Williams [GatherDir] exclude_filename = cpanfile [PruneCruft] [ManifestSkip] [MetaYAML] [License] [Readme] [ExtraTests] [ExecDir] [ShareDir] [MakeMaker] [Manifest] [TestRelease] [ConfirmRelease] [UploadToCPAN] [PkgVersion] [PodVersion] [PruneFiles] match = ~$ match = ^Class-Container [Signature] [Bugtracker] web = http://github.com/kenahoo/%s/issues [Repository] [ModuleBuild] [InstallGuide] [Test::Perl::Critic] ;[PodCoverageTests] [AutoPrereqs] skip = ^English$ [Git::Tag] ;; TravisCI integration, see ;; http://blogs.perl.org/users/neilb/2014/08/try-travis-ci-with-your-cpan-distributions.html [CPANFile] [CopyFilesFromBuild] copy = cpanfile Class-Container-0.13/INSTALL000644 000765 000024 00000001730 13176773246 015414 0ustar00kenstaff000000 000000 This is the Perl distribution Class-Container. Installing Class-Container is straightforward. ## Installation with cpanm If you have cpanm, you only need one line: % cpanm Class::Container If you are installing into a system-wide directory, you may need to pass the "-S" flag to cpanm, which uses sudo to install the module: % cpanm -S Class::Container ## Installing with the CPAN shell Alternatively, if your CPAN shell is set up, you should just be able to do: % cpan Class::Container ## Manual installation As a last resort, you can manually install it. Download the tarball, untar it, then build it: % perl Build.PL % ./Build && ./Build test Then install it: % ./Build install If you are installing into a system-wide directory, you may need to run: % sudo ./Build install ## Documentation Class-Container documentation is available as POD. You can run perldoc from a shell to read the documentation: % perldoc Class::Container Class-Container-0.13/lib/000755 000765 000024 00000000000 13176773246 015130 5ustar00kenstaff000000 000000 Class-Container-0.13/LICENSE000644 000765 000024 00000043653 13176773246 015402 0ustar00kenstaff000000 000000 This software is copyright (c) 2017 by Ken Williams. 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) 2017 by Ken Williams. 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, Suite 500, Boston, MA 02110-1335 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) 2017 by Ken Williams. 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 Class-Container-0.13/Makefile.PL000644 000765 000024 00000002457 13176773246 016344 0ustar00kenstaff000000 000000 use strict; use warnings; use ExtUtils::MakeMaker 6.30; my %WriteMakefileArgs = ( "ABSTRACT" => "Glues object frameworks together transparently", "AUTHOR" => "Ken Williams ", "BUILD_REQUIRES" => { "Module::Build" => "0.3601" }, "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => "6.30", "Module::Build" => "0.3601" }, "DISTNAME" => "Class-Container", "EXE_FILES" => [], "LICENSE" => "perl", "NAME" => "Class::Container", "PREREQ_PM" => { "B::Deparse" => 0, "Carp" => 0, "Params::Validate" => 0, "Scalar::Util" => 0, "strict" => 0, "vars" => 0 }, "TEST_REQUIRES" => { "File::Spec" => 0, "Test" => 0 }, "VERSION" => "0.13", "test" => { "TESTS" => "t/*.t" } ); my %FallbackPrereqs = ( "B::Deparse" => 0, "Carp" => 0, "File::Spec" => 0, "Module::Build" => "0.3601", "Params::Validate" => 0, "Scalar::Util" => 0, "Test" => 0, "strict" => 0, "vars" => 0 ); 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); Class-Container-0.13/MANIFEST000644 000765 000024 00000000266 13176773246 015517 0ustar00kenstaff000000 000000 Build.PL Changes INSTALL LICENSE MANIFEST META.yml Makefile.PL README SIGNATURE cpanfile dist.ini lib/Class/Container.pm t/01-basic.t t/02-decorator.t t/author-critic.t t/classes.pl Class-Container-0.13/META.yml000644 000765 000024 00000001317 13176773246 015635 0ustar00kenstaff000000 000000 --- abstract: 'Glues object frameworks together transparently' author: - 'Ken Williams ' build_requires: File::Spec: 0 Module::Build: 0.3601 Test: 0 configure_requires: ExtUtils::MakeMaker: 6.30 Module::Build: 0.3601 dynamic_config: 0 generated_by: 'Dist::Zilla version 5.006, CPAN::Meta::Converter version 2.150001' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: Class-Container requires: B::Deparse: 0 Carp: 0 Params::Validate: 0 Scalar::Util: 0 strict: 0 vars: 0 resources: bugtracker: http://github.com/kenahoo/Class-Container/issues repository: git://github.com/kenahoo/Class-Container.git version: 0.13 Class-Container-0.13/README000644 000765 000024 00000000472 13176773246 015245 0ustar00kenstaff000000 000000 This archive contains the distribution Class-Container, version 0.13: Glues object frameworks together transparently This software is copyright (c) 2017 by Ken Williams. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Class-Container-0.13/SIGNATURE000644 000765 000024 00000003137 13176773246 015652 0ustar00kenstaff000000 000000 This file contains message digests of all files listed in MANIFEST, signed via the Module::Signature module, version 0.73. To verify the content in this distribution, first make sure you have Module::Signature installed, then type: % cpansign -v It will check each file's integrity, as well as the signature's validity. If "==> Signature verified OK! <==" is not displayed, the distribution may already have been compromised, and you should not run its Makefile.PL or Build.PL. -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 SHA1 a1ba286c01e42e90f41ad222897698b1a4a4f2ac Build.PL SHA1 59e86d738663b3ee65ef269e1174de943b0d8d92 Changes SHA1 c787d88b3935853f48f6204a1d7ef463694702f5 INSTALL SHA1 87d1d7d8be19f13b0ee779177fa03fcca3e2b628 LICENSE SHA1 a35434bea6cb6de976b09fa2bacc05ecd9e6a3a7 MANIFEST SHA1 dd611cdd49745ae9a2fc09034014b69b48f05046 META.yml SHA1 cbcd8951f75b2f97add6578140cfcca486af081a Makefile.PL SHA1 34c04c1651dbc6a26fa785bec8b08390b5acdfc2 README SHA1 feac03c41fd721eda9d2865be041f66d59fecb8a cpanfile SHA1 b5f4a8e152959e7d391fa2508620adfddb98c21e dist.ini SHA1 cf40f42c3313085bf17db0b4e4f3ad071f03a705 lib/Class/Container.pm SHA1 a7f3e0732c4a3d45affd6a4f5e95a11c15674789 t/01-basic.t SHA1 64e7e9420276e05e85f05b1b17b85d55a630a7a9 t/02-decorator.t SHA1 fa45d6e6ab1cd421349dea4ef527bfd5cdc8a09e t/author-critic.t SHA1 56e282a6a8379f3a3a42d5f78cd1682ab1cfc68b t/classes.pl -----BEGIN PGP SIGNATURE----- Comment: GPGTools - http://gpgtools.org iF0EARECAB0WIQSty9sFtA6yoqVVVGKCu8wEt++UdgUCWfv2ogAKCRCCu8wEt++U dtZDAKCm0N97e/ebIvg7dmDcE+kO6l+fJwCfYbC+xwjbS7NzKBJOmq7nLJwi1eA= =zO1B -----END PGP SIGNATURE----- Class-Container-0.13/t/000755 000765 000024 00000000000 13176773246 014625 5ustar00kenstaff000000 000000 Class-Container-0.13/t/01-basic.t000644 000765 000024 00000021127 13176773246 016314 0ustar00kenstaff000000 000000 #!/usr/bin/perl -w # Note - I create a bunch of classes in these tests and then change # their valid_params() and contained_objects() lists several times. # This isn't really supported behavior of this module, but it's # necessary to do it in the tests. use strict; use Test; use Class::Container; use Params::Validate qw(:types); use File::Spec; require File::Spec->rel2abs(File::Spec->catfile('t', 'classes.pl')); my $HAVE_WEAKEN = 0 + exists $INC{'Scalar/Util.pm'}; plan tests => 67 + 1*$HAVE_WEAKEN; use Carp; $SIG{__DIE__} = \&Carp::confess; eval {new Daughter(hair => 'long')}; ok $@, '', "Try making an object"; eval {new Parent()}; ok $@, '/mood/', "Should fail, missing required parameter"; my %args = (parent_val => 7, mood => 'bubbly'); eval {new Parent(%args)}; ok $@, '', "Try creating top-level object"; my $mood = eval {Parent->new(%args)->{son}->{mood}}; ok $mood, 'bubbly'; ok $@, '', "Make sure sub-objects are created with proper values"; if ($HAVE_WEAKEN) { my $p = Parent->new(%args); ok $p->{son}->container, $p, "Container of son should be parent"; } eval {my $p = new Parent(%args); $p->create_delayed_object('daughter')}; ok $@, '', "Create a delayed object"; my $d = eval {Parent->new(%args)->create_delayed_object('daughter', hair => 'short')}; ok $@, '', "Create a delayed object with parameters"; ok $d->{hair}, 'short', "Make sure parameters are propogated to delayed object"; eval {new Daughter(foo => 'invalid')}; ok $@, '/Daughter/', "Make sure error messages contain the name of the class"; # Make sure we can override class names { ok my $p = eval {new Parent(mood => 'foo', parent_val => 1, daughter_class => 'StepDaughter', toy_class => 'Ball', other_toys_class => 'Streamer', son_class => 'StepSon')}; warn $@ if $@; my $d = eval {$p->create_delayed_object('daughter')}; ok $@, ''; ok ref($d), 'StepDaughter'; ok ref($p->{son}), 'StepSon'; # Note - if one of these fails and the other succeeds, then we're # not properly passing 'toy_class' to both son & daughter classes. ok ref($d->{toy}), 'Ball'; ok ref($p->{son}{toy}), 'Ball'; ok $d->delayed_object_class('other_toys'), 'Streamer'; ok $p->{son}->delayed_object_class('other_toys'), 'Streamer'; # Special 'container' parameter shouldn't be shared among objects ok ($p->{container} ne $p->{son}{container}); # Check some of the formatting of show_containers() my $string = $p->show_containers; ok $string, '/\n son -> StepSon/', $string; } { # Check that subclass contained_objects override superclass local @Superclass::ISA = qw(Class::Container); local @Subclass::ISA = qw(Superclass); 'Superclass'->valid_params( foo => {isa => 'Foo'} ); 'Subclass'->valid_params( foo => {isa => 'Bar'} ); 'Superclass'->contained_objects( foo => 'Foo' ); 'Subclass'->contained_objects( foo => 'Bar' ); local @Bar::ISA = qw(Foo); sub Foo::new { bless {}, 'Foo' } sub Bar::new { bless {}, 'Bar' } my $child = 'Subclass'->new; ok ref($child->{foo}), 'Bar', 'Subclass contained_object should override superclass'; my $spec = 'Subclass'->validation_spec; ok $spec->{foo}{isa}, 'Bar'; } { local @Top::ISA = qw(Class::Container); 'Top'->valid_params( document => {isa => 'Document'} ); 'Top'->contained_objects( document => 'Document', collection => {class => 'Collection', delayed => 1} ); local @Collection::ISA = qw(Class::Container); 'Collection'->contained_objects( document => {class => 'Document', delayed => 1} ); local @Document::ISA = qw(Class::Container); local @Document2::ISA = qw(Document); my $k = new Top; print $k->show_containers; ok $k->contained_class('document'), 'Document'; my $collection = $k->create_delayed_object('collection'); ok ref($collection), 'Collection'; ok $collection->contained_class('document'), 'Document'; my $string = $k->show_containers; ok $string, '/ collection -> Collection \(delayed\)/'; ok $string, '/ document -> Document \(delayed\)/'; my $k2 = new Top(document_class => 'Document2'); print $k2->show_containers; ok $k2->contained_class('document'), 'Document2'; my $collection2 = $k2->create_delayed_object('collection'); ok ref($collection2), 'Collection'; ok $collection2->contained_class('document'), 'Document2'; my $string2 = $k2->show_containers; ok $string2, '/ collection -> Collection \(delayed\)/'; ok $string2, '/ document -> Document2 \(delayed\)/'; } { local @Top::ISA = qw(Class::Container); 'Top'->valid_params( document => {isa => 'Document1'} ); 'Top'->contained_objects( document => 'Document1' ); my $contained = 'Top'->get_contained_object_spec; ok $contained->{document}; ok !$contained->{collection}; # Shouldn't have anything left over from the last block local @Document1::ISA = qw(Class::Container); 'Document1'->valid_params( doc1 => {type => SCALAR} ); local @Document2::ISA = qw(Class::Container); 'Document2'->valid_params( doc2 => {type => SCALAR} ); my $allowed = 'Top'->allowed_params(); ok $allowed->{doc1}; ok !$allowed->{doc2}; $allowed = 'Top'->allowed_params( document_class => 'Document2' ); ok $allowed->{doc2}; ok !$allowed->{doc1}; } { local @Top::ISA = qw(Class::Container); 'Top'->_expire_caches; 'Top'->valid_params( document => {isa => 'Document1'} ); 'Top'->contained_objects( document => 'Document1' ); local @Document1::ISA = qw(Class::Container); 'Document1'->valid_params(); local @Document2::ISA = qw(Document1); 'Document2'->valid_params(); my $t = new Top( document => bless {}, 'Document2' ); ok $t; ok ref($t->{document}), 'Document2'; } { local @Top::ISA = qw(Class::Container); 'Top'->valid_params( document => {isa => 'Document'} ); 'Top'->contained_objects( document => 'Document' ); local @Document::ISA = qw(Class::Container); 'Document'->valid_params( sub => {isa => 'Class::Container'} ); 'Document'->contained_objects( sub => 'Sub1' ); local @Sub1::ISA = qw(Class::Container); 'Sub1'->valid_params( bar => {type => SCALAR} ); 'Sub1'->contained_objects(); local @Sub2::ISA = qw(Class::Container); 'Sub2'->valid_params( foo => {type => SCALAR} ); 'Sub2'->contained_objects(); my $allowed = 'Top'->allowed_params(); ok $allowed->{document}; ok $allowed->{bar}; ok !$allowed->{foo}; $allowed = 'Top'->allowed_params(sub_class => 'Sub2'); ok $allowed->{document}; ok !$allowed->{bar}; ok $allowed->{foo}; } { local @Top::ISA = qw(Class::Container); Top->valid_params(foo => {type => SCALAR}); Top->contained_objects(); ok 'Top'->valid_params; ok 'Top'->valid_params->{foo}{type}, SCALAR; } { local @Top::ISA = qw(Class::Container); Top->valid_params(foo => {type => SCALAR}, child => {isa => 'Child'}); Top->contained_objects(child => 'Child'); local @Child::ISA = qw(Class::Container); Child->valid_params(bar => {type => SCALAR}, grand_child => {isa => 'GrandChild'}); Child->contained_objects(grand_child => 'GrandChild'); local @GrandChild::ISA = qw(Class::Container); GrandChild->valid_params(baz => {type => SCALAR}, boo => {default => 5}); GrandChild->contained_objects(); local @GrandSibling::ISA = qw(GrandChild); my $dump = GrandSibling->new(baz => 'BAZ')->dump_parameters; ok keys(%$dump), 2; ok $dump->{baz}, 'BAZ', "Sibling has baz=BAZ"; ok $dump->{boo}, 5, "Sibling has boo=5"; $dump = Child->new(bar => 'BAR', baz => 'BAZ')->dump_parameters; ok keys(%$dump), 3; ok $dump->{bar}, 'BAR'; ok $dump->{baz}, 'BAZ'; $dump = Child->new(bar => 'BAR', baz => 'BAZ', grand_child_class => 'GrandChild')->dump_parameters; ok keys(%$dump), 3; ok $dump->{bar}, 'BAR'; ok $dump->{baz}, 'BAZ'; $dump = Top->new(foo => 'FOO', bar => 'BAR', baz => 'BAZ')->dump_parameters; ok keys(%$dump), 4; ok $dump->{foo}, 'FOO'; ok $dump->{bar}, 'BAR'; ok $dump->{baz}, 'BAZ'; # Test default values in a delayed object Top->valid_params(undef); Top->contained_objects(child => {class => 'Child', delayed => 1}); Child->valid_params(bar => {default => 4}); Child->contained_objects(); $dump = Top->new()->dump_parameters; ok keys(%$dump), 1; ok $dump->{bar}, 4; $dump = Top->new(bar => 6)->dump_parameters; ok keys(%$dump), 1; ok $dump->{bar}, 6; } { # Make sure a later call to valid_params() clears the param list local @Top::ISA = qw(Class::Container); Top->valid_params(undef); Top->contained_objects(); ok eval{ new Top }; } { # Make sure valid_params() gives sensible null output local @Nonexistent::ISA = qw(Class::Container); my $params = Nonexistent->valid_params; ok ref($params), 'HASH'; ok keys(%$params), 0; } Class-Container-0.13/t/02-decorator.t000644 000765 000024 00000003661 13176773246 017221 0ustar00kenstaff000000 000000 use strict; use Test; BEGIN { plan tests => 24 } use Class::Container; use Params::Validate qw(:types); use File::Spec; require File::Spec->rel2abs(File::Spec->catfile('t', 'classes.pl')); # Decorator stuff { local @Top::ISA = qw(Class::Container); Top->valid_params(undef); Top->contained_objects(); sub Top::foo { "foo" } local @Decorator::ISA = qw(Top); Decorator->decorates; sub Decorator::bar { "bar" } local @OtherDec::ISA = qw(Top); OtherDec->decorates; sub OtherDec::baz { "baz" } # Make sure a simple 1-level decorator works { my $d = new Decorator; ok $d; ok $d->foo, 'foo'; ok $d->bar, 'bar'; # Should be using simple subclassing since it's just 1 level (no interface for this) ok !$d->{_decorates}; # Make sure can() is correct # Test.pm will run subrefs (don't want that), so make them booleans ok !!$d->can('foo'); ok !!$d->can('bar'); ok !$d->can('baz'); } # Try a 2-level decorator { my $d = new Decorator(decorate_class => 'OtherDec'); ok $d; ok !!$d->can('foo'); ok !!$d->can('bar'); ok !!$d->can('baz'); ok $d->foo, 'foo'; ok $d->bar, 'bar'; ok $d->baz, 'baz'; # Make sure it's using decoration containment at top level, and subclassing below. ok $d->{_decorates}; ok ref($d->{_decorates}), 'OtherDec'; ok !$d->{_decorates}{_decorates}; } # Make sure arguments are passed correctly Top->valid_params( one => { type => SCALAR } ); Decorator->valid_params( two => { type => SCALAR } ); Top->decorates; Decorator->decorates; OtherDec->decorates; my $d = Decorator->new( one => 1, two => 2 ); ok $d; $d = OtherDec->new( decorate_class => 'Decorator', one => 1, two => 2 ); ok $d; ok $d->{one}, 1; ok $d->{_decorates}{two}, 2; $d = Decorator->new( decorate_class => 'OtherDec', one => 1, two => 2 ); ok $d; ok $d->{one}, 1; ok $d->{two}, 2; } Class-Container-0.13/t/author-critic.t000644 000765 000024 00000000666 13176773246 017577 0ustar00kenstaff000000 000000 #!perl BEGIN { unless ($ENV{AUTHOR_TESTING}) { require Test::More; Test::More::plan(skip_all => 'these tests are for testing by the author'); } } use strict; use warnings; use Test::More; use English qw(-no_match_vars); eval "use Test::Perl::Critic"; plan skip_all => 'Test::Perl::Critic required to criticise code' if $@; Test::Perl::Critic->import( -profile => "perlcritic.rc" ) if -e "perlcritic.rc"; all_critic_ok(); Class-Container-0.13/t/classes.pl000644 000765 000024 00000003050 13176773246 016615 0ustar00kenstaff000000 000000 use strict; use Params::Validate qw(:types); my $SCALAR = SCALAR; # So we don't have to keep importing it below # Create some boilerplate classes { no strict 'refs'; foreach my $class (qw(Parent Boy Toy Daughter)) { push @{$class.'::ISA'}, 'Class::Container'; } } # Define the relationships { package Parent; push @Parent::ISA, 'Foo'; # Make sure it works with non-container superclasses # Has one son and several daughters __PACKAGE__->valid_params( parent_val => { type => $SCALAR }, son => {isa => 'Son'}, ); __PACKAGE__->contained_objects( son => 'Son', daughter => {delayed => 1, class => 'Daughter'}); } { package Boy; __PACKAGE__->valid_params( eyes => { default => 'brown', type => $SCALAR }, toy => {isa => 'Toy'}); __PACKAGE__->contained_objects( toy => 'Slingshot', other_toys => {class => 'Toy', delayed => 1}, ); } { package Son; push @Son::ISA, 'Boy'; __PACKAGE__->valid_params( mood => { type => $SCALAR } ); } { package Slingshot; push @Slingshot::ISA, 'Toy'; __PACKAGE__->valid_params( weapon => { default => 'rock', type => $SCALAR } ); } { package Daughter; __PACKAGE__->valid_params( hair => { default => 'short' } ); } { package StepDaughter; push @StepDaughter::ISA, 'Daughter'; __PACKAGE__->valid_params( toy => {isa => 'Toy'} ); __PACKAGE__->contained_objects( toy => { class => 'Toy'}, other_toys => {class => 'Toy', delayed => 1}, ); } { push @StepSon::ISA, 'Son'; push @Ball::ISA, 'Toy'; push @Streamer::ISA, 'Toy'; } 1; Class-Container-0.13/lib/Class/000755 000765 000024 00000000000 13176773246 016175 5ustar00kenstaff000000 000000 Class-Container-0.13/lib/Class/Container.pm000644 000765 000024 00000070104 13176773246 020457 0ustar00kenstaff000000 000000 use strict; package Class::Container; { $Class::Container::VERSION = '0.13'; } my $HAVE_WEAKEN; BEGIN { eval { require Scalar::Util; Scalar::Util->import('weaken'); $HAVE_WEAKEN = 1; }; *weaken = sub {} unless defined &weaken; } use Carp; # The create_contained_objects() method lets one object # (e.g. Compiler) transparently create another (e.g. Lexer) by passing # creator parameters through to the created object. # # Any auto-created objects should be declared in a class's # %CONTAINED_OBJECTS hash. The keys of this hash are objects which # can be created and the values are the default classes to use. # For instance, the key 'lexer' indicates that a 'lexer' parameter # should be silently passed through, and a 'lexer_class' parameter # will trigger the creation of an object whose class is specified by # the value. If no value is present there, the value of 'lexer' in # the %CONTAINED_OBJECTS hash is used. If no value is present there, # no contained object is created. # # We return the list of parameters for the creator. If contained # objects were auto-created, their creation parameters aren't included # in the return value. This lets the creator be totally ignorant of # the creation parameters of any objects it creates. use Params::Validate qw(:all); Params::Validate::validation_options( on_fail => sub { die @_ } ); my %VALID_PARAMS = (); my %CONTAINED_OBJECTS = (); my %VALID_CACHE = (); my %CONTAINED_CACHE = (); my %DECORATEES = (); sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = bless scalar validate_with ( params => $class->create_contained_objects(@_), spec => $class->validation_spec, called => "$class->new()", ), $class; if ($HAVE_WEAKEN) { my $c = $self->get_contained_object_spec; foreach my $name (keys %$c) { next if $c->{$name}{delayed}; $self->{$name}{container}{container} = $self; weaken $self->{$name}{container}{container}; } } return $self; } sub all_specs { require B::Deparse; my %out; foreach my $class (sort keys %VALID_PARAMS) { my $params = $VALID_PARAMS{$class}; foreach my $name (sort keys %$params) { my $spec = $params->{$name}; my ($type, $default); if ($spec->{isa}) { my $obj_class; $type = 'object'; if (exists $CONTAINED_OBJECTS{$class}{$name}) { $default = "$CONTAINED_OBJECTS{$class}{$name}{class}->new"; } } else { ($type, $default) = ($spec->{parse}, $spec->{default}); } if (ref($default) eq 'CODE') { $default = 'sub ' . B::Deparse->new()->coderef2text($default); $default =~ s/\s+/ /g; } elsif (ref($default) eq 'ARRAY') { $default = '[' . join(', ', map "'$_'", @$default) . ']'; } elsif (ref($default) eq 'Regexp') { $type = 'regex'; $default =~ s,^\(\?(\w*)-\w*:(.*)\),/$2/$1,; $default = "qr$default"; } unless ($type) { # Guess from the validation spec $type = ($spec->{type} & ARRAYREF ? 'list' : $spec->{type} & SCALAR ? 'string' : $spec->{type} & CODEREF ? 'code' : $spec->{type} & HASHREF ? 'hash' : undef); # Oh well } my $descr = $spec->{descr} || '(No description available)'; $out{$class}{valid_params}{$name} = { type => $type, pv_type => $spec->{type}, default => $default, descr => $descr, required => defined $default || $spec->{optional} ? 0 : 1, public => exists $spec->{public} ? $spec->{public} : 1, }; } $out{$class}{contained_objects} = {}; next unless exists $CONTAINED_OBJECTS{$class}; my $contains = $CONTAINED_OBJECTS{$class}; foreach my $name (sort keys %$contains) { $out{$class}{contained_objects}{$name} = {map {$_, $contains->{$name}{$_}} qw(class delayed descr)}; } } return %out; } sub dump_parameters { my $self = shift; my $class = ref($self) || $self; my %params; foreach my $param (keys %{ $class->validation_spec }) { next if $param eq 'container'; my $spec = $class->validation_spec->{$param}; if (ref($self) and defined $self->{$param}) { $params{$param} = $self->{$param}; } else { $params{$param} = $spec->{default} if exists $spec->{default}; } } foreach my $name (keys %{ $class->get_contained_object_spec }) { next unless ref($self); my $contained = ($self->{container}{contained}{$name}{delayed} ? $self->delayed_object_class($name) : $params{$name}); my $subparams = UNIVERSAL::isa($contained, __PACKAGE__) ? $contained->dump_parameters : {}; my $more = $self->{container}{contained}{$name}{args} || {}; $subparams->{$_} = $more->{$_} foreach keys %$more; @params{ keys %$subparams } = values %$subparams; delete $params{$name}; } return \%params; } sub show_containers { my $self = shift; my $name = shift; my %args = (indent => '', @_); $name = defined($name) ? "$name -> " : ""; my $out = "$args{indent}$name$self"; $out .= " (delayed)" if $args{delayed}; $out .= "\n"; return $out unless $self->isa(__PACKAGE__); my $specs = ref($self) ? $self->{container}{contained} : $self->get_contained_object_spec; while (my ($name, $spec) = each %$specs) { my $class = $args{args}{"${name}_class"} || $spec->{class}; $self->_load_module($class); if ($class->isa(__PACKAGE__)) { $out .= $class->show_containers($name, indent => "$args{indent} ", args => $spec->{args}, delayed => $spec->{delayed}); } else { $out .= "$args{indent} $name -> $class\n"; } } return $out; } sub _expire_caches { %VALID_CACHE = %CONTAINED_CACHE = (); } sub valid_params { my $class = shift; if (@_) { $class->_expire_caches; $VALID_PARAMS{$class} = @_ == 1 && !defined($_[0]) ? {} : {@_}; } return $VALID_PARAMS{$class} ||= {}; } sub contained_objects { my $class = shift; $class->_expire_caches; $CONTAINED_OBJECTS{$class} = {}; while (@_) { my ($name, $spec) = (shift, shift); $CONTAINED_OBJECTS{$class}{$name} = ref($spec) ? $spec : { class => $spec }; } } sub _decorator_AUTOLOAD { my $self = shift; use vars qw($AUTOLOAD); my ($method) = $AUTOLOAD =~ /([^:]+)$/; return if $method eq 'DESTROY'; die qq{Can't locate object method "$method" via package $self} unless ref($self); my $subr = $self->{_decorates}->can($method) or die qq{Can't locate object method "$method" via package } . ref($self); unshift @_, $self->{_decorates}; goto $subr; } sub _decorator_CAN { my ($self, $method) = @_; return $self->SUPER::can($method) if $self->SUPER::can($method); if (ref $self) { return $self->{_decorates}->can($method) if $self->{_decorates}; return; } else { return $DECORATEES{$self}->can($method); } } sub decorates { my ($class, $super) = @_; no strict 'refs'; ## no critic $super ||= ${$class . '::ISA'}[0]; # Pass through unknown method invocations *{$class . '::AUTOLOAD'} = \&_decorator_AUTOLOAD; *{$class . '::can'} = \&_decorator_CAN; $DECORATEES{$class} = $super; $VALID_PARAMS{$class}{_decorates} = { isa => $super, optional => 1 }; } sub container { my $self = shift; die "The ", ref($self), "->container() method requires installation of Scalar::Util" unless $HAVE_WEAKEN; return $self->{container}{container}; } sub call_method { my ($self, $name, $method, @args) = @_; my $class = $self->contained_class($name) or die "Unknown contained item '$name'"; $self->_load_module($class); return $class->$method( %{ $self->{container}{contained}{$name}{args} }, @args ); } # Accepts a list of key-value pairs as parameters, representing all # parameters taken by this object and its descendants. Returns a list # of key-value pairs representing *only* this object's parameters. sub create_contained_objects { # Typically $self doesn't exist yet, $_[0] is a string classname my $class = shift; my $c = $class->get_contained_object_spec; return {@_, container => {}} unless %$c or $DECORATEES{$class}; my %args = @_; if ($DECORATEES{$class}) { # Fix format $args{decorate_class} = [$args{decorate_class}] if $args{decorate_class} and !ref($args{decorate_class}); # Figure out which class to decorate my $decorate; if (my $c = $args{decorate_class}) { $decorate = @$c ? shift @$c : undef; delete $args{decorate_class} unless @$c; } $c->{_decorates} = { class => $decorate } if $decorate; } # This one is special, don't pass to descendants my $container_stuff = delete($args{container}) || {}; keys %$c; # Reset the iterator - why can't I do this in get_contained_object_spec?? my %contained_args; my %to_create; while (my ($name, $spec) = each %$c) { # Figure out exactly which class to make an object of my ($contained_class, $c_args) = $class->_get_contained_args($name, \%args); @contained_args{ keys %$c_args } = (); # Populate with keys $to_create{$name} = { class => $contained_class, args => $c_args }; } while (my ($name, $spec) = each %$c) { # This delete() needs to be outside the previous loop, because # multiple contained objects might need to see it delete $args{"${name}_class"}; if ($spec->{delayed}) { $container_stuff->{contained}{$name} = $to_create{$name}; $container_stuff->{contained}{$name}{delayed} = 1; } else { $args{$name} ||= $to_create{$name}{class}->new(%{$to_create{$name}{args}}); $container_stuff->{contained}{$name}{class} = ref $args{$name}; } } # Delete things that we're not going to use - things that are in # our contained object specs but not in ours. my $my_spec = $class->validation_spec; delete @args{ grep {!exists $my_spec->{$_}} keys %contained_args }; delete $c->{_decorates} if $DECORATEES{$class}; $args{container} = $container_stuff; return \%args; } sub create_delayed_object { my ($self, $name) = (shift, shift); croak "Unknown delayed item '$name'" unless $self->{container}{contained}{$name}{delayed}; if ($HAVE_WEAKEN) { push @_, container => {container => $self}; weaken $_[-1]->{container}; } return $self->call_method($name, 'new', @_); } sub delayed_object_class { my $self = shift; my $name = shift; croak "Unknown delayed item '$name'" unless $self->{container}{contained}{$name}{delayed}; return $self->{container}{contained}{$name}{class}; } sub contained_class { my ($self, $name) = @_; croak "Unknown contained item '$name'" unless my $spec = $self->{container}{contained}{$name}; return $spec->{class}; } sub delayed_object_params { my ($self, $name) = (shift, shift); croak "Unknown delayed object '$name'" unless $self->{container}{contained}{$name}{delayed}; if (@_ == 1) { return $self->{container}{contained}{$name}{args}{$_[0]}; } my %args = @_; if (keys %args) { @{ $self->{container}{contained}{$name}{args} }{ keys %args } = values %args; } return %{ $self->{container}{contained}{$name}{args} }; } # Everything the specified contained object will accept, including # parameters it will pass on to its own contained objects. sub _get_contained_args { my ($class, $name, $args) = @_; my $spec = $class->get_contained_object_spec->{$name} or croak "Unknown contained object '$name'"; my $contained_class = $args->{"${name}_class"} || $spec->{class}; croak "Invalid class name '$contained_class'" unless $contained_class =~ /^[\w:]+$/; $class->_load_module($contained_class); return ($contained_class, {}) unless $contained_class->isa(__PACKAGE__); my $allowed = $contained_class->allowed_params($args); my %contained_args; foreach (keys %$allowed) { $contained_args{$_} = $args->{$_} if exists $args->{$_}; } return ($contained_class, \%contained_args); } sub _load_module { my ($self, $module) = @_; unless ( eval { $module->can('new') } ) { no strict 'refs'; ## no critic eval "use $module"; ## no critic croak $@ if $@; } } sub allowed_params { my $class = shift; my $args = ref($_[0]) ? shift : {@_}; # Strategy: the allowed_params of this class consists of the # validation_spec of this class, merged with the allowed_params of # all contained classes. The specific contained classes may be # affected by arguments passed in, like 'interp' or # 'interp_class'. A parameter like 'interp' doesn't add anything # to our allowed_params (because it's already created) but # 'interp_class' does. my $c = $class->get_contained_object_spec; my %p = %{ $class->validation_spec }; foreach my $name (keys %$c) { # Can accept a 'foo' parameter - should already be in the validation_spec. # Also, its creation parameters should already have been extracted from $args, # so don't extract any parameters. next if exists $args->{$name}; # Figure out what class to use for this contained item my $contained_class; if ( exists $args->{"${name}_class"} ) { $contained_class = $args->{"${name}_class"}; $p{"${name}_class"} = { type => SCALAR }; # Add to spec } else { $contained_class = $c->{$name}{class}; } # We have to make sure it is loaded before we try calling allowed_params() $class->_load_module($contained_class); next unless $contained_class->can('allowed_params'); my $subparams = $contained_class->allowed_params($args); foreach (keys %$subparams) { $p{$_} ||= $subparams->{$_}; } } return \%p; } sub _iterate_ISA { my ($class, $look_in, $cache_in, $add) = @_; return $cache_in->{$class} if $cache_in->{$class}; my %out; no strict 'refs'; ## no critic foreach my $superclass (@{ "${class}::ISA" }) { next unless $superclass->isa(__PACKAGE__); my $superparams = $superclass->_iterate_ISA($look_in, $cache_in, $add); @out{keys %$superparams} = values %$superparams; } if (my $x = $look_in->{$class}) { @out{keys %$x} = values %$x; } @out{keys %$add} = values %$add if $add; return $cache_in->{$class} = \%out; } sub get_contained_object_spec { return (ref($_[0]) || $_[0])->_iterate_ISA(\%CONTAINED_OBJECTS, \%CONTAINED_CACHE); } sub validation_spec { return (ref($_[0]) || $_[0])->_iterate_ISA(\%VALID_PARAMS, \%VALID_CACHE, { container => {type => HASHREF} }); } 1; __END__ =head1 NAME Class::Container - Glues object frameworks together transparently =head1 VERSION version 0.13 =head1 SYNOPSIS package Car; use Class::Container; @ISA = qw(Class::Container); __PACKAGE__->valid_params ( paint => {default => 'burgundy'}, style => {default => 'coupe'}, windshield => {isa => 'Glass'}, radio => {isa => 'Audio::Device'}, ); __PACKAGE__->contained_objects ( windshield => 'Glass::Shatterproof', wheel => { class => 'Vehicle::Wheel', delayed => 1 }, radio => 'Audio::MP3', ); sub new { my $package = shift; # 'windshield' and 'radio' objects are created automatically by # SUPER::new() my $self = $package->SUPER::new(@_); $self->{right_wheel} = $self->create_delayed_object('wheel'); ... do any more initialization here ... return $self; } =head1 DESCRIPTION This class facilitates building frameworks of several classes that inter-operate. It was first designed and built for C, in which the Compiler, Lexer, Interpreter, Resolver, Component, Buffer, and several other objects must create each other transparently, passing the appropriate parameters to the right class, possibly substituting other subclasses for any of these objects. The main features of C are: =over 4 =item * Explicit declaration of containment relationships (aggregation, factory creation, etc.) =item * Declaration of constructor parameters accepted by each member in a class framework =item * Transparent passing of constructor parameters to the class that needs them =item * Ability to create one (automatic) or many (manual) contained objects automatically and transparently =back =head2 Scenario Suppose you've got a class called C, which contains an object of the class C, which in turn contains an object of the class C. Each class creates the object that it contains. Each class also accepts a set of named parameters in its C method. Without using C, C will have to know all the parameters that C takes, and C will have to know all the parameters that C takes. And some of the parameters accepted by C will really control aspects of C or C. Likewise, some of the parameters accepted by C will really control aspects of C. So, what happens when you decide you want to use a C class instead of the generic C? C and C must be modified accordingly, so that any additional parameters taken by C can be accommodated. This is a pain - the kind of pain that object-oriented programming was supposed to shield us from. Now, how can C help? Using C, each class (C, C, and C) will declare what arguments they take, and declare their relationships to the other classes (C creates/contains a C, and C creates/contains a C). Then, when you create a C object, you can pass C<< Parent->new() >> all the parameters for all three classes, and they will trickle down to the right places. Furthermore, C and C won't have to know anything about the parameters of its contained objects. And finally, if you replace C with C, no changes to C or C will likely be necessary. =head1 METHODS =head2 new() Any class that inherits from C should also inherit its C method. You can do this simply by omitting it in your class, or by calling C as indicated in the SYNOPSIS. The C method ensures that the proper parameters and objects are passed to the proper constructor methods. At the moment, the only possible constructor method is C. If you need to create other constructor methods, they should call C internally. =head2 __PACKAGE__->contained_objects() This class method is used to register what other objects, if any, a given class creates. It is called with a hash whose keys are the parameter names that the contained class's constructor accepts, and whose values are the default class to create an object of. For example, consider the C class, which uses the following code: __PACKAGE__->contained_objects( lexer => 'HTML::Mason::Lexer' ); This defines the relationship between the C class and the class it creates to go in its C slot. The C class "has a" C. The C<< HTML::Mason::Compiler->new() >> method will accept a C parameter and, if no such parameter is given, an object of the C class should be constructed. We implement a bit of magic here, so that if C<< HTML::Mason::Compiler->new() >> is called with a C parameter, it will load the indicated class (presumably a subclass of C), instantiate a new object of that class, and use it for the Compiler's C object. We're also smart enough to notice if parameters given to C<< HTML::Mason::Compiler->new() >> actually should go to the C contained object, and it will make sure that they get passed along. Furthermore, an object may be declared as "delayed", which means that an object I be created when its containing class is constructed. Instead, these objects will be created "on demand", potentially more than once. The constructors will still enjoy the automatic passing of parameters to the correct class. See the C for more. To declare an object as "delayed", call this method like this: __PACKAGE__->contained_objects( train => { class => 'Big::Train', delayed => 1 } ); =head2 __PACKAGE__->valid_params(...) Specifies the parameters accepted by this class's C method as a set of key/value pairs. Any parameters accepted by a superclass/subclass will also be accepted, as well as any parameters accepted by contained objects. This method is a get/set accessor method, so it returns a reference to a hash of these key/value pairs. As a special case, if you wish to set the valid params to an empty set and you previously set it to a non-empty set, you may call C<< __PACKAGE__->valid_params(undef) >>. C is called with a hash that contains parameter names as its keys and validation specifications as values. This validation specification is largely the same as that used by the C module, because we use C internally. As an example, consider the following situation: use Class::Container; use Params::Validate qw(:types); __PACKAGE__->valid_params ( allow_globals => { type => ARRAYREF, parse => 'list', default => [] }, default_escape_flags => { type => SCALAR, parse => 'string', default => '' }, lexer => { isa => 'HTML::Mason::Lexer' }, preprocess => { type => CODEREF, parse => 'code', optional => 1 }, postprocess_perl => { type => CODEREF, parse => 'code', optional => 1 }, postprocess_text => { type => CODEREF, parse => 'code', optional => 1 }, ); __PACKAGE__->contained_objects( lexer => 'HTML::Mason::Lexer' ); The C, C, and C parameters are part of the validation specification used by C. The various constants used, C, C, etc. are all exported by C. This means that any of these six parameter names, plus the C parameter (because of the C specification given earlier), are valid arguments to the Compiler's C method. Note that there are also some C attributes declared. These have nothing to do with C or C - any extra entries like this are simply ignored, so you are free to put extra information in the specifications as long as it doesn't overlap with what C or C are looking for. =head2 $self->create_delayed_object() If a contained object was declared with C<< delayed => 1 >>, use this method to create an instance of the object. Note that this is an object method, not a class method: my $foo = $self->create_delayed_object('foo', ...); # YES! my $foo = __PACKAGE__->create_delayed_object('foo', ...); # NO! The first argument should be a key passed to the C method. Any additional arguments will be passed to the C method of the object being created, overriding any parameters previously passed to the container class constructor. (Could I possibly be more alliterative? Veni, vedi, vici.) =head2 $self->delayed_object_params($name, [params]) Allows you to adjust the parameters that will be used to create any delayed objects in the future. The first argument specifies the "name" of the object, and any additional arguments are key-value pairs that will become parameters to the delayed object. When called with only a C<$name> argument and no list of parameters to set, returns a hash reference containing the parameters that will be passed when creating objects of this type. =head2 $self->delayed_object_class($name) Returns the class that will be used when creating delayed objects of the given name. Use this sparingly - in most situations you shouldn't care what the class is. =head2 __PACKAGE__->decorates() Version 0.09 of Class::Container added [as yet experimental] support for so-called "decorator" relationships, using the term as defined in I by Gamma, et al. (the Gang of Four book). To declare a class as a decorator of another class, simply set C<@ISA> to the class which will be decorated, and call the decorator class's C method. Internally, this will ensure that objects are instantiated as decorators. This means that you can mix & match extra add-on functionality classes much more easily. In the current implementation, if only a single decoration is used on an object, it will be instantiated as a simple subclass, thus avoiding a layer of indirection. =head2 $self->validation_spec() Returns a hash reference suitable for passing to the C C function. Does I include any arguments that can be passed to contained objects. =head2 $class->allowed_params(\%args) Returns a hash reference of every parameter this class will accept, I parameters it will pass on to its own contained objects. The keys are the parameter names, and the values are their corresponding specifications from their C definitions. If a parameter is used by both the current object and one of its contained objects, the specification returned will be from the container class, not the contained. Because the parameters accepted by C can vary based on the parameters I to C, you can pass any parameters to the C method too, ensuring that the hash you get back is accurate. =head2 $self->container() Returns the object that created you. This is remembered by storing a reference to that object, so we use the C C function to avoid persistent circular references that would cause memory leaks. If you don't have C installed, we don't make these references in the first place, and calling C will result in a fatal error. If you weren't created by another object via C, C returns C. In most cases you shouldn't care what object created you, so use this method sparingly. =head2 $object->show_containers =head2 $package->show_containers This method returns a string meant to describe the containment relationships among classes. You should not depend on the specific formatting of the string, because I may change things in a future release to make it prettier. For example, the HTML::Mason code returns the following when you do C<< $interp->show_containers >>: HTML::Mason::Interp=HASH(0x238944) resolver -> HTML::Mason::Resolver::File compiler -> HTML::Mason::Compiler::ToObject lexer -> HTML::Mason::Lexer request -> HTML::Mason::Request (delayed) buffer -> HTML::Mason::Buffer (delayed) Currently, containment is shown by indentation, so the Interp object contains a resolver and a compiler, and a delayed request (or several delayed requests). The compiler contains a lexer, and each request contains a delayed buffer (or several delayed buffers). =head2 $object->dump_parameters Returns a hash reference containing a set of parameters that should be sufficient to re-create the given object using its class's C method. This is done by fetching the current value for each declared parameter (i.e. looking in C<$object> for hash entries of the same name), then recursing through all contained objects and doing the same. A few words of caution here. First, the dumped parameters represent the I state of the object, not the state when it was originally created. Second, a class's declared parameters may not correspond exactly to its data members, so it might not be possible to recover the former from the latter. If it's possible but requires some manual fudging, you can override this method in your class, something like so: sub dump_parameters { my $self = shift; my $dump = $self->SUPER::dump_parameters(); # Perform fudgery $dump->{incoming} = $self->{_private}; delete $dump->{superfluous}; return $dump; } =head1 SEE ALSO L =head1 AUTHOR Originally by Ken Williams and Dave Rolsky for the HTML::Mason project. Important feedback contributed by Jonathan Swartz . Extended by Ken Williams for the AI::Categorizer project. Currently maintained by Ken Williams. =head1 COPYRIGHT This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut