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
42       oriented 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';"
56       syntax and everything will work exactly the same. The only caveat is
57       that Spiffy.pm must already be loaded. That's because Spiffy rewires
58       base.pm 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
67       subclass of Spiffy.  The first version will work in all cases, as long
68       as 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
92       difference between the two functions is that "const" attributes can not
93       be 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
116       "parse_arguments", that it also uses for parsing its own arguments. You
117       declare which arguments are boolean (singletons) and which ones are
118       paired, with two special methods called "boolean_arguments" and
119       "paired_arguments". Parse arguments pulls out the booleans and pairs
120       and returns them in an anonymous hash, followed by a list of the
121       unmatched arguments.
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
141       comprise 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
157       hundreds of classes, and you want them all to have access to some
158       functions 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
162       delegates 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
168       Multiple Inheritance (MI), and if you've done much MI you've probably
169       run into weird problems and headaches. Some languages like Ruby,
170       attempt to resolve MI issues using a technique called mixins.
171       Basically, all Ruby classes use only Single Inheritance (SI), and then
172       mixin functionality 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
176       standpoint that's not the best way to do it. Spiffy does what Ruby
177       does. It creates an empty anonymous class, imports everything into that
178       class, and then chains the new class into your SI ISA path. In other
179       words, if 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
199       inheritance. 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
225       performance penalty for creating clean code on par with Ruby and
226       Python.
227
228           package Example;
229           use Spiffy -Base;
230
231           sub crazy {
232               $self->nuts;
233           }
234           sub wacky { }
235           sub new() {
236               bless [], shift;
237           }
238
239       is exactly the same as:
240
241           package Example;
242           use Spiffy -base;
243           use strict;use warnings;
244           sub crazy {my $self = shift;
245               $self->nuts;
246           }
247           sub wacky {my $self = shift; }
248           sub new {
249               bless [], shift;
250           }
251           ;1;
252
253       Note that the empty parens after the subroutine "new" keep it from
254       having a $self added. Also note that the extra code is added to
255       existing lines to ensure that line numbers are not altered.
256
257       "-Base" also turns on the strict and warnings pragmas, and adds that
258       annoying '1;' line to your module.
259

PRIVATE METHODS

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

Spiffy DEBUGGING

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

Spiffy FUNCTIONS

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

Spiffy METHODS

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

Spiffy ARGUMENTS

430       When you "use" the Spiffy module or a subclass of it, you can pass it a
431       list of arguments. These arguments are parsed using the
432       "parse_arguments" method described above. The special argument "-base",
433       is used to make the current package a subclass of the Spiffy module
434       being used.
435
436       Any non-paired parameters act like a normal import list; just like
437       those used with the Exporter module.
438

USING Spiffy WITH base.pm

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

Spiffy TODO LIST

490       Spiffy is a wonderful way to do OO programming in Perl, but it is still
491       a work in progress. New things will be added, and things that don't
492       work well, might be removed.
493

AUTHOR

495       Ingy dA~Xt Net <ingy@cpan.org>
496
498       Copyright (c) 2006. Ingy dA~Xt Net. All rights reserved.  Copyright (c)
499       2004. Brian Ingerson. All rights reserved.
500
501       This program is free software; you can redistribute it and/or modify it
502       under the same terms as Perl itself.
503
504       See <http://www.perl.com/perl/misc/Artistic.html>
505
506
507
508perl v5.12.0                      2006-01-29                         Spiffy(3)
Impressum