1Spiffy(3)             User Contributed Perl Documentation            Spiffy(3)
2
3
4

NAME

6       Spiffy - Spiffy Perl Interface Framework For You
7

SYNOPSIS

9           package Keen;
10           use Spiffy -Base;
11           field 'mirth';
12           const mood => ':-)';
13
14           sub happy {
15               if ($self->mood eq ':-(') {
16                   $self->mirth(-1);
17                   print "Cheer up!";
18               }
19               super;
20           }
21

DESCRIPTION

23       "Spiffy" is a framework and methodology for doing object oriented (OO)
24       programming in Perl. Spiffy combines the best parts of Exporter.pm,
25       base.pm, mixin.pm and SUPER.pm into one magic foundation class. It
26       attempts to fix all the nits and warts of traditional Perl OO, in a
27       clean, straightforward and (perhaps someday) standard way.
28
29       Spiffy borrows ideas from other OO languages like Python, Ruby, Java
30       and Perl 6. It also adds a few tricks of its own.
31
32       If you take a look on CPAN, there are a ton of OO related modules. When
33       starting a new project, you need to pick the set of modules that makes
34       most sense, and then you need to use those modules in each of your
35       classes. Spiffy, on the other hand, has everything you'll probably need
36       in one module, and you only need to use it once in one of your classes.
37       If you make Spiffy.pm the base class of the basest class in your
38       project, Spiffy will automatically pass all of its magic to all of your
39       subclasses. You may eventually forget that you're even using it!
40
41       The most striking difference between Spiffy and other Perl object ori‐
42       ented base classes, is that it has the ability to export things.  If
43       you create a subclass of Spiffy, all the things that Spiffy exports
44       will automatically be exported by your subclass, in addition to any
45       more things that you want to export. And if someone creates a subclass
46       of your subclass, all of those things will be exported automatically,
47       and so on. Think of it as "Inherited Exportation", and it uses the
48       familiar Exporter.pm specification syntax.
49
50       To use Spiffy or any subclass of Spiffy as a base class of your class,
51       you specify the "-base" argument to the "use" command.
52
53           use MySpiffyBaseModule -base;
54
55       You can also use the traditional "use base 'MySpiffyBaseModule';" syn‐
56       tax and everything will work exactly the same. The only caveat is that
57       Spiffy.pm must already be loaded. That's because Spiffy rewires base.pm
58       on the fly to do all the Spiffy magics.
59
60       Spiffy has support for Ruby-like mixins with Perl6-like roles. Just
61       like "base" you can use either of the following invocations:
62
63           use mixin 'MySpiffyBaseModule';
64           use MySpiffyBaseModule -mixin;
65
66       The second version will only work if the class being mixed in is a sub‐
67       class of Spiffy.  The first version will work in all cases, as long as
68       Spiffy has already been loaded.
69
70       To limit the methods that get mixed in, use roles. (Hint: they work
71       just like an Exporter list):
72
73           use MySpiffyBaseModule -mixin => qw(:basics x y !foo);
74
75       In object oriented Perl almost every subroutine is a method. Each
76       method gets the object passed to it as its first argument. That means
77       practically every subroutine starts with the line:
78
79            my $self = shift;
80
81       Spiffy provides a simple, optional filter mechanism to insert that line
82       for you, resulting in cleaner code. If you figure an average method has
83       10 lines of code, that's 10% of your code! To turn this option on, you
84       just use the "-Base" option instead of the "-base" option, or add the
85       "-selfless" option. If source filtering makes you queazy, don't use the
86       feature. I personally find it addictive in my quest for writing squeaky
87       clean, maintainable code.
88
89       A useful feature of Spiffy is that it exports two functions: "field"
90       and "const" that can be used to declare the attributes of your class,
91       and automatically generate accessor methods for them. The only differ‐
92       ence between the two functions is that "const" attributes can not be
93       modified; thus the accessor is much faster.
94
95       One interesting aspect of OO programming is when a method calls the
96       same method from a parent class. This is generally known as calling a
97       super method. Perl's facility for doing this is butt ugly:
98
99           sub cleanup {
100               my $self = shift;
101               $self->scrub;
102               $self->SUPER::cleanup(@_);
103           }
104
105       Spiffy makes it, er, super easy to call super methods. You just use the
106       "super" function. You don't need to pass it any arguments because it
107       automatically passes them on for you. Here's the same function with
108       Spiffy:
109
110           sub cleanup {
111               $self->scrub;
112               super;
113           }
114
115       Spiffy has a special method for parsing arguments called "parse_argu‐
116       ments", that it also uses for parsing its own arguments. You declare
117       which arguments are boolean (singletons) and which ones are paired,
118       with two special methods called "boolean_arguments" and "paired_argu‐
119       ments". Parse arguments pulls out the booleans and pairs and returns
120       them in an anonymous hash, followed by a list of the unmatched argu‐
121       ments.
122
123       Finally, Spiffy can export a few debugging functions "WWW", "XXX",
124       "YYY" and "ZZZ". Each of them produces a YAML dump of its arguments.
125       WWW warns the output, XXX dies with the output, YYY prints the output,
126       and ZZZ confesses the output. If YAML doesn't suit your needs, you can
127       switch all the dumps to Data::Dumper format with the "-dumper" option.
128
129       That's Spiffy!
130

Spiffy EXPORTING

132       Spiffy implements a completely new idea in Perl. Modules that act both
133       as object oriented classes and that also export functions. But it takes
134       the concept of Exporter.pm one step further; it walks the entire @ISA
135       path of a class and honors the export specifications of each module.
136       Since Spiffy calls on the Exporter module to do this, you can use all
137       the fancy interface features that Exporter has, including tags and
138       negation.
139
140       Spiffy considers all the arguments that don't begin with a dash to com‐
141       prise the export specification.
142
143           package Vehicle;
144           use Spiffy -base;
145           our $SERIAL_NUMBER = 0;
146           our @EXPORT = qw($SERIAL_NUMBER);
147           our @EXPORT_BASE = qw(tire horn);
148
149           package Bicycle;
150           use Vehicle -base, '!field';
151           $self->inflate(tire);
152
153       In this case, "Bicycle-"isa('Vehicle')> and also all the things that
154       "Vehicle" and "Spiffy" export, will go into "Bicycle", except "field".
155
156       Exporting can be very helpful when you've designed a system with hun‐
157       dreds of classes, and you want them all to have access to some func‐
158       tions or constants or variables. Just export them in your main base
159       class and every subclass will get the functions they need.
160
161       You can do almost everything that Exporter does because Spiffy dele‐
162       gates the job to Exporter (after adding some Spiffy magic). Spiffy
163       offers a @EXPORT_BASE variable which is like @EXPORT, but only for
164       usages that use "-base".
165

Spiffy MIXINs & ROLEs

167       If you've done much OO programming in Perl you've probably used Multi‐
168       ple Inheritance (MI), and if you've done much MI you've probably run
169       into weird problems and headaches. Some languages like Ruby, attempt to
170       resolve MI issues using a technique called mixins. Basically, all Ruby
171       classes use only Single Inheritance (SI), and then mixin functionality
172       from other modules if they need to.
173
174       Mixins can be thought of at a simplistic level as importing the methods
175       of another class into your subclass. But from an implementation stand‐
176       point that's not the best way to do it. Spiffy does what Ruby does. It
177       creates an empty anonymous class, imports everything into that class,
178       and then chains the new class into your SI ISA path. In other words, if
179       you say:
180
181           package A;
182           use B -base;
183           use C -mixin;
184           use D -mixin;
185
186       You end up with a single inheritance chain of classes like this:
187
188           A << A-D << A-C << B;
189
190       "A-D" and "A-C" are the actual package names of the generated classes.
191       The nice thing about this style is that mixing in C doesn't clobber any
192       methods in A, and D doesn't conflict with A or C either. If you mixed
193       in a method in C that was also in A, you can still get to it by using
194       "super".
195
196       When Spiffy mixes in C, it pulls in all the methods in C that do not
197       begin with an underscore. Actually it goes farther than that. If C is a
198       subclass it will pull in every method that C "can" do through inheri‐
199       tance. This is very powerful, maybe too powerful.
200
201       To limit what you mixin, Spiffy borrows the concept of Roles from
202       Perl6. The term role is used more loosely in Spiffy though. It's much
203       like an import list that the Exporter module uses, and you can use
204       groups (tags) and negation. If the first element of your list uses
205       negation, Spiffy will start with all the methods that your mixin class
206       can do.
207
208           use E -mixin => qw(:tools walk !run !:sharp_tools);
209
210       In this example, "walk" and "run" are methods that E can do, and
211       "tools" and "sharp_tools" are roles of class E. How does class E define
212       these roles? It very simply defines methods called "_role_tools" and
213       "_role_sharp_tools" which return lists of more methods. (And possibly
214       other roles!) The neat thing here is that since roles are just methods,
215       they too can be inherited. Take that Perl6!
216

Spiffy FILTERING

218       By using the "-Base" flag instead of "-base" you never need to write
219       the line:
220
221           my $self = shift;
222
223       This statement is added to every subroutine in your class by using a
224       source filter. The magic is simple and fast, so there is litte perfor‐
225       mance penalty for creating clean code on par with Ruby and Python.
226
227           package Example;
228           use Spiffy -Base;
229
230           sub crazy {
231               $self->nuts;
232           }
233           sub wacky { }
234           sub new() {
235               bless [], shift;
236           }
237
238       is exactly the same as:
239
240           package Example;
241           use Spiffy -base;
242           use strict;use warnings;
243           sub crazy {my $self = shift;
244               $self->nuts;
245           }
246           sub wacky {my $self = shift; }
247           sub new {
248               bless [], shift;
249           }
250           ;1;
251
252       Note that the empty parens after the subroutine "new" keep it from hav‐
253       ing a $self added. Also note that the extra code is added to existing
254       lines to ensure that line numbers are not altered.
255
256       "-Base" also turns on the strict and warnings pragmas, and adds that
257       annoying '1;' line to your module.
258

PRIVATE METHODS

260       Spiffy now has support for private methods when you use the '-Base'
261       filter mechanism. You just declare the subs with the "my" keyword, and
262       call them with a '$' in front. Like this:
263
264           package Keen;
265           use SomethingSpiffy -Base;
266
267           # normal public method
268           sub swell {
269               $self->$stinky;
270           }
271
272           # private lexical method. uncallable from outside this file.
273           my sub stinky {
274               ...
275           }
276

Spiffy DEBUGGING

278       The XXX function is very handy for debugging because you can insert it
279       almost anywhere, and it will dump your data in nice clean YAML. Take
280       the following statement:
281
282           my @stuff = grep { /keen/ } $self->find($a, $b);
283
284       If you have a problem with this statement, you can debug it in any of
285       the following ways:
286
287           XXX my @stuff = grep { /keen/ } $self->find($a, $b);
288           my @stuff = XXX grep { /keen/ } $self->find($a, $b);
289           my @stuff = grep { /keen/ } XXX $self->find($a, $b);
290           my @stuff = grep { /keen/ } $self->find(XXX $a, $b);
291
292       XXX is easy to insert and remove. It is also a tradition to mark uncer‐
293       tain areas of code with XXX. This will make the debugging dumpers easy
294       to spot if you forget to take them out.
295
296       WWW and YYY are nice because they dump their arguments and then return
297       the arguments. This way you can insert them into many places and still
298       have the code run as before. Use ZZZ when you need to die with both a
299       YAML dump and a full stack trace.
300
301       The debugging functions are exported by default if you use the "-base"
302       option, but only if you have previously used the "-XXX" option. To
303       export all 4 functions use the export tag:
304
305           use SomeSpiffyModule ':XXX';
306
307       To force the debugging functions to use Data::Dumper instead of YAML:
308
309           use SomeSpiffyModule -dumper;
310

Spiffy FUNCTIONS

312       This section describes the functions the Spiffy exports. The "field",
313       "const", "stub" and "super" functions are only exported when you use
314       the "-base" or "-Base" options.
315
316       * field
317           Defines accessor methods for a field of your class:
318
319               package Example;
320               use Spiffy -Base;
321
322               field 'foo';
323               field bar => [];
324
325               sub lalala {
326                   $self->foo(42);
327                   push @{$self->{bar}}, $self->foo;
328               }
329
330           The first parameter passed to "field" is the name of the attribute
331           being defined. Accessors can be given an optional default value.
332           This value will be returned if no value for the field has been set
333           in the object.
334
335       * const
336               const bar => 42;
337
338           The "const" function is similar to <field> except that it is
339           immutable.  It also does not store data in the object. You probably
340           always want to give a "const" a default value, otherwise the gener‐
341           ated method will be somewhat useless.
342
343       * stub
344               stub 'cigar';
345
346           The "stub" function generates a method that will die with an appro‐
347           priate message. The idea is that subclasses must implement these
348           methods so that the stub methods don't get called.
349
350       * super
351           If this function is called without any arguments, it will call the
352           same method that it is in, higher up in the ISA tree, passing it
353           all the same arguments. If it is called with arguments, it will use
354           those arguments with $self in the front. In other words, it just
355           works like you'd expect.
356
357               sub foo {
358                   super;             # Same as $self->SUPER::foo(@_);
359                   super('hello');    # Same as $self->SUPER::foo('hello');
360                   $self->bar(42);
361               }
362
363               sub new() {
364                   my $self = super;
365                   $self->init;
366                   return $self;
367               }
368
369           "super" will simply do nothing if there is no super method.
370           Finally, "super" does the right thing in AUTOLOAD subroutines.
371

Spiffy METHODS

373       This section lists all of the methods that any subclass of Spiffy auto‐
374       matically inherits.
375
376       * mixin
377           A method to mixin a class at runtime. Takes the same arguments as
378           "use mixin ...". Makes the target class a mixin of the caller.
379
380               $self->mixin('SomeClass');
381               $object->mixin('SomeOtherClass' => 'some_method');
382
383       * parse_arguments
384           This method takes a list of arguments and groups them into pairs.
385           It allows for boolean arguments which may or may not have a value
386           (defaulting to 1). The method returns a hash reference of all the
387           pairs as keys and values in the hash. Any arguments that cannot be
388           paired, are returned as a list. Here is an example:
389
390               sub boolean_arguments { qw(-has_spots -is_yummy) }
391               sub paired_arguments { qw(-name -size) }
392               my ($pairs, @others) = $self->parse_arguments(
393                   'red', 'white',
394                   -name => 'Ingy',
395                   -has_spots =>
396                   -size => 'large',
397                   'black',
398                   -is_yummy => 0,
399               );
400
401           After this call, $pairs will contain:
402
403               {
404                   -name => 'Ingy',
405                   -has_spots => 1,
406                   -size => 'large',
407                   -is_yummy => 0,
408               }
409
410           and @others will contain 'red', 'white', and 'black'.
411
412       * boolean_arguments
413           Returns the list of arguments that are recognized as being boolean.
414           Override this method to define your own list.
415
416       * paired_arguments
417           Returns the list of arguments that are recognized as being paired.
418           Override this method to define your own list.
419

Spiffy ARGUMENTS

421       When you "use" the Spiffy module or a subclass of it, you can pass it a
422       list of arguments. These arguments are parsed using the "parse_argu‐
423       ments" method described above. The special argument "-base", is used to
424       make the current package a subclass of the Spiffy module being used.
425
426       Any non-paired parameters act like a normal import list; just like
427       those used with the Exporter module.
428

USING Spiffy WITH base.pm

430       The proper way to use a Spiffy module as a base class is with the
431       "-base" parameter to the "use" statement. This differs from typical
432       modules where you would want to "use base".
433
434           package Something;
435           use Spiffy::Module -base;
436           use base 'NonSpiffy::Module';
437
438       Now it may be hard to keep track of what's Spiffy and what is not.
439       Therefore Spiffy has actually been made to work with base.pm. You can
440       say:
441
442           package Something;
443           use base 'Spiffy::Module';
444           use base 'NonSpiffy::Module';
445
446       "use base" is also very useful when your class is not an actual module
447       (a separate file) but just a package in some file that has already been
448       loaded.  "base" will work whether the class is a module or not, while
449       the "-base" syntax cannot work that way, since "use" always tries to
450       load a module.
451
452       base.pm Caveats
453
454       To make Spiffy work with base.pm, a dirty trick was played. Spiffy
455       swaps "base::import" with its own version. If the base modules are not
456       Spiffy, Spiffy calls the original base::import. If the base modules are
457       Spiffy, then Spiffy does its own thing.
458
459       There are two caveats.
460
461       * Spiffy must be loaded first.
462           If Spiffy is not loaded and "use base" is invoked on a Spiffy mod‐
463           ule, Spiffy will die with a useful message telling the author to
464           read this documentation. That's because Spiffy needed to do the
465           import swap beforehand.
466
467           If you get this error, simply put a statement like this up front in
468           your code:
469
470               use Spiffy ();
471
472       * No Mixing
473           "base.pm" can take multiple arguments. And this works with Spiffy
474           as long as all the base classes are Spiffy, or they are all
475           non-Spiffy. If they are mixed, Spiffy will die. In this case just
476           use separate "use base" statements.
477

Spiffy TODO LIST

479       Spiffy is a wonderful way to do OO programming in Perl, but it is still
480       a work in progress. New things will be added, and things that don't
481       work well, might be removed.
482

AUTHOR

484       Ingy döt Net <ingy@cpan.org>
485
487       Copyright (c) 2006. Ingy döt Net. All rights reserved.  Copyright (c)
488       2004. Brian Ingerson. All rights reserved.
489
490       This program is free software; you can redistribute it and/or modify it
491       under the same terms as Perl itself.
492
493       See <http://www.perl.com/perl/misc/Artistic.html>
494
495
496
497perl v5.8.8                       2006-01-29                         Spiffy(3)
Impressum