Spiffy-0.46/0000755000175000017500000000000012373727265011417 5ustar ingyingySpiffy-0.46/META.yml0000644000175000017500000000124212373727265012667 0ustar ingyingy--- abstract: 'Spiffy Perl Interface Framework For You' author: - 'Ingy döt Net ' build_requires: {} configure_requires: ExtUtils::MakeMaker: '6.30' dynamic_config: 0 generated_by: 'Dist::Zilla version 5.019, CPAN::Meta::Converter version 2.132830' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Spiffy no_index: directory: - inc - t - xt - example requires: perl: v5.8.1 resources: bugtracker: https://github.com/ingydotnet/spiffy-pm/issues homepage: https://github.com/ingydotnet/spiffy-pm repository: https://github.com/ingydotnet/spiffy-pm.git version: '0.46' Spiffy-0.46/LICENSE0000644000175000017500000004365612373727265012442 0ustar ingyingyThis software is copyright (c) 2014 by Ingy döt Net. 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) 2014 by Ingy döt Net. 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) 2014 by Ingy döt Net. 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 Spiffy-0.46/Makefile.PL0000644000175000017500000000162612373727265013376 0ustar ingyingy # This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v5.019. use strict; use warnings; use 5.008001; use ExtUtils::MakeMaker 6.30; my %WriteMakefileArgs = ( "ABSTRACT" => "Spiffy Perl Interface Framework For You", "AUTHOR" => "Ingy d\x{f6}t Net ", "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => "6.30" }, "DISTNAME" => "Spiffy", "EXE_FILES" => [], "LICENSE" => "perl", "NAME" => "Spiffy", "PREREQ_PM" => {}, "VERSION" => "0.46", "test" => { "TESTS" => "t/*.t" } ); my %FallbackPrereqs = (); 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); Spiffy-0.46/README0000644000175000017500000004537012373727265012310 0ustar ingyingyNAME Spiffy - Spiffy Perl Interface Framework For You SYNOPSIS package Keen; use Spiffy -Base; field 'mirth'; const mood => ':-)'; sub happy { if ($self->mood eq ':-(') { $self->mirth(-1); print "Cheer up!"; } super; } DESCRIPTION "Spiffy" is a framework and methodology for doing object oriented (OO) programming in Perl. Spiffy combines the best parts of Exporter.pm, base.pm, mixin.pm and SUPER.pm into one magic foundation class. It attempts to fix all the nits and warts of traditional Perl OO, in a clean, straightforward and (perhaps someday) standard way. Spiffy borrows ideas from other OO languages like Python, Ruby, Java and Perl 6. It also adds a few tricks of its own. If you take a look on CPAN, there are a ton of OO related modules. When starting a new project, you need to pick the set of modules that makes most sense, and then you need to use those modules in each of your classes. Spiffy, on the other hand, has everything you'll probably need in one module, and you only need to use it once in one of your classes. If you make Spiffy.pm the base class of the basest class in your project, Spiffy will automatically pass all of its magic to all of your subclasses. You may eventually forget that you're even using it! The most striking difference between Spiffy and other Perl object oriented base classes, is that it has the ability to export things. If you create a subclass of Spiffy, all the things that Spiffy exports will automatically be exported by your subclass, in addition to any more things that you want to export. And if someone creates a subclass of your subclass, all of those things will be exported automatically, and so on. Think of it as "Inherited Exportation", and it uses the familiar Exporter.pm specification syntax. To use Spiffy or any subclass of Spiffy as a base class of your class, you specify the "-base" argument to the "use" command. use MySpiffyBaseModule -base; You can also use the traditional "use base 'MySpiffyBaseModule';" syntax and everything will work exactly the same. The only caveat is that Spiffy.pm must already be loaded. That's because Spiffy rewires base.pm on the fly to do all the Spiffy magics. Spiffy has support for Ruby-like mixins with Perl6-like roles. Just like "base" you can use either of the following invocations: use mixin 'MySpiffyBaseModule'; use MySpiffyBaseModule -mixin; The second version will only work if the class being mixed in is a subclass of Spiffy. The first version will work in all cases, as long as Spiffy has already been loaded. To limit the methods that get mixed in, use roles. (Hint: they work just like an Exporter list): use MySpiffyBaseModule -mixin => qw(:basics x y !foo); In object oriented Perl almost every subroutine is a method. Each method gets the object passed to it as its first argument. That means practically every subroutine starts with the line: my $self = shift; Spiffy provides a simple, optional filter mechanism to insert that line for you, resulting in cleaner code. If you figure an average method has 10 lines of code, that's 10% of your code! To turn this option on, you just use the "- Base" option instead of the "-base" option, or add the "-selfless" option. If source filtering makes you queazy, don't use the feature. I personally find it addictive in my quest for writing squeaky clean, maintainable code. A useful feature of Spiffy is that it exports two functions: "field" and "const" that can be used to declare the attributes of your class, and automatically generate accessor methods for them. The only difference between the two functions is that "const" attributes can not be modified; thus the accessor is much faster. One interesting aspect of OO programming is when a method calls the same method from a parent class. This is generally known as calling a super method. Perl's facility for doing this is butt ugly: sub cleanup { my $self = shift; $self->scrub; $self->SUPER::cleanup(@_); } Spiffy makes it, er, super easy to call super methods. You just use the "super" function. You don't need to pass it any arguments because it automatically passes them on for you. Here's the same function with Spiffy: sub cleanup { $self->scrub; super; } Spiffy has a special method for parsing arguments called "parse_arguments", that it also uses for parsing its own arguments. You declare which arguments are boolean (singletons) and which ones are paired, with two special methods called "boolean_arguments" and "paired_arguments". Parse arguments pulls out the booleans and pairs and returns them in an anonymous hash, followed by a list of the unmatched arguments. Finally, Spiffy can export a few debugging functions "WWW", "XXX", "YYY" and "ZZZ". Each of them produces a YAML dump of its arguments. WWW warns the output, XXX dies with the output, YYY prints the output, and ZZZ confesses the output. If YAML doesn't suit your needs, you can switch all the dumps to Data::Dumper format with the "-dumper" option. That's Spiffy! EXPORTING Spiffy implements a completely new idea in Perl. Modules that act both as object oriented classes and that also export functions. But it takes the concept of Exporter.pm one step further; it walks the entire @ISA path of a class and honors the export specifications of each module. Since Spiffy calls on the Exporter module to do this, you can use all the fancy interface features that Exporter has, including tags and negation. Spiffy considers all the arguments that don't begin with a dash to comprise the export specification. package Vehicle; use Spiffy -base; our $SERIAL_NUMBER = 0; our @EXPORT = qw($SERIAL_NUMBER); our @EXPORT_BASE = qw(tire horn); package Bicycle; use Vehicle -base, '!field'; $self->inflate(tire); In this case, "Bicycle->isa('Vehicle')" and also all the things that "Vehicle" and "Spiffy" export, will go into "Bicycle", except "field". Exporting can be very helpful when you've designed a system with hundreds of classes, and you want them all to have access to some functions or constants or variables. Just export them in your main base class and every subclass will get the functions they need. You can do almost everything that Exporter does because Spiffy delegates the job to Exporter (after adding some Spiffy magic). Spiffy offers a @EXPORT_BASE variable which is like @EXPORT, but only for usages that use "-base". MIXINS & ROLES If you've done much OO programming in Perl you've probably used Multiple Inheritance (MI), and if you've done much MI you've probably run into weird problems and headaches. Some languages like Ruby, attempt to resolve MI issues using a technique called mixins. Basically, all Ruby classes use only Single Inheritance (SI), and then *mixin* functionality from other modules if they need to. Mixins can be thought of at a simplistic level as *importing* the methods of another class into your subclass. But from an implementation standpoint that's not the best way to do it. Spiffy does what Ruby does. It creates an empty anonymous class, imports everything into that class, and then chains the new class into your SI ISA path. In other words, if you say: package AAA; use BBB -base; use CCC -mixin; use DDD -mixin; You end up with a single inheritance chain of classes like this: AAA << AAA-DDD << AAA-CCC << BBB; "AAA-DDD" and "AAA-CCC" are the actual package names of the generated classes. The nice thing about this style is that mixing in CCC doesn't clobber any methods in AAA, and DDD doesn't conflict with AAA or CCC either. If you mixed in a method in CCC that was also in AAA, you can still get to it by using "super". When Spiffy mixes in CCC, it pulls in all the methods in CCC that do not begin with an underscore. Actually it goes farther than that. If CCC is a subclass it will pull in every method that CCC "can" do through inheritance. This is very powerful, maybe too powerful. To limit what you mixin, Spiffy borrows the concept of Roles from Perl6. The term role is used more loosely in Spiffy though. It's much like an import list that the Exporter module uses, and you can use groups (tags) and negation. If the first element of your list uses negation, Spiffy will start with all the methods that your mixin class can do. use EEE -mixin => qw(:tools walk !run !:sharp_tools); In this example, "walk" and "run" are methods that EEE can do, and "tools" and "sharp_tools" are roles of class EEE. How does class EEE define these roles? It very simply defines methods called "_role_tools" and "_role_sharp_tools" which return lists of more methods. (And possibly other roles!) The neat thing here is that since roles are just methods, they too can be inherited. Take that Perl6! FILTERING By using the "-Base" flag instead of "-base" you never need to write the line: my $self = shift; This statement is added to every subroutine in your class by using a source filter. The magic is simple and fast, so there is litte performance penalty for creating clean code on par with Ruby and Python. package Example; use Spiffy -Base; sub crazy { $self->nuts; } sub wacky { } sub new() { bless [], shift; } is exactly the same as: package Example; use Spiffy -base; use strict;use warnings; sub crazy {my $self = shift; $self->nuts; } sub wacky {my $self = shift; } sub new { bless [], shift; } ;1; Note that the empty parens after the subroutine "new" keep it from having a $self added. Also note that the extra code is added to existing lines to ensure that line numbers are not altered. "-Base" also turns on the strict and warnings pragmas, and adds that annoying '1;' line to your module. PRIVATE METHODS Spiffy now has support for private methods when you use the '-Base' filter mechanism. You just declare the subs with the "my" keyword, and call them with a '$' in front. Like this: package Keen; use SomethingSpiffy -Base; # normal public method sub swell { $self->$stinky; } # private lexical method. uncallable from outside this file. my sub stinky { ... } SPIFFY DEBUGGING The XXX function is very handy for debugging because you can insert it almost anywhere, and it will dump your data in nice clean YAML. Take the following statement: my @stuff = grep { /keen/ } $self->find($a, $b); If you have a problem with this statement, you can debug it in any of the following ways: XXX my @stuff = grep { /keen/ } $self->find($a, $b); my @stuff = XXX grep { /keen/ } $self->find($a, $b); my @stuff = grep { /keen/ } XXX $self->find($a, $b); my @stuff = grep { /keen/ } $self->find(XXX $a, $b); XXX is easy to insert and remove. It is also a tradition to mark uncertain areas of code with XXX. This will make the debugging dumpers easy to spot if you forget to take them out. WWW and YYY are nice because they dump their arguments and then return the arguments. This way you can insert them into many places and still have the code run as before. Use ZZZ when you need to die with both a YAML dump and a full stack trace. The debugging functions are exported by default if you use the "-base" option, but only if you have previously used the "-XXX" option. To export all 4 functions use the export tag: use SomeSpiffyModule ':XXX'; To force the debugging functions to use Data::Dumper instead of YAML: use SomeSpiffyModule -dumper; SPIFFY FUNCTIONS This section describes the functions the Spiffy exports. The "field", "const", "stub" and "super" functions are only exported when you use the "-base" or "-Base" options. field Defines accessor methods for a field of your class: package Example; use Spiffy -Base; field 'foo'; field bar => []; sub lalala { $self->foo(42); push @{$self->{bar}}, $self->foo; } The first parameter passed to "field" is the name of the attribute being defined. Accessors can be given an optional default value. This value will be returned if no value for the field has been set in the object. const const bar => 42; The "const" function is similar to except that it is immutable. It also does not store data in the object. You probably always want to give a "const" a default value, otherwise the generated method will be somewhat useless. stub stub 'cigar'; The "stub" function generates a method that will die with an appropriate message. The idea is that subclasses must implement these methods so that the stub methods don't get called. super If this function is called without any arguments, it will call the same method that it is in, higher up in the ISA tree, passing it all the same arguments. If it is called with arguments, it will use those arguments with $self in the front. In other words, it just works like you'd expect. sub foo { super; # Same as $self->SUPER::foo(@_); super('hello'); # Same as $self->SUPER::foo('hello'); $self->bar(42); } sub new() { my $self = super; $self->init; return $self; } "super" will simply do nothing if there is no super method. Finally, "super" does the right thing in AUTOLOAD subroutines. METHODS This section lists all of the methods that any subclass of Spiffy automatically inherits. mixin A method to mixin a class at runtime. Takes the same arguments as "use mixin ...". Makes the target class a mixin of the caller. $self->mixin('SomeClass'); $object->mixin('SomeOtherClass' => 'some_method'); parse_arguments This method takes a list of arguments and groups them into pairs. It allows for boolean arguments which may or may not have a value (defaulting to 1). The method returns a hash reference of all the pairs as keys and values in the hash. Any arguments that cannot be paired, are returned as a list. Here is an example: sub boolean_arguments { qw(-has_spots -is_yummy) } sub paired_arguments { qw(-name -size) } my ($pairs, @others) = $self->parse_arguments( 'red', 'white', -name => 'Ingy', -has_spots => -size => 'large', 'black', -is_yummy => 0, ); After this call, $pairs will contain: { -name => 'Ingy', -has_spots => 1, -size => 'large', -is_yummy => 0, } and @others will contain 'red', 'white', and 'black'. boolean_arguments Returns the list of arguments that are recognized as being boolean. Override this method to define your own list. paired_arguments Returns the list of arguments that are recognized as being paired. Override this method to define your own list. ARGUMENTS When you "use" the Spiffy module or a subclass of it, you can pass it a list of arguments. These arguments are parsed using the "parse_arguments" method described above. The special argument "-base", is used to make the current package a subclass of the Spiffy module being used. Any non-paired parameters act like a normal import list; just like those used with the Exporter module. USING SPIFFY WITH BASE.PM The proper way to use a Spiffy module as a base class is with the "-base" parameter to the "use" statement. This differs from typical modules where you would want to "use base". package Something; use Spiffy::Module -base; use base 'NonSpiffy::Module'; Now it may be hard to keep track of what's Spiffy and what is not. Therefore Spiffy has actually been made to work with base.pm. You can say: package Something; use base 'Spiffy::Module'; use base 'NonSpiffy::Module'; "use base" is also very useful when your class is not an actual module (a separate file) but just a package in some file that has already been loaded. "base" will work whether the class is a module or not, while the "-base" syntax cannot work that way, since "use" always tries to load a module. base.pm Caveats To make Spiffy work with base.pm, a dirty trick was played. Spiffy swaps "base::import" with its own version. If the base modules are not Spiffy, Spiffy calls the original base::import. If the base modules are Spiffy, then Spiffy does its own thing. There are two caveats. Spiffy must be loaded first. If Spiffy is not loaded and "use base" is invoked on a Spiffy module, Spiffy will die with a useful message telling the author to read this documentation. That's because Spiffy needed to do the import swap beforehand. If you get this error, simply put a statement like this up front in your code: use Spiffy (); No Mixing "base.pm" can take multiple arguments. And this works with Spiffy as long as all the base classes are Spiffy, or they are all non-Spiffy. If they are mixed, Spiffy will die. In this case just use separate "use base" statements. SPIFFY TODO LIST Spiffy is a wonderful way to do OO programming in Perl, but it is still a work in progress. New things will be added, and things that don't work well, might be removed. AUTHOR Ingy döt Net COPYRIGHT AND LICENSE Copyright 2004-2014. Ingy döt Net. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See Spiffy-0.46/t/0000755000175000017500000000000012373727265011662 5ustar ingyingySpiffy-0.46/t/filter4.t0000644000175000017500000000113712373727265013422 0ustar ingyingyuse strict; use warnings; use lib (-e 't' ? 't' : 'test'), 'inc'; use Test::More tests => 1; use Spiffy '-filter_save'; use Filter4; my $result = $Spiffy::filter_result; my $expected = do { local $/; }; $result =~ s/\r//g; $expected =~ s/\r//g; is($result, $expected); __DATA__ use strict;use warnings;my($bar,$bam);# comment sub foo {my $self = shift; my $x = $self->$bar; } sub one {my $self = shift; } sub uno {my $self = shift;} $bar = sub {my $self = shift; if (1) { my $y = 1; } }; sub baz { my $z = 1; } $bam = sub {my $self = shift; $self->$bar(42); }; ;1; Spiffy-0.46/t/export1.t0000644000175000017500000000141412373727265013451 0ustar ingyingyuse strict; use warnings; use lib (-e 't' ? 't' : 'test'), 'inc'; package Foo; use Test::More tests => 20; use Something; ok(not defined &Foo::import); ok(defined &Foo::thing); ok(ref(thing) eq 'Something'); ok(thing()->can('cool')); ok(thing()->isa('Something')); ok(thing()->isa('Thing')); ok(thing()->isa('Spiffy')); is(join('-', @Foo::ISA), ''); ok(not defined &Foo::field); ok(not defined &Foo::spiffy_constructor); ok(not defined &Something::import); ok(defined &Something::thing); ok(defined &Something::field); ok(not defined &Something::spiffy_constructor); is(join('-', @Something::ISA), 'Thing'); ok(not defined &Thing::import); ok(defined &Thing::thing); ok(defined &Thing::field); ok(not defined &Thing::spiffy_constructor); is(join('-', @Thing::ISA), 'Spiffy'); Spiffy-0.46/t/Filter5.pm0000644000175000017500000000020412373727265013526 0ustar ingyingypackage Filter5; use Spiffy -Base; my sub xxx { $self->$xxx; $self->$yyy; } my sub yyy { $self->$xxx; $self->$yyy } Spiffy-0.46/t/field3.t0000644000175000017500000000403512373727265013217 0ustar ingyingyuse lib 't', 'lib'; use strict; use warnings; package Foo; use Spiffy -Base; my $test1 = field test1 => []; my $test2 = field test2 => {}; my $test3 = field test3 => [1..4]; my $test4 = field test4 => {1..4}; my $test5 = field test5 => -weaken; my $test6 = field test6 => -init => '$self->setup(@_)'; my $test7 = field test7 => -weak => -init => '$self->setup(@_)'; package main; use Test::More tests => 7; my @expected = map { s/\r//g; $_ } split /\.\.\.\r?\n/, join '', ; my $i = 1; for my $expected (@expected) { is(eval '$test' . $i++, $expected); } __DATA__ sub { $_[0]->{test1} = [] unless exists $_[0]->{test1}; return $_[0]->{test1} unless $#_ > 0; $_[0]->{test1} = $_[1]; return $_[0]->{test1}; } ... sub { $_[0]->{test2} = {} unless exists $_[0]->{test2}; return $_[0]->{test2} unless $#_ > 0; $_[0]->{test2} = $_[1]; return $_[0]->{test2}; } ... sub { $_[0]->{test3} = [ 1, 2, 3, 4 ] unless exists $_[0]->{test3}; return $_[0]->{test3} unless $#_ > 0; $_[0]->{test3} = $_[1]; return $_[0]->{test3}; } ... sub { $_[0]->{test4} = { '1' => 2, '3' => 4 } unless exists $_[0]->{test4}; return $_[0]->{test4} unless $#_ > 0; $_[0]->{test4} = $_[1]; return $_[0]->{test4}; } ... sub { $_[0]->{test5} = '-weaken' unless exists $_[0]->{test5}; return $_[0]->{test5} unless $#_ > 0; $_[0]->{test5} = $_[1]; return $_[0]->{test5}; } ... sub { return $_[0]->{test6} = do { my $self = $_[0]; $self->setup(@_) } unless $#_ > 0 or defined $_[0]->{test6}; return $_[0]->{test6} unless $#_ > 0; $_[0]->{test6} = $_[1]; return $_[0]->{test6}; } ... sub { return do { $_[0]->{test7} = do { my $self = $_[0]; $self->setup(@_) }; Scalar::Util::weaken($_[0]->{test7}) if ref $_[0]->{test7}; $_[0]->{test7}; } unless $#_ > 0 or defined $_[0]->{test7}; return $_[0]->{test7} unless $#_ > 0; $_[0]->{test7} = $_[1]; Scalar::Util::weaken($_[0]->{test7}) if ref $_[0]->{test7}; return $_[0]->{test7}; } Spiffy-0.46/t/cascade.t0000644000175000017500000000113412373727265013431 0ustar ingyingyuse lib 'lib'; package Foo; use strict; use Spiffy -base; use Cwd; our @EXPORT = qw(cwd); package Bar; use strict; Foo->base; our @EXPORT = qw(doodle); sub doodle {} sub poodle {} package Baz; use strict; Bar->base; package main; use strict; use Test::More tests => 12; ok(not defined &Foo::import); ok(defined &Foo::cwd); ok(not defined &Foo::doodle); ok(not defined &Foo::poodle); ok(not defined &Bar::import); ok(defined &Bar::cwd); ok(defined &Bar::doodle); ok(defined &Bar::poodle); ok(not defined &Baz::import); ok(defined &Baz::cwd); ok(defined &Baz::doodle); ok(not defined &Baz::poodle); Spiffy-0.46/t/Something.pm0000644000175000017500000000022312373727265014152 0ustar ingyingypackage Something; use strict; sub thing { Something->new(@_) } our @EXPORT = qw(thing); use Thing -base; field color => 'blue'; sub cool {} 1; Spiffy-0.46/t/new.t0000644000175000017500000000051112373727265012635 0ustar ingyingyuse lib 't'; use strict; use warnings; package A; use Spiffy -base; field 'x'; field 'y'; package main; use Test::More tests => 6; my $a1 = A->new; ok(not defined $a1->x); ok(not defined $a1->y); my $a2 = A->new(x => 5); is($a2->x, 5); ok(not defined $a2->y); my $a3 = A->new(x => 15, y => 10); is($a3->x, 15); is($a3->y, 10); Spiffy-0.46/t/super2.t0000644000175000017500000000112312373727265013264 0ustar ingyingyuse lib 'lib'; use strict; use warnings; package Alpha; use Spiffy -Base; sub three { print "ok 6\n"; } package Foo; use base 'Alpha'; sub one { super; print "ok 2\n"; } sub two { print "ok 4\n"; } package Bar; use base 'Foo'; sub one { super; print "ok 3\n"; } sub two { super; print "ok 5\n"; } package Baz; use base 'Bar'; sub one { print "ok 1\n"; super; } sub two { super; print "not ok 6\n"; } sub three { super; print "ok 7\n"; } package main; use strict; print "1..7\n"; Baz->new->one; Bar->new->two; Baz->new->three; Spiffy-0.46/t/filter3.t0000644000175000017500000000044012373727265013415 0ustar ingyingyuse lib 't', 'lib'; use strict; use warnings; package BOX; use Spiffy -Base; package main; use Test::More tests => 3; is(scalar , "one\n"); is(scalar , "two\n"); is(scalar , "three\n"); sub foo { $self->foo; } package BOX; __DATA__ one two three Spiffy-0.46/t/export3.t0000644000175000017500000000072212373727265013454 0ustar ingyingyuse lib 't'; use strict; use warnings; package AAA; use Spiffy -base; BEGIN {@AAA::EXPORT_OK = qw($A1 $A2)} $AAA::A1 = 5; $AAA::A2 = 10; package BBB; use base 'AAA'; BEGIN {@BBB::EXPORT_OK = qw($A2 $A3)} $BBB::A2 = 15; $BBB::A3 = 20; package main; no warnings; use Test::More tests => 7; BEGIN {BBB->import(qw($A1 $A2 $A3 $A4))} ok(defined $main::A1); ok(defined $main::A2); ok(defined $main::A3); ok(not defined $main::A4); is($A1, 5); is($A2, 10); is($A3, 20); Spiffy-0.46/t/export7.t0000644000175000017500000000033512373727265013460 0ustar ingyingyuse Test::More; plan tests => 4; package BBB; use Spiffy -Base, -XXX; package AAA; use Spiffy -Base; package main; ok(not defined &AAA::XXX); ok(defined &AAA::field); ok(defined &BBB::XXX); ok(defined &BBB::field); Spiffy-0.46/t/base.t0000644000175000017500000000217312373727265012764 0ustar ingyingyuse strict; use warnings; use lib (-e 't' ? 't' : 'test'), 'inc'; package XXX; BEGIN {require Thing} use base 'Thing'; package Foo; use base 'Spiffy'; BEGIN { @Foo::EXPORT=qw(xxx) } sub xxx {} package Bar; use base 'Foo', 'Thing'; package Boo; BEGIN { @Boo::EXPORT=qw(xxx) } sub xxx {} package Goo; use base 'Boo'; package Something; use base 'Spiffy'; BEGIN { @Something::EXPORT = qw(qwerty) } sub qwerty {} package SomethingGood; use base 'Something'; package main; use Test::More tests => 24; ok(Thing->isa('Spiffy')); ok(defined &XXX::thing); ok(defined &XXX::field); ok(defined &XXX::const); ok(defined &Foo::field); ok(defined &Foo::const); ok(defined &Foo::xxx); ok(Bar->isa('Spiffy')); ok(Bar->isa('Foo')); ok(Bar->isa('Thing')); ok(defined &Bar::field); ok(defined &Bar::const); ok(defined &Bar::xxx); ok(defined &Bar::thing); ok(not Boo->isa('Spiffy')); ok(defined &Boo::xxx); ok(not Goo->isa('Spiffy')); ok(Goo->isa('Boo')); ok(not defined &Goo::xxx); ok(SomethingGood->isa('Something')); ok(SomethingGood->isa('Spiffy')); ok(not SomethingGood->isa('Thing')); ok(not defined &SomethingGood::thing); ok(not @Spiffy::ISA); Spiffy-0.46/t/NonSpiffy.pm0000644000175000017500000000007312373727265014133 0ustar ingyingypackage NonSpiffy; use Filter4; # Filter4 /is/ Spiffy 1; Spiffy-0.46/t/mixin2.t0000644000175000017500000000223612373727265013260 0ustar ingyingyuse lib 't', 'lib'; use Spiffy (); package A; sub _role_a { qw(a1 a2 a3) } sub a1 {'a1' } sub a2 {'a2' } sub a3 {'a3' } sub _role_A { qw(A1 A2 A3) } sub A1 {'A1' } sub A2 {'A2' } sub A3 {'A3' } sub _role_aA { qw(:a :A foo) } sub foo {'foo'} package BB; use base 'A'; package X; use mixin A => qw(:a !a2); package X2; use mixin BB => qw(:a !a2); package X3; use mixin A => qw(!:A A2); package X4; use mixin A => qw(:aA !a1 !a1 !A1); package X5; use mixin A => qw(!:a !:A); package main; use Test::More tests => 32; ok(X->can('a1')); ok(not X->can('a2')); ok(X->can('a3')); ok(not X->can('A1')); is(X->a1, 'a1'); is(X->a3, 'a3'); ok(X2->can('a1')); ok(not X2->can('a2')); ok(X2->can('a3')); ok(not X2->can('A1')); is(X2->a1, 'a1'); is(X2->a3, 'a3'); ok(X3->can('a1')); ok(X3->can('a2')); ok(X3->can('a3')); ok(not X3->can('A1')); ok(X3->can('A2')); ok(not X3->can('A3')); ok(not X4->can('a1')); ok(X4->can('a2')); ok(X4->can('a3')); ok(not X4->can('A1')); ok(X4->can('A2')); ok(X4->can('A3')); ok(X4->can('foo')); ok(not X5->can('a1')); ok(not X5->can('a2')); ok(not X5->can('a3')); ok(not X5->can('A1')); ok(not X5->can('A2')); ok(not X5->can('A3')); ok(X5->can('foo')); Spiffy-0.46/t/const.t0000644000175000017500000000033312373727265013174 0ustar ingyingyuse lib 't', 'lib'; use strict; use warnings; package XXX; use Spiffy -base; const foo => 42; package main; use Test::More tests => 3; my $xxx = XXX->new; is($xxx->foo, 42); is($xxx->foo(69), 42); is($xxx->foo, 42); Spiffy-0.46/t/export6.t0000644000175000017500000000042512373727265013457 0ustar ingyingyuse lib 't', 'lib'; use strict; use warnings; package AAA; use Spiffy -Base, ':XXX'; package BBB; use Spiffy -Base, ':XXX', 'field'; package main; use Test::More tests => 4; ok(not defined &AAA::field); ok(defined &BBB::field); ok(defined &AAA::XXX); ok(defined &BBB::XXX); Spiffy-0.46/t/release-pod-syntax.t0000644000175000017500000000045612373727265015600 0ustar ingyingy#!perl BEGIN { unless ($ENV{RELEASE_TESTING}) { require Test::More; Test::More::plan(skip_all => 'these tests are for release candidate testing'); } } # This file was automatically generated by Dist::Zilla::Plugin::PodSyntaxTests. use Test::More; use Test::Pod 1.41; all_pod_files_ok(); Spiffy-0.46/t/mixin.t0000644000175000017500000000100512373727265013167 0ustar ingyingyuse lib 't', 'lib'; use strict; no strict 'refs'; use warnings; package A; use Spiffy -base; field 'foo' => 17; package X; sub extra {99} package BB; use base 'X'; sub xxx {42} sub yyy {} sub _zzz {} package C; use base 'A'; use mixin 'BB'; package main; use Test::More tests => 10; my $c = C->new; ok($c->can('foo')); is($c->foo, 17); ok($c->can('extra')); is($c->extra, 99); ok($c->can('xxx')); is($c->xxx, 42); ok(not $c->can('_zzz')); is(@{C::ISA}, 1); is(${C::ISA}[0], 'C-BB'); is(${"C-BB::ISA"}[0], 'A'); Spiffy-0.46/t/Filter4.pm0000644000175000017500000000034312373727265013531 0ustar ingyingypackage Filter4; use Spiffy -Base; # comment sub foo { my $x = $self->$bar; } sub one { } sub uno {} my sub bar { if (1) { my $y = 1; } } sub baz() { my $z = 1; } my sub bam { $self->$bar(42); } Spiffy-0.46/t/super.t0000644000175000017500000000142312373727265013205 0ustar ingyingyuse lib 'lib'; package Foo; use strict; use Spiffy -base; field 'xxx'; field 'dog'; field 'bog'; sub new { my $self = super; $self->xxx('XXX'); return $self; } sub poodle { my $self = shift; my $count = shift; $self->dog("$count poodle"); } sub doodle { my $self = shift; my $count = shift; $self->bog("$count doodle"); } package Bar; use strict; BEGIN { Foo->base } sub poodle { my $self = shift; super; $self->dog($self->dog . ' dogs'); } sub doodle { my $self = shift; eval 'eval "super"'; $self->bog($self->bog . ' bogs'); } package main; use strict; use Test::More tests => 3; my $f = Bar->new; is($f->{xxx}, 'XXX'); $f->poodle(3); is($f->{dog}, '3 poodle dogs'); $f->doodle(4); is($f->{bog}, '4 doodle bogs'); Spiffy-0.46/t/export4.t0000644000175000017500000000175312373727265013462 0ustar ingyingyuse lib 't'; use strict; use warnings; package AAA; # Exporter before 5.8.4 needs the tag as the first thing imported use Spiffy -base, qw(:XXX const); package BBB; use base 'AAA'; package CCC; use Spiffy -XXX, -base; package DDD; use Spiffy -base; package EEE; use Spiffy -base, 'XXX'; package FFF; use Spiffy -base; use Spiffy 'XXX'; package main; use Test::More tests => 24; ok(not defined &AAA::field); ok(defined &AAA::const); ok(defined &AAA::XXX); ok(defined &AAA::YYY); ok(defined &BBB::field); ok(defined &BBB::const); ok(not defined &BBB::XXX); ok(not defined &BBB::YYY); ok(defined &CCC::field); ok(defined &CCC::const); ok(defined &CCC::XXX); ok(defined &CCC::YYY); ok(defined &DDD::field); ok(defined &DDD::const); ok(not defined &DDD::XXX); ok(not defined &DDD::YYY); ok(not defined &EEE::field); ok(not defined &EEE::const); ok(defined &EEE::XXX); ok(not defined &EEE::YYY); ok(defined &FFF::field); ok(defined &FFF::const); ok(defined &FFF::XXX); ok(not defined &FFF::YYY); Spiffy-0.46/t/package.t0000644000175000017500000000027612373727265013447 0ustar ingyingyuse lib 'lib'; use strict; use warnings; use Test::More tests => 2; package Foo; use Spiffy -base => -package => 'Bar'; package main; ok(not defined &Foo::field); ok(defined &Bar::field); Spiffy-0.46/t/mixin3.t0000644000175000017500000000030512373727265013254 0ustar ingyingyuse lib 't', 'lib'; package AAA; use Spiffy -Base; package BBB; use Spiffy -Base; field foo => 42; package main; use Test::More tests => 1; my $a = AAA->new; $a->mixin('BBB'); is($a->foo, 42); Spiffy-0.46/t/export5.t0000644000175000017500000000056112373727265013457 0ustar ingyingyuse lib 't', 'lib'; use strict; use warnings; package AAA; use Spiffy -base; BEGIN {@AAA::EXPORT_OK = qw(dude)} const dude => 10; package BBB; use base 'AAA'; BEGIN { @BBB::EXPORT_OK = qw(dude); const dude => 20; } package CCC; BEGIN {BBB->import('dude')} package main; no warnings; use Test::More tests => 2; ok(defined $CCC::{dude}); is(CCC::dude(), 20); Spiffy-0.46/t/filter.t0000644000175000017500000000051512373727265013335 0ustar ingyingyuse lib 't', 'lib'; use strict; use warnings; package XXX; use Spiffy -Base; #, '-filter_dump'; const name => 'world'; sub foo { "Hello, " . $self->name; } sub bar() { my $self = shift; return $self; } package main; use Test::More tests => 2; my $xxx = XXX->new; is($xxx->foo, 'Hello, world'); is(XXX::bar(42), 42); Spiffy-0.46/t/field2.t0000644000175000017500000000064512373727265013221 0ustar ingyingyuse lib 't', 'lib'; use strict; use warnings; package Foo; use Spiffy -base; field one => []; field two => {}; field three => [1..4]; field four => {1..4}; package main; use Test::More tests => 5; use Spiffy 'id'; my $f1 = Foo->new; my $f2 = Foo->new; ok(id($f1->one) ne id($f2->one)); ok(id($f1->two) ne id($f2->two)); is(scalar(@{$f1->three}), 4); is_deeply($f1->three, $f2->three); is_deeply($f1->four, $f2->four); Spiffy-0.46/t/parse.t0000644000175000017500000000023712373727265013163 0ustar ingyingyuse lib 'lib'; use strict; use warnings; use Test::More tests => 1; use Spiffy; my $args = Spiffy->parse_arguments(); ok(ref $args && ref($args) eq 'HASH'); Spiffy-0.46/t/early.t0000644000175000017500000000044312373727265013164 0ustar ingyingyuse Test::More tests => 1; use lib 't'; SKIP: { skip 'XXX - fix later', 1; eval <<'...'; package Foo; use base 'Filter4'; ... like $@, qr/\QSpiffy.pm must be loaded before calling 'use base'/, "Caught attempt to use 'base' on Spiffy module before loading Spiffy"; } Spiffy-0.46/t/base2.t0000644000175000017500000000020512373727265013040 0ustar ingyingyuse lib (-e 't' ? 't' : 'test'), 'inc'; use Test::More tests => 1; eval <<'...'; package Foo; use base 'NonSpiffy'; ... is $@, ''; Spiffy-0.46/t/field.t0000644000175000017500000000051012373727265013126 0ustar ingyingyuse lib 't', 'lib'; use strict; use warnings; use Spiffy (); package Bar; package Foo; use base 'Spiffy'; sub new { my $self = super; field -package => 'Bar', 'xxx'; } use Test::More tests => 4; Foo->new; ok(not defined $Foo::{-package}); ok(not defined &Foo::Bar); ok(not defined &Foo::xxx); ok(defined &Bar::xxx); Spiffy-0.46/t/stub.t0000644000175000017500000000033712373727265013027 0ustar ingyingyuse lib 't', 'lib'; use strict; use warnings; package XXX; use Spiffy -base; stub 'foo'; package YYY; use base 'XXX'; package main; use Test::More tests => 1; my $y = YYY->new; eval {$y->foo}; like($@, qr/subclassed/); Spiffy-0.46/t/autoload.t0000644000175000017500000000053612373727265013663 0ustar ingyingyuse lib 't', 'lib'; use strict; use warnings; use Spiffy (); package AAA; use Spiffy -Base; sub AUTOLOAD { super; join '+', $AAA::AUTOLOAD, @_; } package BBB; use base 'AAA'; sub AUTOLOAD { super; } package CCC; use base 'BBB'; sub AUTOLOAD { super; } package main; use Test::More tests => 1; is(CCC->foo(42), 'CCC::foo+42'); Spiffy-0.46/t/filter2.t0000644000175000017500000000053212373727265013416 0ustar ingyingyuse lib 't', 'lib'; use strict; use warnings; package YYY; use Spiffy -Base; package XXX; use Spiffy -Base; const name => 'world'; sub foo { "Hello, " . $self->name; } sub bar() { my $self = shift; return $self; } package main; use Test::More tests => 2; my $xxx = XXX->new; is($xxx->foo, 'Hello, world'); is(XXX::bar(42), 42); Spiffy-0.46/t/Thing.pm0000644000175000017500000000022012373727265013263 0ustar ingyingypackage Thing; use strict; use Spiffy -base; use base 'Spiffy'; our @EXPORT = qw(thing); field volume => 11; sub thing { Thing->new(@_) } 1; Spiffy-0.46/t/export2.t0000644000175000017500000000065412373727265013457 0ustar ingyingyuse lib 't'; use strict; use warnings; package AAA; use Spiffy -base; BEGIN {@AAA::EXPORT = qw($A1 $A2)} $AAA::A1 = 5; $AAA::A2 = 10; package BBB; use base 'AAA'; BEGIN {@BBB::EXPORT = qw($A2 $A3)} $BBB::A2 = 15; $BBB::A3 = 20; package main; use strict; use Test::More tests => 6; BEGIN {BBB->import} ok(defined $main::A1); ok(defined $main::A2); ok(defined $main::A3); is($main::A1, 5); is($main::A2, 15); is($main::A3, 20); Spiffy-0.46/t/filter5.t0000644000175000017500000000071512373727265013424 0ustar ingyingyuse strict; use warnings; use lib (-e 't' ? 't' : 'test'), 'inc'; use Test::More tests => 1; use Spiffy -filter_save; use Filter5; my $result = $Spiffy::filter_result; my $expected = do { local $/; }; $result =~ s/\r//g; $expected =~ s/\r//g; is($result, $expected); __DATA__ use strict;use warnings;my($xxx,$yyy);$xxx = sub {my $self = shift; $self->$xxx; $self->$yyy; }; $yyy = sub {my $self = shift; $self->$xxx; $self->$yyy }; ;1; Spiffy-0.46/t/exporter.t0000644000175000017500000000024112373727265013714 0ustar ingyingypackage Foo; use Spiffy -base; package autouse; use Test::More tests => 1; is 'Foo'->can('import'), \&Exporter::import, 'Spiffy modules support autouse'; Spiffy-0.46/lib/0000755000175000017500000000000012373727265012165 5ustar ingyingySpiffy-0.46/lib/Spiffy/0000755000175000017500000000000012373727265013425 5ustar ingyingySpiffy-0.46/lib/Spiffy/mixin.pm0000644000175000017500000000003212373727265015102 0ustar ingyingypackage Spiffy::mixin; 1; Spiffy-0.46/lib/Spiffy.pod0000644000175000017500000004340512373727265014137 0ustar ingyingy=pod =for comment DO NOT EDIT. This Pod was generated by Swim. See http://github.com/ingydotnet/swim-pm#readme =encoding utf8 =head1 NAME Spiffy - Spiffy Perl Interface Framework For You =for html spiffy-pm spiffy-pm =head1 SYNOPSIS package Keen; use Spiffy -Base; field 'mirth'; const mood => ':-)'; sub happy { if ($self->mood eq ':-(') { $self->mirth(-1); print "Cheer up!"; } super; } =head1 DESCRIPTION "Spiffy" is a framework and methodology for doing object oriented (OO) programming in Perl. Spiffy combines the best parts of Exporter.pm, base.pm, mixin.pm and SUPER.pm into one magic foundation class. It attempts to fix all the nits and warts of traditional Perl OO, in a clean, straightforward and (perhaps someday) standard way. Spiffy borrows ideas from other OO languages like Python, Ruby, Java and Perl 6. It also adds a few tricks of its own. If you take a look on CPAN, there are a ton of OO related modules. When starting a new project, you need to pick the set of modules that makes most sense, and then you need to use those modules in each of your classes. Spiffy, on the other hand, has everything you'll probably need in one module, and you only need to use it once in one of your classes. If you make Spiffy.pm the base class of the basest class in your project, Spiffy will automatically pass all of its magic to all of your subclasses. You may eventually forget that you're even using it! The most striking difference between Spiffy and other Perl object oriented base classes, is that it has the ability to export things. If you create a subclass of Spiffy, all the things that Spiffy exports will automatically be exported by your subclass, in addition to any more things that you want to export. And if someone creates a subclass of your subclass, all of those things will be exported automatically, and so on. Think of it as "Inherited Exportation", and it uses the familiar Exporter.pm specification syntax. To use Spiffy or any subclass of Spiffy as a base class of your class, you specify the C<-base> argument to the C command. use MySpiffyBaseModule -base; You can also use the traditional C syntax and everything will work exactly the same. The only caveat is that Spiffy.pm must already be loaded. That's because Spiffy rewires base.pm on the fly to do all the Spiffy magics. Spiffy has support for Ruby-like mixins with Perl6-like roles. Just like C you can use either of the following invocations: use mixin 'MySpiffyBaseModule'; use MySpiffyBaseModule -mixin; The second version will only work if the class being mixed in is a subclass of Spiffy. The first version will work in all cases, as long as Spiffy has already been loaded. To limit the methods that get mixed in, use roles. (Hint: they work just like an Exporter list): use MySpiffyBaseModule -mixin => qw(:basics x y !foo); In object oriented Perl almost every subroutine is a method. Each method gets the object passed to it as its first argument. That means practically every subroutine starts with the line: my $self = shift; Spiffy provides a simple, optional filter mechanism to insert that line for you, resulting in cleaner code. If you figure an average method has 10 lines of code, that's 10% of your code! To turn this option on, you just use the C<- Base> option instead of the C<-base> option, or add the C<-selfless> option. If source filtering makes you queazy, don't use the feature. I personally find it addictive in my quest for writing squeaky clean, maintainable code. A useful feature of Spiffy is that it exports two functions: C and C that can be used to declare the attributes of your class, and automatically generate accessor methods for them. The only difference between the two functions is that C attributes can not be modified; thus the accessor is much faster. One interesting aspect of OO programming is when a method calls the same method from a parent class. This is generally known as calling a super method. Perl's facility for doing this is butt ugly: sub cleanup { my $self = shift; $self->scrub; $self->SUPER::cleanup(@_); } Spiffy makes it, er, super easy to call super methods. You just use the C function. You don't need to pass it any arguments because it automatically passes them on for you. Here's the same function with Spiffy: sub cleanup { $self->scrub; super; } Spiffy has a special method for parsing arguments called C, that it also uses for parsing its own arguments. You declare which arguments are boolean (singletons) and which ones are paired, with two special methods called C and C. Parse arguments pulls out the booleans and pairs and returns them in an anonymous hash, followed by a list of the unmatched arguments. Finally, Spiffy can export a few debugging functions C, C, C and C. Each of them produces a YAML dump of its arguments. WWW warns the output, XXX dies with the output, YYY prints the output, and ZZZ confesses the output. If YAML doesn't suit your needs, you can switch all the dumps to Data::Dumper format with the C<-dumper> option. That's Spiffy! =head1 EXPORTING Spiffy implements a completely new idea in Perl. Modules that act both as object oriented classes and that also export functions. But it takes the concept of Exporter.pm one step further; it walks the entire C<@ISA> path of a class and honors the export specifications of each module. Since Spiffy calls on the Exporter module to do this, you can use all the fancy interface features that Exporter has, including tags and negation. Spiffy considers all the arguments that don't begin with a dash to comprise the export specification. package Vehicle; use Spiffy -base; our $SERIAL_NUMBER = 0; our @EXPORT = qw($SERIAL_NUMBER); our @EXPORT_BASE = qw(tire horn); package Bicycle; use Vehicle -base, '!field'; $self->inflate(tire); In this case, C<< Bicycle->isa('Vehicle') >> and also all the things that C and C export, will go into C, except C. Exporting can be very helpful when you've designed a system with hundreds of classes, and you want them all to have access to some functions or constants or variables. Just export them in your main base class and every subclass will get the functions they need. You can do almost everything that Exporter does because Spiffy delegates the job to Exporter (after adding some Spiffy magic). Spiffy offers a C<@EXPORT_BASE> variable which is like C<@EXPORT>, but only for usages that use C<-base>. =head1 MIXINS & ROLES If you've done much OO programming in Perl you've probably used Multiple Inheritance (MI), and if you've done much MI you've probably run into weird problems and headaches. Some languages like Ruby, attempt to resolve MI issues using a technique called mixins. Basically, all Ruby classes use only Single Inheritance (SI), and then I functionality from other modules if they need to. Mixins can be thought of at a simplistic level as I the methods of another class into your subclass. But from an implementation standpoint that's not the best way to do it. Spiffy does what Ruby does. It creates an empty anonymous class, imports everything into that class, and then chains the new class into your SI ISA path. In other words, if you say: package AAA; use BBB -base; use CCC -mixin; use DDD -mixin; You end up with a single inheritance chain of classes like this: AAA << AAA-DDD << AAA-CCC << BBB; C and C are the actual package names of the generated classes. The nice thing about this style is that mixing in CCC doesn't clobber any methods in AAA, and DDD doesn't conflict with AAA or CCC either. If you mixed in a method in CCC that was also in AAA, you can still get to it by using C. When Spiffy mixes in CCC, it pulls in all the methods in CCC that do not begin with an underscore. Actually it goes farther than that. If CCC is a subclass it will pull in every method that CCC C do through inheritance. This is very powerful, maybe too powerful. To limit what you mixin, Spiffy borrows the concept of Roles from Perl6. The term role is used more loosely in Spiffy though. It's much like an import list that the Exporter module uses, and you can use groups (tags) and negation. If the first element of your list uses negation, Spiffy will start with all the methods that your mixin class can do. use EEE -mixin => qw(:tools walk !run !:sharp_tools); In this example, C and C are methods that EEE can do, and C and C are roles of class EEE. How does class EEE define these roles? It very simply defines methods called C<_role_tools> and C<_role_sharp_tools> which return lists of more methods. (And possibly other roles!) The neat thing here is that since roles are just methods, they too can be inherited. Take B Perl6! =head1 FILTERING By using the C<-Base> flag instead of C<-base> you never need to write the line: my $self = shift; This statement is added to every subroutine in your class by using a source filter. The magic is simple and fast, so there is litte performance penalty for creating clean code on par with Ruby and Python. package Example; use Spiffy -Base; sub crazy { $self->nuts; } sub wacky { } sub new() { bless [], shift; } is exactly the same as: package Example; use Spiffy -base; use strict;use warnings; sub crazy {my $self = shift; $self->nuts; } sub wacky {my $self = shift; } sub new { bless [], shift; } ;1; Note that the empty parens after the subroutine C keep it from having a $self added. Also note that the extra code is added to existing lines to ensure that line numbers are not altered. C<-Base> also turns on the strict and warnings pragmas, and adds that annoying '1;' line to your module. =head1 PRIVATE METHODS Spiffy now has support for private methods when you use the '-Base' filter mechanism. You just declare the subs with the C keyword, and call them with a C<'$'> in front. Like this: package Keen; use SomethingSpiffy -Base; # normal public method sub swell { $self->$stinky; } # private lexical method. uncallable from outside this file. my sub stinky { ... } =head1 SPIFFY DEBUGGING The XXX function is very handy for debugging because you can insert it almost anywhere, and it will dump your data in nice clean YAML. Take the following statement: my @stuff = grep { /keen/ } $self->find($a, $b); If you have a problem with this statement, you can debug it in any of the following ways: XXX my @stuff = grep { /keen/ } $self->find($a, $b); my @stuff = XXX grep { /keen/ } $self->find($a, $b); my @stuff = grep { /keen/ } XXX $self->find($a, $b); my @stuff = grep { /keen/ } $self->find(XXX $a, $b); XXX is easy to insert and remove. It is also a tradition to mark uncertain areas of code with XXX. This will make the debugging dumpers easy to spot if you forget to take them out. WWW and YYY are nice because they dump their arguments and then return the arguments. This way you can insert them into many places and still have the code run as before. Use ZZZ when you need to die with both a YAML dump and a full stack trace. The debugging functions are exported by default if you use the C<-base> option, but only if you have previously used the C<-XXX> option. To export all 4 functions use the export tag: use SomeSpiffyModule ':XXX'; To force the debugging functions to use Data::Dumper instead of YAML: use SomeSpiffyModule -dumper; =head1 SPIFFY FUNCTIONS This section describes the functions the Spiffy exports. The C, C, C and C functions are only exported when you use the C<-base> or C<-Base> options. =over =item field Defines accessor methods for a field of your class: package Example; use Spiffy -Base; field 'foo'; field bar => []; sub lalala { $self->foo(42); push @{$self->{bar}}, $self->foo; } The first parameter passed to C is the name of the attribute being defined. Accessors can be given an optional default value. This value will be returned if no value for the field has been set in the object. =item const const bar => 42; The C function is similar to except that it is immutable. It also does not store data in the object. You probably always want to give a C a default value, otherwise the generated method will be somewhat useless. =item stub stub 'cigar'; The C function generates a method that will die with an appropriate message. The idea is that subclasses must implement these methods so that the stub methods don't get called. =item super If this function is called without any arguments, it will call the same method that it is in, higher up in the ISA tree, passing it all the same arguments. If it is called with arguments, it will use those arguments with C<$self> in the front. In other words, it just works like you'd expect. sub foo { super; # Same as $self->SUPER::foo(@_); super('hello'); # Same as $self->SUPER::foo('hello'); $self->bar(42); } sub new() { my $self = super; $self->init; return $self; } C will simply do nothing if there is no super method. Finally, C does the right thing in AUTOLOAD subroutines. =back =head1 METHODS This section lists all of the methods that any subclass of Spiffy automatically inherits. =over =item mixin A method to mixin a class at runtime. Takes the same arguments as C. Makes the target class a mixin of the caller. $self->mixin('SomeClass'); $object->mixin('SomeOtherClass' => 'some_method'); =item parse_arguments This method takes a list of arguments and groups them into pairs. It allows for boolean arguments which may or may not have a value (defaulting to 1). The method returns a hash reference of all the pairs as keys and values in the hash. Any arguments that cannot be paired, are returned as a list. Here is an example: sub boolean_arguments { qw(-has_spots -is_yummy) } sub paired_arguments { qw(-name -size) } my ($pairs, @others) = $self->parse_arguments( 'red', 'white', -name => 'Ingy', -has_spots => -size => 'large', 'black', -is_yummy => 0, ); After this call, C<$pairs> will contain: { -name => 'Ingy', -has_spots => 1, -size => 'large', -is_yummy => 0, } and C<@others> will contain 'red', 'white', and 'black'. =item boolean_arguments Returns the list of arguments that are recognized as being boolean. Override this method to define your own list. =item paired_arguments Returns the list of arguments that are recognized as being paired. Override this method to define your own list. =back =head1 ARGUMENTS When you C the Spiffy module or a subclass of it, you can pass it a list of arguments. These arguments are parsed using the C method described above. The special argument C<-base>, is used to make the current package a subclass of the Spiffy module being used. Any non-paired parameters act like a normal import list; just like those used with the Exporter module. =head1 USING SPIFFY WITH BASE.PM The proper way to use a Spiffy module as a base class is with the C<-base> parameter to the C statement. This differs from typical modules where you would want to C. package Something; use Spiffy::Module -base; use base 'NonSpiffy::Module'; Now it may be hard to keep track of what's Spiffy and what is not. Therefore Spiffy has actually been made to work with base.pm. You can say: package Something; use base 'Spiffy::Module'; use base 'NonSpiffy::Module'; C is also very useful when your class is not an actual module (a separate file) but just a package in some file that has already been loaded. C will work whether the class is a module or not, while the C<-base> syntax cannot work that way, since C always tries to load a module. =head2 base.pm Caveats To make Spiffy work with base.pm, a dirty trick was played. Spiffy swaps C with its own version. If the base modules are not Spiffy, Spiffy calls the original base::import. If the base modules are Spiffy, then Spiffy does its own thing. There are two caveats. =over =item Spiffy must be loaded first. If Spiffy is not loaded and C is invoked on a Spiffy module, Spiffy will die with a useful message telling the author to read this documentation. That's because Spiffy needed to do the import swap beforehand. If you get this error, simply put a statement like this up front in your code: use Spiffy (); =item No Mixing C can take multiple arguments. And this works with Spiffy as long as all the base classes are Spiffy, or they are all non-Spiffy. If they are mixed, Spiffy will die. In this case just use separate C statements. =back =head1 SPIFFY TODO LIST Spiffy is a wonderful way to do OO programming in Perl, but it is still a work in progress. New things will be added, and things that don't work well, might be removed. =head1 AUTHOR Ingy döt Net =head1 COPYRIGHT AND LICENSE Copyright 2004-2014. Ingy döt Net. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See L =cut Spiffy-0.46/lib/Spiffy.pm0000644000175000017500000003617512373727265013777 0ustar ingyingyuse strict; use warnings; package Spiffy; our $VERSION = '0.46'; use Carp; require Exporter; our @EXPORT = (); our @EXPORT_BASE = qw(field const stub super); our @EXPORT_OK = (@EXPORT_BASE, qw(id WWW XXX YYY ZZZ)); our %EXPORT_TAGS = (XXX => [qw(WWW XXX YYY ZZZ)]); my $stack_frame = 0; my $dump = 'yaml'; my $bases_map = {}; sub WWW; sub XXX; sub YYY; sub ZZZ; # This line is here to convince "autouse" into believing we are autousable. sub can { ($_[1] eq 'import' and caller()->isa('autouse')) ? \&Exporter::import # pacify autouse's equality test : $_[0]->SUPER::can($_[1]) # normal case } # TODO # # Exported functions like field and super should be hidden so as not to # be confused with methods that can be inherited. # sub new { my $class = shift; $class = ref($class) || $class; my $self = bless {}, $class; while (@_) { my $method = shift; $self->$method(shift); } return $self; } my $filtered_files = {}; my $filter_dump = 0; my $filter_save = 0; our $filter_result = ''; sub import { no strict 'refs'; no warnings; my $self_package = shift; # XXX Using parse_arguments here might cause confusion, because the # subclass's boolean_arguments and paired_arguments can conflict, causing # difficult debugging. Consider using something truly local. my ($args, @export_list) = do { local *boolean_arguments = sub { qw( -base -Base -mixin -selfless -XXX -dumper -yaml -filter_dump -filter_save ) }; local *paired_arguments = sub { qw(-package) }; $self_package->parse_arguments(@_); }; return spiffy_mixin_import(scalar(caller(0)), $self_package, @export_list) if $args->{-mixin}; $filter_dump = 1 if $args->{-filter_dump}; $filter_save = 1 if $args->{-filter_save}; $dump = 'yaml' if $args->{-yaml}; $dump = 'dumper' if $args->{-dumper}; local @EXPORT_BASE = @EXPORT_BASE; if ($args->{-XXX}) { push @EXPORT_BASE, @{$EXPORT_TAGS{XXX}} unless grep /^XXX$/, @EXPORT_BASE; } spiffy_filter() if ($args->{-selfless} or $args->{-Base}) and not $filtered_files->{(caller($stack_frame))[1]}++; my $caller_package = $args->{-package} || caller($stack_frame); push @{"$caller_package\::ISA"}, $self_package if $args->{-Base} or $args->{-base}; for my $class (@{all_my_bases($self_package)}) { next unless $class->isa('Spiffy'); my @export = grep { not defined &{"$caller_package\::$_"}; } ( @{"$class\::EXPORT"}, ($args->{-Base} or $args->{-base}) ? @{"$class\::EXPORT_BASE"} : (), ); my @export_ok = grep { not defined &{"$caller_package\::$_"}; } @{"$class\::EXPORT_OK"}; # Avoid calling the expensive Exporter::export # if there is nothing to do (optimization) my %exportable = map { ($_, 1) } @export, @export_ok; next unless keys %exportable; my @export_save = @{"$class\::EXPORT"}; my @export_ok_save = @{"$class\::EXPORT_OK"}; @{"$class\::EXPORT"} = @export; @{"$class\::EXPORT_OK"} = @export_ok; my @list = grep { (my $v = $_) =~ s/^[\!\:]//; $exportable{$v} or ${"$class\::EXPORT_TAGS"}{$v}; } @export_list; Exporter::export($class, $caller_package, @list); @{"$class\::EXPORT"} = @export_save; @{"$class\::EXPORT_OK"} = @export_ok_save; } } sub spiffy_filter { require Filter::Util::Call; my $done = 0; Filter::Util::Call::filter_add( sub { return 0 if $done; my ($data, $end) = ('', ''); while (my $status = Filter::Util::Call::filter_read()) { return $status if $status < 0; if (/^__(?:END|DATA)__\r?$/) { $end = $_; last; } $data .= $_; $_ = ''; } $_ = $data; my @my_subs; s[^(sub\s+\w+\s+\{)(.*\n)] [${1}my \$self = shift;$2]gm; s[^(sub\s+\w+)\s*\(\s*\)(\s+\{.*\n)] [${1}${2}]gm; s[^my\s+sub\s+(\w+)(\s+\{)(.*)((?s:.*?\n))\}\n] [push @my_subs, $1; "\$$1 = sub$2my \$self = shift;$3$4\};\n"]gem; my $preclare = ''; if (@my_subs) { $preclare = join ',', map "\$$_", @my_subs; $preclare = "my($preclare);"; } $_ = "use strict;use warnings;$preclare${_};1;\n$end"; if ($filter_dump) { print; exit } if ($filter_save) { $filter_result = $_; $_ = $filter_result; } $done = 1; } ); } sub base { push @_, -base; goto &import; } sub all_my_bases { my $class = shift; return $bases_map->{$class} if defined $bases_map->{$class}; my @bases = ($class); no strict 'refs'; for my $base_class (@{"${class}::ISA"}) { push @bases, @{all_my_bases($base_class)}; } my $used = {}; $bases_map->{$class} = [grep {not $used->{$_}++} @bases]; } my %code = ( sub_start => "sub {\n", set_default => " \$_[0]->{%s} = %s\n unless exists \$_[0]->{%s};\n", init => " return \$_[0]->{%s} = do { my \$self = \$_[0]; %s }\n" . " unless \$#_ > 0 or defined \$_[0]->{%s};\n", weak_init => " return do {\n" . " \$_[0]->{%s} = do { my \$self = \$_[0]; %s };\n" . " Scalar::Util::weaken(\$_[0]->{%s}) if ref \$_[0]->{%s};\n" . " \$_[0]->{%s};\n" . " } unless \$#_ > 0 or defined \$_[0]->{%s};\n", return_if_get => " return \$_[0]->{%s} unless \$#_ > 0;\n", set => " \$_[0]->{%s} = \$_[1];\n", weaken => " Scalar::Util::weaken(\$_[0]->{%s}) if ref \$_[0]->{%s};\n", sub_end => " return \$_[0]->{%s};\n}\n", ); sub field { my $package = caller; my ($args, @values) = do { no warnings; local *boolean_arguments = sub { (qw(-weak)) }; local *paired_arguments = sub { (qw(-package -init)) }; Spiffy->parse_arguments(@_); }; my ($field, $default) = @values; $package = $args->{-package} if defined $args->{-package}; die "Cannot have a default for a weakened field ($field)" if defined $default && $args->{-weak}; return if defined &{"${package}::$field"}; require Scalar::Util if $args->{-weak}; my $default_string = ( ref($default) eq 'ARRAY' and not @$default ) ? '[]' : (ref($default) eq 'HASH' and not keys %$default ) ? '{}' : default_as_code($default); my $code = $code{sub_start}; if ($args->{-init}) { my $fragment = $args->{-weak} ? $code{weak_init} : $code{init}; my @count = ($fragment =~ /(%s)/g); $code .= sprintf $fragment, $field, $args->{-init}, ($field) x (@count - 2); } $code .= sprintf $code{set_default}, $field, $default_string, $field if defined $default; $code .= sprintf $code{return_if_get}, $field; $code .= sprintf $code{set}, $field; $code .= sprintf $code{weaken}, $field, $field if $args->{-weak}; $code .= sprintf $code{sub_end}, $field; my $sub = eval $code; die $@ if $@; no strict 'refs'; *{"${package}::$field"} = $sub; return $code if defined wantarray; } sub default_as_code { require Data::Dumper; local $Data::Dumper::Sortkeys = 1; my $code = Data::Dumper::Dumper(shift); $code =~ s/^\$VAR1 = //; $code =~ s/;$//; return $code; } sub const { my $package = caller; my ($args, @values) = do { no warnings; local *paired_arguments = sub { (qw(-package)) }; Spiffy->parse_arguments(@_); }; my ($field, $default) = @values; $package = $args->{-package} if defined $args->{-package}; no strict 'refs'; return if defined &{"${package}::$field"}; *{"${package}::$field"} = sub { $default } } sub stub { my $package = caller; my ($args, @values) = do { no warnings; local *paired_arguments = sub { (qw(-package)) }; Spiffy->parse_arguments(@_); }; my ($field, $default) = @values; $package = $args->{-package} if defined $args->{-package}; no strict 'refs'; return if defined &{"${package}::$field"}; *{"${package}::$field"} = sub { require Carp; Carp::confess "Method $field in package $package must be subclassed"; } } sub parse_arguments { my $class = shift; my ($args, @values) = ({}, ()); my %booleans = map { ($_, 1) } $class->boolean_arguments; my %pairs = map { ($_, 1) } $class->paired_arguments; while (@_) { my $elem = shift; if (defined $elem and defined $booleans{$elem}) { $args->{$elem} = (@_ and $_[0] =~ /^[01]$/) ? shift : 1; } elsif (defined $elem and defined $pairs{$elem} and @_) { $args->{$elem} = shift; } else { push @values, $elem; } } return wantarray ? ($args, @values) : $args; } sub boolean_arguments { () } sub paired_arguments { () } # get a unique id for any node sub id { if (not ref $_[0]) { return 'undef' if not defined $_[0]; \$_[0] =~ /\((\w+)\)$/o or die; return "$1-S"; } require overload; overload::StrVal($_[0]) =~ /\((\w+)\)$/o or die; return $1; } #=============================================================================== # It's super, man. #=============================================================================== package DB; { no warnings 'redefine'; sub super_args { my @dummy = caller(@_ ? $_[0] : 2); return @DB::args; } } package Spiffy; sub super { my $method; my $frame = 1; while ($method = (caller($frame++))[3]) { $method =~ s/.*::// and last; } my @args = DB::super_args($frame); @_ = @_ ? ($args[0], @_) : @args; my $class = ref $_[0] ? ref $_[0] : $_[0]; my $caller_class = caller; my $seen = 0; my @super_classes = reverse grep { ($seen or $seen = ($_ eq $caller_class)) ? 0 : 1; } reverse @{all_my_bases($class)}; for my $super_class (@super_classes) { no strict 'refs'; next if $super_class eq $class; if (defined &{"${super_class}::$method"}) { ${"$super_class\::AUTOLOAD"} = ${"$class\::AUTOLOAD"} if $method eq 'AUTOLOAD'; return &{"${super_class}::$method"}; } } return; } #=============================================================================== # This code deserves a spanking, because it is being very naughty. # It is exchanging base.pm's import() for its own, so that people # can use base.pm with Spiffy modules, without being the wiser. #=============================================================================== my $real_base_import; my $real_mixin_import; BEGIN { require base unless defined $INC{'base.pm'}; $INC{'mixin.pm'} ||= 'Spiffy/mixin.pm'; $real_base_import = \&base::import; $real_mixin_import = \&mixin::import; no warnings; *base::import = \&spiffy_base_import; *mixin::import = \&spiffy_mixin_import; } # my $i = 0; # while (my $caller = caller($i++)) { # next unless $caller eq 'base' or $caller eq 'mixin'; # croak <isa('Spiffy'); } @base_classes; my $inheritor = caller(0); for my $base_class (@base_classes) { next if $inheritor->isa($base_class); croak "Can't mix Spiffy and non-Spiffy classes in 'use base'.\n", "See the documentation of Spiffy.pm for details\n " unless $base_class->isa('Spiffy'); $stack_frame = 1; # tell import to use different caller import($base_class, '-base'); $stack_frame = 0; } } sub mixin { my $self = shift; my $target_class = ref($self); spiffy_mixin_import($target_class, @_) } sub spiffy_mixin_import { my $target_class = shift; $target_class = caller(0) if $target_class eq 'mixin'; my $mixin_class = shift or die "Nothing to mixin"; eval "require $mixin_class"; my @roles = @_; my $pseudo_class = join '-', $target_class, $mixin_class, @roles; my %methods = spiffy_mixin_methods($mixin_class, @roles); no strict 'refs'; no warnings; @{"$pseudo_class\::ISA"} = @{"$target_class\::ISA"}; @{"$target_class\::ISA"} = ($pseudo_class); for (keys %methods) { *{"$pseudo_class\::$_"} = $methods{$_}; } } sub spiffy_mixin_methods { my $mixin_class = shift; no strict 'refs'; my %methods = spiffy_all_methods($mixin_class); map { $methods{$_} ? ($_, \ &{"$methods{$_}\::$_"}) : ($_, \ &{"$mixin_class\::$_"}) } @_ ? (get_roles($mixin_class, @_)) : (keys %methods); } sub get_roles { my $mixin_class = shift; my @roles = @_; while (grep /^!*:/, @roles) { @roles = map { s/!!//g; /^!:(.*)/ ? do { my $m = "_role_$1"; map("!$_", $mixin_class->$m); } : /^:(.*)/ ? do { my $m = "_role_$1"; ($mixin_class->$m); } : ($_) } @roles; } if (@roles and $roles[0] =~ /^!/) { my %methods = spiffy_all_methods($mixin_class); unshift @roles, keys(%methods); } my %roles; for (@roles) { s/!!//g; delete $roles{$1}, next if /^!(.*)/; $roles{$_} = 1; } keys %roles; } sub spiffy_all_methods { no strict 'refs'; my $class = shift; return if $class eq 'Spiffy'; my %methods = map { ($_, $class) } grep { defined &{"$class\::$_"} and not /^_/ } keys %{"$class\::"}; my %super_methods; %super_methods = spiffy_all_methods(${"$class\::ISA"}[0]) if @{"$class\::ISA"}; %{{%super_methods, %methods}}; } # END of naughty code. #=============================================================================== # Debugging support #=============================================================================== sub spiffy_dump { no warnings; if ($dump eq 'dumper') { require Data::Dumper; $Data::Dumper::Sortkeys = 1; $Data::Dumper::Indent = 1; return Data::Dumper::Dumper(@_); } require YAML; $YAML::UseVersion = 0; return YAML::Dump(@_) . "...\n"; } sub at_line_number { my ($file_path, $line_number) = (caller(1))[1,2]; " at $file_path line $line_number\n"; } sub WWW { warn spiffy_dump(@_) . at_line_number; return wantarray ? @_ : $_[0]; } sub XXX { die spiffy_dump(@_) . at_line_number; } sub YYY { print spiffy_dump(@_) . at_line_number; return wantarray ? @_ : $_[0]; } sub ZZZ { require Carp; Carp::confess spiffy_dump(@_); } 1; Spiffy-0.46/MANIFEST0000644000175000017500000000120012373727265012541 0ustar ingyingy# This file was automatically generated by Dist::Zilla::Plugin::Manifest v5.019. CONTRIBUTING Changes LICENSE MANIFEST META.json META.yml Makefile.PL README lib/Spiffy.pm lib/Spiffy.pod lib/Spiffy/mixin.pm t/Filter4.pm t/Filter5.pm t/NonSpiffy.pm t/Something.pm t/Thing.pm t/autoload.t t/base.t t/base2.t t/cascade.t t/const.t t/early.t t/export1.t t/export2.t t/export3.t t/export4.t t/export5.t t/export6.t t/export7.t t/exporter.t t/field.t t/field2.t t/field3.t t/filter.t t/filter2.t t/filter3.t t/filter4.t t/filter5.t t/mixin.t t/mixin2.t t/mixin3.t t/new.t t/package.t t/parse.t t/release-pod-syntax.t t/stub.t t/super.t t/super2.t Spiffy-0.46/Changes0000644000175000017500000000731112373727265012714 0ustar ingyingy0.46 Sat Aug 16 12:19:09 PDT 2014 - Meta 0.0.2 0.45 Sat Aug 16 04:04:07 PDT 2014 - Eliminate spurious trailing whitespace 0.44 Sat Aug 16 02:50:17 PDT 2014 - Eliminate File::Basename from test/ 0.43 Fri Aug 15 20:37:55 PDT 2014 - Add t/000-compile-modules.t 0.42 Sat Aug 9 00:43:22 PDT 2014 - Only support back to 5.8.1 0.41 Thu Aug 7 00:28:54 PDT 2014 - Fix bad encoding in Pod 0.40 Wed Aug 6 10:29:46 PDT 2014 - Fix a bug that was causing lots of warnings in Test::Base on perl 5.21 0.39 Tue Aug 5 10:13:52 PDT 2014 - Add badges to doc 0.38 Mon Aug 4 00:30:13 PDT 2014 - Remove (c) from Copyright 0.37 Wed Jul 30 16:16:56 PDT 2014 - Switch IRC to '#pkg' 0.36 Sat Jul 26 12:01:35 PDT 2014 - Fix email in Meta 0.35 Mon Jul 21 17:15:27 PDT 2014 - Fix Meta and add Contributing 0.34 Wed Jun 18 11:08:07 PDT 2014 - Ship with a META.json file (close 0.33 Wed Jun 18 09:50:05 PDT 2014 - Removed a boilerplate meta field. mst++ 0.32 Tue Jun 17 13:18:12 PDT 2014 - Release with Zilla::Dist 0.31 Thu Aug 2 11:11:11 PDT 2012 - Bring Makefile.PL up to date. - Make tests not use 'B" package. rt72542 - Use Module::Package 0.30 Sun Jan 29 12:18:02 PST 2006 - Use faster runtime code in `field`. 0.29 Thu Jan 19 08:12:06 PST 2006 - Squelch redefine warnings 0.28 Thu Jan 19 07:39:30 PST 2006 - Test patch from Nicholas for older perls 0.27 Wed Jan 18 14:14:52 PST 2006 - The check to make sure Spiffy is loaded before 'base' was flawed. Added new tests, and commented out the flawed code until I can figure out how to do the check properly. The check is just a helper so it shouldn't hurt not to have it. 0.26 Sat Jan 14 05:41:05 PST 2006 - Don't sign the distribution tarball 0.25 Mon Jan 9 20:35:39 PST 2006 - Make Spiffy modules play nice with autouse 0.24 Sat Apr 30 23:12:28 PDT 2005 - Refactored -Base to once and only once - Remove some cruft - Got export algorithm working better 0.23 Sun Apr 17 19:01:11 PDT 2005 - Add a blank return to super - Add the mixin method 0.22 Tue Jan 11 07:22:47 PST 2005 - allow new() to be called from object reference - make -weak and -init work together - add tests for -Base filtering 0.21 Wed Dec 15 04:40:46 PST 2004 - Get rid of spiffy_constructor altogether - Don't use goto in super() anymore, due to irreversible changes in perl 5.8.6 - Generate custom fields base on input. The field function returns the generated code for debugging puposes. (Dave Rolsky) - field supports -weaken and -init flags. - Caching in all_my_bases to speed things up. (Chris Dent) - Filtering now does strict/warnings, '1;\n', and private subs defined with 'my sub ...'. - Add -filter_dump and -filter_save options for filter debugging 0.20 Sat Jul 24 22:11:48 PDT 2004 - call super from eval - -XXX implies :XXX 0.19 Sun Jul 11 01:41:38 PDT 2004 - mixin support - don't export spiffy_constructor by default - Don't export XXX stuff until -XXX used - add support for roles and import lists with mixins - super works with AUTOLOAD 0.18 Wed Jun 2 15:05:05 PDT 2004 - Allow 'use base' to work with Spiffy and non-Spiffy - Allow DATA filehandle to work with source filtering - Support @EXPORT_BASE 0.17 Tue May 11 17:09:00 PDT 2004 - Don't filter a file twice. - Make super nestable. - Data::Dumper support 0.16 Fri May 7 00:57:06 PDT 2004 - Added filtering - fixed super - overhauled documentation 0.15 Sun Mar 21 01:54:07 PST 2004 - Make parse_arguments work in scalar context 0.14 Sun Mar 21 01:54:07 PST 2004 - add support for field and const - field -package Foo 'field1'; 0.13 Sat Mar 13 09:13:01 PST 2004 - Works with base.pm 0.12 Mon Mar 8 11:34:57 PST 2004 - Foo::Bar->base; 0.11 Thu Feb 12 21:36:13 CST 2004 - Use Exporter to export things Spiffy-0.46/CONTRIBUTING0000644000175000017500000000234512373727265013255 0ustar ingyingyContributing ============ The "Spiffy" Project needs your help! Please consider being a contributor. This file contains instructions that will help you be an effective contributor to the Project. GitHub ------ The code for this Project is hosted at GitHub. The repository is: http://github.com/ingydotnet/spiffy-pm You can get the code with this command: git clone http://github.com/ingydotnet/spiffy-pm If you've found a bug or a missing feature that you would like the author to know about, report it here: https://github.com/ingydotnet/spiffy-pm/issues or fix it and submit a pull request here: http://github.com/ingydotnet/spiffy-pm/pulls See these links for help on interacting with GitHub: * https://help.github.com/ * https://help.github.com/articles/creating-a-pull-request Zilla::Dist ----------- This Project uses Zilla::Dist to prepare it for publishing to CPAN. Read: https://metacpan.org/pod/distribution/Zilla-Dist/lib/Zilla/Dist/Contributing.pod for up-to-date instructions on what contributors like yourself need to know to use it. IRC --- Spiffy has an IRC channel where you can find real people to help you: irc.freenode.net#pkg Join the channel. Join the team! Thanks in advance, Ingy döt Net Spiffy-0.46/META.json0000644000175000017500000000236412373727265013045 0ustar ingyingy{ "abstract" : "Spiffy Perl Interface Framework For You", "author" : [ "Ingy döt Net " ], "dynamic_config" : 0, "generated_by" : "Dist::Zilla version 5.019, CPAN::Meta::Converter version 2.132830", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Spiffy", "no_index" : { "directory" : [ "inc", "t", "xt", "example" ] }, "prereqs" : { "configure" : { "requires" : { "ExtUtils::MakeMaker" : "6.30" } }, "develop" : { "requires" : { "Test::Pod" : "1.41" } }, "runtime" : { "requires" : { "perl" : "v5.8.1" } } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/ingydotnet/spiffy-pm/issues" }, "homepage" : "https://github.com/ingydotnet/spiffy-pm", "repository" : { "type" : "git", "url" : "https://github.com/ingydotnet/spiffy-pm.git", "web" : "https://github.com/ingydotnet/spiffy-pm" } }, "version" : "0.46" }