9 our @EXPORT_BASE = qw(field const stub super);
10 our @EXPORT_OK = (@EXPORT_BASE, qw(id WWW XXX YYY ZZZ));
11 our %EXPORT_TAGS = (XXX => [qw(WWW XXX YYY ZZZ)]);
17 sub WWW; sub XXX; sub YYY; sub ZZZ;
19 # This line is here to convince "autouse" into believing we are autousable.
21 ($_[1] eq 'import' and caller()->isa('autouse'))
22 ? \&Exporter::import # pacify autouse's equality test
23 : $_[0]->SUPER::can($_[1]) # normal case
28 # Exported functions like field and super should be hidden so as not to
29 # be confused with methods that can be inherited.
34 $class = ref($class) || $class;
35 my $self = bless {}, $class;
38 $self->$method(shift);
43 my $filtered_files = {};
46 our $filter_result = '';
50 my $self_package = shift;
52 # XXX Using parse_arguments here might cause confusion, because the
53 # subclass's boolean_arguments and paired_arguments can conflict, causing
54 # difficult debugging. Consider using something truly local.
55 my ($args, @export_list) = do {
56 local *boolean_arguments = sub {
58 -base -Base -mixin -selfless
60 -filter_dump -filter_save
63 local *paired_arguments = sub { qw(-package) };
64 $self_package->parse_arguments(@_);
66 return spiffy_mixin_import(scalar(caller(0)), $self_package, @export_list)
69 $filter_dump = 1 if $args->{-filter_dump};
70 $filter_save = 1 if $args->{-filter_save};
71 $dump = 'yaml' if $args->{-yaml};
72 $dump = 'dumper' if $args->{-dumper};
74 local @EXPORT_BASE = @EXPORT_BASE;
77 push @EXPORT_BASE, @{$EXPORT_TAGS{XXX}}
78 unless grep /^XXX$/, @EXPORT_BASE;
82 if ($args->{-selfless} or $args->{-Base}) and
83 not $filtered_files->{(caller($stack_frame))[1]}++;
85 my $caller_package = $args->{-package} || caller($stack_frame);
86 push @{"$caller_package\::ISA"}, $self_package
87 if $args->{-Base} or $args->{-base};
89 for my $class (@{all_my_bases($self_package)}) {
90 next unless $class->isa('Spiffy');
92 not defined &{"$caller_package\::$_"};
93 } ( @{"$class\::EXPORT"},
94 ($args->{-Base} or $args->{-base})
95 ? @{"$class\::EXPORT_BASE"} : (),
97 my @export_ok = grep {
98 not defined &{"$caller_package\::$_"};
99 } @{"$class\::EXPORT_OK"};
101 # Avoid calling the expensive Exporter::export
102 # if there is nothing to do (optimization)
103 my %exportable = map { ($_, 1) } @export, @export_ok;
104 next unless keys %exportable;
106 my @export_save = @{"$class\::EXPORT"};
107 my @export_ok_save = @{"$class\::EXPORT_OK"};
108 @{"$class\::EXPORT"} = @export;
109 @{"$class\::EXPORT_OK"} = @export_ok;
111 (my $v = $_) =~ s/^[\!\:]//;
112 $exportable{$v} or ${"$class\::EXPORT_TAGS"}{$v};
114 Exporter::export($class, $caller_package, @list);
115 @{"$class\::EXPORT"} = @export_save;
116 @{"$class\::EXPORT_OK"} = @export_ok_save;
121 require Filter::Util::Call;
123 Filter::Util::Call::filter_add(
126 my ($data, $end) = ('', '');
127 while (my $status = Filter::Util::Call::filter_read()) {
128 return $status if $status < 0;
129 if (/^__(?:END|DATA)__\r?$/) {
138 s[^(sub\s+\w+\s+\{)(.*\n)]
139 [${1}my \$self = shift;$2]gm;
140 s[^(sub\s+\w+)\s*\(\s*\)(\s+\{.*\n)]
142 s[^my\s+sub\s+(\w+)(\s+\{)(.*)((?s:.*?\n))\}\n]
143 [push @my_subs, $1; "\$$1 = sub$2my \$self = shift;$3$4\};\n"]gem;
146 $preclare = join ',', map "\$$_", @my_subs;
147 $preclare = "my($preclare);";
149 $_ = "use strict;use warnings;$preclare${_};1;\n$end";
150 if ($filter_dump) { print; exit }
151 if ($filter_save) { $filter_result = $_; $_ = $filter_result; }
165 return $bases_map->{$class}
166 if defined $bases_map->{$class};
168 my @bases = ($class);
170 for my $base_class (@{"${class}::ISA"}) {
171 push @bases, @{all_my_bases($base_class)};
174 $bases_map->{$class} = [grep {not $used->{$_}++} @bases];
181 " \$_[0]->{%s} = %s\n unless exists \$_[0]->{%s};\n",
183 " return \$_[0]->{%s} = do { my \$self = \$_[0]; %s }\n" .
184 " unless \$#_ > 0 or defined \$_[0]->{%s};\n",
187 " \$_[0]->{%s} = do { my \$self = \$_[0]; %s };\n" .
188 " Scalar::Util::weaken(\$_[0]->{%s}) if ref \$_[0]->{%s};\n" .
190 " } unless \$#_ > 0 or defined \$_[0]->{%s};\n",
192 " return \$_[0]->{%s} unless \$#_ > 0;\n",
194 " \$_[0]->{%s} = \$_[1];\n",
196 " Scalar::Util::weaken(\$_[0]->{%s}) if ref \$_[0]->{%s};\n",
198 " return \$_[0]->{%s};\n}\n",
202 my $package = caller;
203 my ($args, @values) = do {
205 local *boolean_arguments = sub { (qw(-weak)) };
206 local *paired_arguments = sub { (qw(-package -init)) };
207 Spiffy->parse_arguments(@_);
209 my ($field, $default) = @values;
210 $package = $args->{-package} if defined $args->{-package};
211 die "Cannot have a default for a weakened field ($field)"
212 if defined $default && $args->{-weak};
213 return if defined &{"${package}::$field"};
214 require Scalar::Util if $args->{-weak};
216 ( ref($default) eq 'ARRAY' and not @$default )
218 : (ref($default) eq 'HASH' and not keys %$default )
220 : default_as_code($default);
222 my $code = $code{sub_start};
223 if ($args->{-init}) {
224 my $fragment = $args->{-weak} ? $code{weak_init} : $code{init};
225 $code .= sprintf $fragment, $field, $args->{-init}, ($field) x 4;
227 $code .= sprintf $code{set_default}, $field, $default_string, $field
229 $code .= sprintf $code{return_if_get}, $field;
230 $code .= sprintf $code{set}, $field;
231 $code .= sprintf $code{weaken}, $field, $field
233 $code .= sprintf $code{sub_end}, $field;
235 my $sub = eval $code;
238 *{"${package}::$field"} = $sub;
239 return $code if defined wantarray;
242 sub default_as_code {
243 require Data::Dumper;
244 local $Data::Dumper::Sortkeys = 1;
245 my $code = Data::Dumper::Dumper(shift);
246 $code =~ s/^\$VAR1 = //;
252 my $package = caller;
253 my ($args, @values) = do {
255 local *paired_arguments = sub { (qw(-package)) };
256 Spiffy->parse_arguments(@_);
258 my ($field, $default) = @values;
259 $package = $args->{-package} if defined $args->{-package};
261 return if defined &{"${package}::$field"};
262 *{"${package}::$field"} = sub { $default }
266 my $package = caller;
267 my ($args, @values) = do {
269 local *paired_arguments = sub { (qw(-package)) };
270 Spiffy->parse_arguments(@_);
272 my ($field, $default) = @values;
273 $package = $args->{-package} if defined $args->{-package};
275 return if defined &{"${package}::$field"};
276 *{"${package}::$field"} =
280 "Method $field in package $package must be subclassed";
284 sub parse_arguments {
286 my ($args, @values) = ({}, ());
287 my %booleans = map { ($_, 1) } $class->boolean_arguments;
288 my %pairs = map { ($_, 1) } $class->paired_arguments;
291 if (defined $elem and defined $booleans{$elem}) {
292 $args->{$elem} = (@_ and $_[0] =~ /^[01]$/)
296 elsif (defined $elem and defined $pairs{$elem} and @_) {
297 $args->{$elem} = shift;
303 return wantarray ? ($args, @values) : $args;
306 sub boolean_arguments { () }
307 sub paired_arguments { () }
309 # get a unique id for any node
312 return 'undef' if not defined $_[0];
313 \$_[0] =~ /\((\w+)\)$/o or die;
317 overload::StrVal($_[0]) =~ /\((\w+)\)$/o or die;
321 #===============================================================================
323 #===============================================================================
326 no warnings 'redefine';
328 my @dummy = caller(@_ ? $_[0] : 2);
337 while ($method = (caller($frame++))[3]) {
338 $method =~ s/.*::// and last;
340 my @args = DB::super_args($frame);
341 @_ = @_ ? ($args[0], @_) : @args;
342 my $class = ref $_[0] ? ref $_[0] : $_[0];
343 my $caller_class = caller;
345 my @super_classes = reverse grep {
346 ($seen or $seen = ($_ eq $caller_class)) ? 0 : 1;
347 } reverse @{all_my_bases($class)};
348 for my $super_class (@super_classes) {
350 next if $super_class eq $class;
351 if (defined &{"${super_class}::$method"}) {
352 ${"$super_class\::AUTOLOAD"} = ${"$class\::AUTOLOAD"}
353 if $method eq 'AUTOLOAD';
354 return &{"${super_class}::$method"};
360 #===============================================================================
361 # This code deserves a spanking, because it is being very naughty.
362 # It is exchanging base.pm's import() for its own, so that people
363 # can use base.pm with Spiffy modules, without being the wiser.
364 #===============================================================================
365 my $real_base_import;
366 my $real_mixin_import;
369 require base unless defined $INC{'base.pm'};
370 $INC{'mixin.pm'} ||= 'Spiffy/mixin.pm';
371 $real_base_import = \&base::import;
372 $real_mixin_import = \&mixin::import;
374 *base::import = \&spiffy_base_import;
375 *mixin::import = \&spiffy_mixin_import;
379 # while (my $caller = caller($i++)) {
380 # next unless $caller eq 'base' or $caller eq 'mixin';
382 # Spiffy.pm must be loaded before calling 'use base' or 'use mixin' with a
383 # Spiffy module. See the documentation of Spiffy.pm for details.
387 sub spiffy_base_import {
388 my @base_classes = @_;
391 goto &$real_base_import
393 eval "require $_" unless %{"$_\::"};
396 my $inheritor = caller(0);
397 for my $base_class (@base_classes) {
398 next if $inheritor->isa($base_class);
399 croak "Can't mix Spiffy and non-Spiffy classes in 'use base'.\n",
400 "See the documentation of Spiffy.pm for details\n "
401 unless $base_class->isa('Spiffy');
402 $stack_frame = 1; # tell import to use different caller
403 import($base_class, '-base');
410 my $target_class = ref($self);
411 spiffy_mixin_import($target_class, @_)
414 sub spiffy_mixin_import {
415 my $target_class = shift;
416 $target_class = caller(0)
417 if $target_class eq 'mixin';
418 my $mixin_class = shift
419 or die "Nothing to mixin";
420 eval "require $mixin_class";
422 my $pseudo_class = join '-', $target_class, $mixin_class, @roles;
423 my %methods = spiffy_mixin_methods($mixin_class, @roles);
426 @{"$pseudo_class\::ISA"} = @{"$target_class\::ISA"};
427 @{"$target_class\::ISA"} = ($pseudo_class);
428 for (keys %methods) {
429 *{"$pseudo_class\::$_"} = $methods{$_};
433 sub spiffy_mixin_methods {
434 my $mixin_class = shift;
436 my %methods = spiffy_all_methods($mixin_class);
439 ? ($_, \ &{"$methods{$_}\::$_"})
440 : ($_, \ &{"$mixin_class\::$_"})
442 ? (get_roles($mixin_class, @_))
447 my $mixin_class = shift;
449 while (grep /^!*:/, @roles) {
454 map("!$_", $mixin_class->$m);
463 if (@roles and $roles[0] =~ /^!/) {
464 my %methods = spiffy_all_methods($mixin_class);
465 unshift @roles, keys(%methods);
470 delete $roles{$1}, next
477 sub spiffy_all_methods {
480 return if $class eq 'Spiffy';
484 defined &{"$class\::$_"} and not /^_/
485 } keys %{"$class\::"};
487 %super_methods = spiffy_all_methods(${"$class\::ISA"}[0])
488 if @{"$class\::ISA"};
489 %{{%super_methods, %methods}};
493 # END of naughty code.
494 #===============================================================================
496 #===============================================================================
499 if ($dump eq 'dumper') {
500 require Data::Dumper;
501 $Data::Dumper::Sortkeys = 1;
502 $Data::Dumper::Indent = 1;
503 return Data::Dumper::Dumper(@_);
506 $YAML::UseVersion = 0;
507 return YAML::Dump(@_) . "...\n";
511 my ($file_path, $line_number) = (caller(1))[1,2];
512 " at $file_path line $line_number\n";
516 warn spiffy_dump(@_) . at_line_number;
517 return wantarray ? @_ : $_[0];
521 die spiffy_dump(@_) . at_line_number;
525 print spiffy_dump(@_) . at_line_number;
526 return wantarray ? @_ : $_[0];
531 Carp::confess spiffy_dump(@_);
540 Spiffy - Spiffy Perl Interface Framework For You
550 if ($self->mood eq ':-(') {
559 "Spiffy" is a framework and methodology for doing object oriented (OO)
560 programming in Perl. Spiffy combines the best parts of Exporter.pm,
561 base.pm, mixin.pm and SUPER.pm into one magic foundation class. It
562 attempts to fix all the nits and warts of traditional Perl OO, in a
563 clean, straightforward and (perhaps someday) standard way.
565 Spiffy borrows ideas from other OO languages like Python, Ruby,
566 Java and Perl 6. It also adds a few tricks of its own.
568 If you take a look on CPAN, there are a ton of OO related modules. When
569 starting a new project, you need to pick the set of modules that makes
570 most sense, and then you need to use those modules in each of your
571 classes. Spiffy, on the other hand, has everything you'll probably need
572 in one module, and you only need to use it once in one of your classes.
573 If you make Spiffy.pm the base class of the basest class in your
574 project, Spiffy will automatically pass all of its magic to all of your
575 subclasses. You may eventually forget that you're even using it!
577 The most striking difference between Spiffy and other Perl object
578 oriented base classes, is that it has the ability to export things.
579 If you create a subclass of Spiffy, all the things that Spiffy
580 exports will automatically be exported by your subclass, in addition to
581 any more things that you want to export. And if someone creates a
582 subclass of your subclass, all of those things will be exported
583 automatically, and so on. Think of it as "Inherited Exportation", and it
584 uses the familiar Exporter.pm specification syntax.
586 To use Spiffy or any subclass of Spiffy as a base class of your class,
587 you specify the C<-base> argument to the C<use> command.
589 use MySpiffyBaseModule -base;
591 You can also use the traditional C<use base 'MySpiffyBaseModule';>
592 syntax and everything will work exactly the same. The only caveat is
593 that Spiffy.pm must already be loaded. That's because Spiffy rewires
594 base.pm on the fly to do all the Spiffy magics.
596 Spiffy has support for Ruby-like mixins with Perl6-like roles. Just like
597 C<base> you can use either of the following invocations:
599 use mixin 'MySpiffyBaseModule';
600 use MySpiffyBaseModule -mixin;
602 The second version will only work if the class being mixed in is a
603 subclass of Spiffy. The first version will work in all cases, as long
604 as Spiffy has already been loaded.
606 To limit the methods that get mixed in, use roles. (Hint: they work just like
609 use MySpiffyBaseModule -mixin => qw(:basics x y !foo);
611 In object oriented Perl almost every subroutine is a method. Each method
612 gets the object passed to it as its first argument. That means
613 practically every subroutine starts with the line:
617 Spiffy provides a simple, optional filter mechanism to insert that line
618 for you, resulting in cleaner code. If you figure an average method has
619 10 lines of code, that's 10% of your code! To turn this option on, you
620 just use the C<-Base> option instead of the C<-base> option, or add the
621 C<-selfless> option. If source filtering makes you queazy, don't use the
622 feature. I personally find it addictive in my quest for writing squeaky
623 clean, maintainable code.
625 A useful feature of Spiffy is that it exports two functions: C<field>
626 and C<const> that can be used to declare the attributes of your class,
627 and automatically generate accessor methods for them. The only
628 difference between the two functions is that C<const> attributes can not
629 be modified; thus the accessor is much faster.
631 One interesting aspect of OO programming is when a method calls the same
632 method from a parent class. This is generally known as calling a super
633 method. Perl's facility for doing this is butt ugly:
638 $self->SUPER::cleanup(@_);
641 Spiffy makes it, er, super easy to call super methods. You just use
642 the C<super> function. You don't need to pass it any arguments
643 because it automatically passes them on for you. Here's the same
644 function with Spiffy:
651 Spiffy has a special method for parsing arguments called
652 C<parse_arguments>, that it also uses for parsing its own arguments. You
653 declare which arguments are boolean (singletons) and which ones are
654 paired, with two special methods called C<boolean_arguments> and
655 C<paired_arguments>. Parse arguments pulls out the booleans and pairs
656 and returns them in an anonymous hash, followed by a list of the
659 Finally, Spiffy can export a few debugging functions C<WWW>, C<XXX>,
660 C<YYY> and C<ZZZ>. Each of them produces a YAML dump of its arguments.
661 WWW warns the output, XXX dies with the output, YYY prints the output,
662 and ZZZ confesses the output. If YAML doesn't suit your needs, you can
663 switch all the dumps to Data::Dumper format with the C<-dumper> option.
667 =head1 Spiffy EXPORTING
669 Spiffy implements a completely new idea in Perl. Modules that act both
670 as object oriented classes and that also export functions. But it
671 takes the concept of Exporter.pm one step further; it walks the entire
672 C<@ISA> path of a class and honors the export specifications of each
673 module. Since Spiffy calls on the Exporter module to do this, you can
674 use all the fancy interface features that Exporter has, including tags
677 Spiffy considers all the arguments that don't begin with a dash to
678 comprise the export specification.
682 our $SERIAL_NUMBER = 0;
683 our @EXPORT = qw($SERIAL_NUMBER);
684 our @EXPORT_BASE = qw(tire horn);
687 use Vehicle -base, '!field';
688 $self->inflate(tire);
690 In this case, C<Bicycle->isa('Vehicle')> and also all the things
691 that C<Vehicle> and C<Spiffy> export, will go into C<Bicycle>,
694 Exporting can be very helpful when you've designed a system with
695 hundreds of classes, and you want them all to have access to some
696 functions or constants or variables. Just export them in your main base
697 class and every subclass will get the functions they need.
699 You can do almost everything that Exporter does because Spiffy delegates
700 the job to Exporter (after adding some Spiffy magic). Spiffy offers a
701 C<@EXPORT_BASE> variable which is like C<@EXPORT>, but only for usages
704 =head1 Spiffy MIXINs & ROLEs
706 If you've done much OO programming in Perl you've probably used Multiple
707 Inheritance (MI), and if you've done much MI you've probably run into
708 weird problems and headaches. Some languages like Ruby, attempt to
709 resolve MI issues using a technique called mixins. Basically, all Ruby
710 classes use only Single Inheritance (SI), and then I<mixin>
711 functionality from other modules if they need to.
713 Mixins can be thought of at a simplistic level as I<importing> the
714 methods of another class into your subclass. But from an implementation
715 standpoint that's not the best way to do it. Spiffy does what Ruby
716 does. It creates an empty anonymous class, imports everything into that
717 class, and then chains the new class into your SI ISA path. In other
725 You end up with a single inheritance chain of classes like this:
727 A << A-D << A-C << B;
729 C<A-D> and C<A-C> are the actual package names of the generated
730 classes. The nice thing about this style is that mixing in C doesn't
731 clobber any methods in A, and D doesn't conflict with A or C either. If
732 you mixed in a method in C that was also in A, you can still get to it
735 When Spiffy mixes in C, it pulls in all the methods in C that do not
736 begin with an underscore. Actually it goes farther than that. If C is a
737 subclass it will pull in every method that C C<can> do through
738 inheritance. This is very powerful, maybe too powerful.
740 To limit what you mixin, Spiffy borrows the concept of Roles from
741 Perl6. The term role is used more loosely in Spiffy though. It's much
742 like an import list that the Exporter module uses, and you can use
743 groups (tags) and negation. If the first element of your list uses
744 negation, Spiffy will start with all the methods that your mixin
747 use E -mixin => qw(:tools walk !run !:sharp_tools);
749 In this example, C<walk> and C<run> are methods that E can do, and
750 C<tools> and C<sharp_tools> are roles of class E. How does class E
751 define these roles? It very simply defines methods called C<_role_tools>
752 and C<_role_sharp_tools> which return lists of more methods. (And
753 possibly other roles!) The neat thing here is that since roles are just
754 methods, they too can be inherited. Take B<that> Perl6!
756 =head1 Spiffy FILTERING
758 By using the C<-Base> flag instead of C<-base> you never need to write the
763 This statement is added to every subroutine in your class by using a source
764 filter. The magic is simple and fast, so there is litte performance penalty
765 for creating clean code on par with Ruby and Python.
778 is exactly the same as:
782 use strict;use warnings;
783 sub crazy {my $self = shift;
786 sub wacky {my $self = shift; }
792 Note that the empty parens after the subroutine C<new> keep it from
793 having a $self added. Also note that the extra code is added to existing
794 lines to ensure that line numbers are not altered.
796 C<-Base> also turns on the strict and warnings pragmas, and adds that
797 annoying '1;' line to your module.
799 =head1 PRIVATE METHODS
801 Spiffy now has support for private methods when you use the '-Base' filter
802 mechanism. You just declare the subs with the C<my> keyword, and call them
803 with a C<'$'> in front. Like this:
806 use SomethingSpiffy -Base;
808 # normal public method
813 # private lexical method. uncallable from outside this file.
818 =head1 Spiffy DEBUGGING
820 The XXX function is very handy for debugging because you can insert it
821 almost anywhere, and it will dump your data in nice clean YAML. Take the
824 my @stuff = grep { /keen/ } $self->find($a, $b);
826 If you have a problem with this statement, you can debug it in any of the
829 XXX my @stuff = grep { /keen/ } $self->find($a, $b);
830 my @stuff = XXX grep { /keen/ } $self->find($a, $b);
831 my @stuff = grep { /keen/ } XXX $self->find($a, $b);
832 my @stuff = grep { /keen/ } $self->find(XXX $a, $b);
834 XXX is easy to insert and remove. It is also a tradition to mark
835 uncertain areas of code with XXX. This will make the debugging dumpers
836 easy to spot if you forget to take them out.
838 WWW and YYY are nice because they dump their arguments and then return the
839 arguments. This way you can insert them into many places and still have the
840 code run as before. Use ZZZ when you need to die with both a YAML dump and a
843 The debugging functions are exported by default if you use the C<-base>
844 option, but only if you have previously used the C<-XXX> option. To
845 export all 4 functions use the export tag:
847 use SomeSpiffyModule ':XXX';
849 To force the debugging functions to use Data::Dumper instead of YAML:
851 use SomeSpiffyModule -dumper;
853 =head1 Spiffy FUNCTIONS
855 This section describes the functions the Spiffy exports. The C<field>,
856 C<const>, C<stub> and C<super> functions are only exported when you use
857 the C<-base> or C<-Base> options.
863 Defines accessor methods for a field of your class:
873 push @{$self->{bar}}, $self->foo;
876 The first parameter passed to C<field> is the name of the attribute
877 being defined. Accessors can be given an optional default value.
878 This value will be returned if no value for the field has been set
885 The C<const> function is similar to <field> except that it is immutable.
886 It also does not store data in the object. You probably always want to
887 give a C<const> a default value, otherwise the generated method will be
894 The C<stub> function generates a method that will die with an
895 appropriate message. The idea is that subclasses must implement these
896 methods so that the stub methods don't get called.
900 If this function is called without any arguments, it will call the same
901 method that it is in, higher up in the ISA tree, passing it all the
902 same arguments. If it is called with arguments, it will use those
903 arguments with C<$self> in the front. In other words, it just works
907 super; # Same as $self->SUPER::foo(@_);
908 super('hello'); # Same as $self->SUPER::foo('hello');
918 C<super> will simply do nothing if there is no super method. Finally,
919 C<super> does the right thing in AUTOLOAD subroutines.
923 =head1 Spiffy METHODS
925 This section lists all of the methods that any subclass of Spiffy
926 automatically inherits.
932 A method to mixin a class at runtime. Takes the same arguments as C<use
933 mixin ...>. Makes the target class a mixin of the caller.
935 $self->mixin('SomeClass');
936 $object->mixin('SomeOtherClass' => 'some_method');
938 =item * parse_arguments
940 This method takes a list of arguments and groups them into pairs. It
941 allows for boolean arguments which may or may not have a value
942 (defaulting to 1). The method returns a hash reference of all the pairs
943 as keys and values in the hash. Any arguments that cannot be paired, are
944 returned as a list. Here is an example:
946 sub boolean_arguments { qw(-has_spots -is_yummy) }
947 sub paired_arguments { qw(-name -size) }
948 my ($pairs, @others) = $self->parse_arguments(
957 After this call, C<$pairs> will contain:
966 and C<@others> will contain 'red', 'white', and 'black'.
968 =item * boolean_arguments
970 Returns the list of arguments that are recognized as being boolean. Override
971 this method to define your own list.
973 =item * paired_arguments
975 Returns the list of arguments that are recognized as being paired. Override
976 this method to define your own list.
980 =head1 Spiffy ARGUMENTS
982 When you C<use> the Spiffy module or a subclass of it, you can pass it a
983 list of arguments. These arguments are parsed using the
984 C<parse_arguments> method described above. The special argument
985 C<-base>, is used to make the current package a subclass of the Spiffy
988 Any non-paired parameters act like a normal import list; just like those
989 used with the Exporter module.
991 =head1 USING Spiffy WITH base.pm
993 The proper way to use a Spiffy module as a base class is with the C<-base>
994 parameter to the C<use> statement. This differs from typical modules where you
995 would want to C<use base>.
998 use Spiffy::Module -base;
999 use base 'NonSpiffy::Module';
1001 Now it may be hard to keep track of what's Spiffy and what is not.
1002 Therefore Spiffy has actually been made to work with base.pm. You can
1006 use base 'Spiffy::Module';
1007 use base 'NonSpiffy::Module';
1009 C<use base> is also very useful when your class is not an actual module (a
1010 separate file) but just a package in some file that has already been loaded.
1011 C<base> will work whether the class is a module or not, while the C<-base>
1012 syntax cannot work that way, since C<use> always tries to load a module.
1014 =head2 base.pm Caveats
1016 To make Spiffy work with base.pm, a dirty trick was played. Spiffy swaps
1017 C<base::import> with its own version. If the base modules are not Spiffy,
1018 Spiffy calls the original base::import. If the base modules are Spiffy,
1019 then Spiffy does its own thing.
1021 There are two caveats.
1025 =item * Spiffy must be loaded first.
1027 If Spiffy is not loaded and C<use base> is invoked on a Spiffy module,
1028 Spiffy will die with a useful message telling the author to read this
1029 documentation. That's because Spiffy needed to do the import swap
1032 If you get this error, simply put a statement like this up front in
1039 C<base.pm> can take multiple arguments. And this works with Spiffy as
1040 long as all the base classes are Spiffy, or they are all non-Spiffy. If
1041 they are mixed, Spiffy will die. In this case just use separate C<use
1046 =head1 Spiffy TODO LIST
1048 Spiffy is a wonderful way to do OO programming in Perl, but it is still
1049 a work in progress. New things will be added, and things that don't work
1050 well, might be removed.
1054 Ingy döt Net <ingy@cpan.org>
1058 Copyright (c) 2006. Ingy döt Net. All rights reserved.
1059 Copyright (c) 2004. Brian Ingerson. All rights reserved.
1061 This program is free software; you can redistribute it and/or modify it
1062 under the same terms as Perl itself.
1064 See L<http://www.perl.com/perl/misc/Artistic.html>