Debian Codegraph @

README

raw
1 NAME
2 Spiffy - Spiffy Perl Interface Framework For You
3
4 SYNOPSIS
5 package Keen;
6 use Spiffy -Base;
7 field 'mirth';
8 const mood => ':-)';
9
10 sub happy {
11 if ($self->mood eq ':-(') {
12 $self->mirth(-1);
13 print "Cheer up!";
14 }
15 super;
16 }
17
18 DESCRIPTION
19 "Spiffy" is a framework and methodology for doing object oriented (OO)
20 programming in Perl. Spiffy combines the best parts of Exporter.pm,
21 base.pm, mixin.pm and SUPER.pm into one magic foundation class. It
22 attempts to fix all the nits and warts of traditional Perl OO, in a
23 clean, straightforward and (perhaps someday) standard way.
24
25 Spiffy borrows ideas from other OO languages like Python, Ruby, Java and
26 Perl 6. It also adds a few tricks of its own.
27
28 If you take a look on CPAN, there are a ton of OO related modules. When
29 starting a new project, you need to pick the set of modules that makes
30 most sense, and then you need to use those modules in each of your
31 classes. Spiffy, on the other hand, has everything you'll probably need
32 in one module, and you only need to use it once in one of your classes.
33 If you make Spiffy.pm the base class of the basest class in your
34 project, Spiffy will automatically pass all of its magic to all of your
35 subclasses. You may eventually forget that you're even using it!
36
37 The most striking difference between Spiffy and other Perl object
38 oriented base classes, is that it has the ability to export things. If
39 you create a subclass of Spiffy, all the things that Spiffy exports will
40 automatically be exported by your subclass, in addition to any more
41 things that you want to export. And if someone creates a subclass of
42 your subclass, all of those things will be exported automatically, and
43 so on. Think of it as "Inherited Exportation", and it uses the familiar
44 Exporter.pm specification syntax.
45
46 To use Spiffy or any subclass of Spiffy as a base class of your class,
47 you specify the "-base" argument to the "use" command.
48
49 use MySpiffyBaseModule -base;
50
51 You can also use the traditional "use base 'MySpiffyBaseModule';" syntax
52 and everything will work exactly the same. The only caveat is that
53 Spiffy.pm must already be loaded. That's because Spiffy rewires base.pm
54 on the fly to do all the Spiffy magics.
55
56 Spiffy has support for Ruby-like mixins with Perl6-like roles. Just like
57 "base" you can use either of the following invocations:
58
59 use mixin 'MySpiffyBaseModule';
60 use MySpiffyBaseModule -mixin;
61
62 The second version will only work if the class being mixed in is a
63 subclass of Spiffy. The first version will work in all cases, as long as
64 Spiffy has already been loaded.
65
66 To limit the methods that get mixed in, use roles. (Hint: they work just
67 like an Exporter list):
68
69 use MySpiffyBaseModule -mixin => qw(:basics x y !foo);
70
71 In object oriented Perl almost every subroutine is a method. Each method
72 gets the object passed to it as its first argument. That means
73 practically every subroutine starts with the line:
74
75 my $self = shift;
76
77 Spiffy provides a simple, optional filter mechanism to insert that line
78 for you, resulting in cleaner code. If you figure an average method has
79 10 lines of code, that's 10% of your code! To turn this option on, you
80 just use the "- Base" option instead of the "-base" option, or add the
81 "-selfless" option. If source filtering makes you queazy, don't use the
82 feature. I personally find it addictive in my quest for writing squeaky
83 clean, maintainable code.
84
85 A useful feature of Spiffy is that it exports two functions: "field" and
86 "const" that can be used to declare the attributes of your class, and
87 automatically generate accessor methods for them. The only difference
88 between the two functions is that "const" attributes can not be
89 modified; thus the accessor is much faster.
90
91 One interesting aspect of OO programming is when a method calls the same
92 method from a parent class. This is generally known as calling a super
93 method. Perl's facility for doing this is butt ugly:
94
95 sub cleanup {
96 my $self = shift;
97 $self->scrub;
98 $self->SUPER::cleanup(@_);
99 }
100
101 Spiffy makes it, er, super easy to call super methods. You just use the
102 "super" function. You don't need to pass it any arguments because it
103 automatically passes them on for you. Here's the same function with
104 Spiffy:
105
106 sub cleanup {
107 $self->scrub;
108 super;
109 }
110
111 Spiffy has a special method for parsing arguments called
112 "parse_arguments", that it also uses for parsing its own arguments. You
113 declare which arguments are boolean (singletons) and which ones are
114 paired, with two special methods called "boolean_arguments" and
115 "paired_arguments". Parse arguments pulls out the booleans and pairs and
116 returns them in an anonymous hash, followed by a list of the unmatched
117 arguments.
118
119 Finally, Spiffy can export a few debugging functions "WWW", "XXX", "YYY"
120 and "ZZZ". Each of them produces a YAML dump of its arguments. WWW warns
121 the output, XXX dies with the output, YYY prints the output, and ZZZ
122 confesses the output. If YAML doesn't suit your needs, you can switch
123 all the dumps to Data::Dumper format with the "-dumper" option.
124
125 That's Spiffy!
126
127 EXPORTING
128 Spiffy implements a completely new idea in Perl. Modules that act both
129 as object oriented classes and that also export functions. But it takes
130 the concept of Exporter.pm one step further; it walks the entire @ISA
131 path of a class and honors the export specifications of each module.
132 Since Spiffy calls on the Exporter module to do this, you can use all
133 the fancy interface features that Exporter has, including tags and
134 negation.
135
136 Spiffy considers all the arguments that don't begin with a dash to
137 comprise the export specification.
138
139 package Vehicle;
140 use Spiffy -base;
141 our $SERIAL_NUMBER = 0;
142 our @EXPORT = qw($SERIAL_NUMBER);
143 our @EXPORT_BASE = qw(tire horn);
144
145 package Bicycle;
146 use Vehicle -base, '!field';
147 $self->inflate(tire);
148
149 In this case, "Bicycle->isa('Vehicle')" and also all the things that
150 "Vehicle" and "Spiffy" export, will go into "Bicycle", except "field".
151
152 Exporting can be very helpful when you've designed a system with
153 hundreds of classes, and you want them all to have access to some
154 functions or constants
155
156 or variables. Just export them in your main base class and every subclass
157
158 will get the functions they need.
159
160 You can do almost everything that Exporter does because Spiffy delegates
161 the job to Exporter (after adding some Spiffy magic). Spiffy offers a
162 @EXPORT_BASE variable which is like @EXPORT, but only for usages that
163 use "-base".
164
165 MIXINS & ROLES
166 If you've done much OO programming in Perl you've probably used Multiple
167 Inheritance (MI), and if you've done much MI you've probably run into
168 weird problems and headaches. Some languages like Ruby, attempt to
169 resolve MI issues using a technique called mixins. Basically, all Ruby
170 classes use only Single Inheritance (SI), and then *mixin* functionality
171 from other modules if they need to.
172
173 Mixins can be thought of at a simplistic level as *importing* the
174 methods of another class into your subclass. But from an implementation
175 standpoint that's not the best way to do it. Spiffy does what Ruby does.
176 It creates an empty anonymous class, imports everything into that class,
177 and then chains the new class into your SI ISA path. In other words, if
178 you say:
179
180 package AAA;
181 use BBB -base;
182 use CCC -mixin;
183 use DDD -mixin;
184
185 You end up with a single inheritance chain of classes like this:
186
187 AAA << AAA-DDD << AAA-CCC << BBB;
188
189 "AAA-DDD" and "AAA-CCC" are the actual package names of the generated
190 classes. The nice thing about this style is that mixing in CCC doesn't
191 clobber any methods in AAA, and DDD doesn't conflict with AAA or CCC
192 either. If you mixed in a method in CCC that was also in AAA, you can
193 still get to it by using "super".
194
195 When Spiffy mixes in CCC, it pulls in all the methods in CCC that do not
196 begin with an underscore. Actually it goes farther than that. If CCC is
197 a subclass it will pull in every method that CCC "can" do through
198 inheritance. This is very powerful, maybe too powerful.
199
200 To limit what you mixin, Spiffy borrows the concept of Roles from Perl6.
201 The term role is used more loosely in Spiffy though. It's much like an
202 import list that the Exporter module uses, and you can use groups (tags)
203 and negation. If the first element of your list uses negation, Spiffy
204 will start with all the methods that your mixin class can do.
205
206 use EEE -mixin => qw(:tools walk !run !:sharp_tools);
207
208 In this example, "walk" and "run" are methods that EEE can do, and
209 "tools" and "sharp_tools" are roles of class EEE. How does class EEE
210 define these roles? It very simply defines methods called "_role_tools"
211 and "_role_sharp_tools" which return lists of more methods. (And
212 possibly other roles!) The neat thing here is that since roles are just
213 methods, they too can be inherited. Take that Perl6!
214
215 FILTERING
216 By using the "-Base" flag instead of "-base" you never need to write the
217 line:
218
219 my $self = shift;
220
221 This statement is added to every subroutine in your class by using a
222 source filter. The magic is simple and fast, so there is litte
223 performance penalty for creating clean code on par with Ruby and Python.
224
225 package Example;
226 use Spiffy -Base;
227
228 sub crazy {
229 $self->nuts;
230 }
231 sub wacky { }
232 sub new() {
233 bless [], shift;
234 }
235
236 is exactly the same as:
237
238 package Example;
239 use Spiffy -base;
240 use strict;use warnings;
241 sub crazy {my $self = shift;
242 $self->nuts;
243 }
244 sub wacky {my $self = shift; }
245 sub new {
246 bless [], shift;
247 }
248 ;1;
249
250 Note that the empty parens after the subroutine "new" keep it from
251 having a $self added. Also note that the extra code is added to existing
252 lines to ensure that line numbers are not altered.
253
254 "-Base" also turns on the strict and warnings pragmas, and adds that
255 annoying '1;' line to your module.
256
257 PRIVATE METHODS
258 Spiffy now has support for private methods when you use the '-Base'
259 filter mechanism. You just declare the subs with the "my" keyword, and
260 call them with a '$' in front. Like this:
261
262 package Keen;
263 use SomethingSpiffy -Base;
264
265 # normal public method
266 sub swell {
267 $self->$stinky;
268 }
269
270 # private lexical method. uncallable from outside this file.
271 my sub stinky {
272 ...
273 }
274
275 SPIFFY DEBUGGING
276 The XXX function is very handy for debugging because you can insert it
277 almost anywhere, and it will dump your data in nice clean YAML. Take the
278 following statement:
279
280 my @stuff = grep { /keen/ } $self->find($a, $b);
281
282 If you have a problem with this statement, you can debug it in any of
283 the following ways:
284
285 XXX my @stuff = grep { /keen/ } $self->find($a, $b);
286 my @stuff = XXX grep { /keen/ } $self->find($a, $b);
287 my @stuff = grep { /keen/ } XXX $self->find($a, $b);
288 my @stuff = grep { /keen/ } $self->find(XXX $a, $b);
289
290 XXX is easy to insert and remove. It is also a tradition to mark
291 uncertain areas of code with XXX. This will make the debugging dumpers
292 easy to spot if you forget to take them out.
293
294 WWW and YYY are nice because they dump their arguments and then return
295 the arguments. This way you can insert them into many places and still
296 have the code run as before. Use ZZZ when you need to die with both a
297 YAML dump and a full stack trace.
298
299 The debugging functions are exported by default if you use the "-base"
300 option, but only if you have previously used the "-XXX" option. To
301 export all 4 functions use the export tag:
302
303 use SomeSpiffyModule ':XXX';
304
305 To force the debugging functions to use Data::Dumper instead of YAML:
306
307 use SomeSpiffyModule -dumper;
308
309 SPIFFY FUNCTIONS
310 This section describes the functions the Spiffy exports. The "field",
311 "const", "stub" and "super" functions are only exported when you use the
312 "-base" or "-Base" options.
313
314 field
315 Defines accessor methods for a field of your class:
316
317 package Example;
318 use Spiffy -Base;
319
320 field 'foo';
321 field bar => [];
322
323 sub lalala {
324 $self->foo(42);
325 push @{$self->{bar}}, $self->foo;
326 }
327
328 The first parameter passed to "field" is the name of the attribute
329 being defined. Accessors can be given an optional default value.
330 This value will be returned if no value for the field has been set
331 in the object.
332
333 const
334 const bar => 42;
335
336 The "const" function is similar to <field> except that it is
337 immutable. It also does not store data in the object. You probably
338 always want to give a "const" a default value, otherwise the
339 generated method will be somewhat useless.
340
341 stub
342 stub 'cigar';
343
344 The "stub" function generates a method that will die with an
345 appropriate message. The idea is that subclasses must implement
346 these methods so that the stub methods don't get called.
347
348 super
349 If this function is called without any arguments, it will call the
350 same method that it is in, higher up in the ISA tree, passing it all
351 the same arguments. If it is called with arguments, it will use
352 those arguments with $self in the front. In other words, it just
353 works like you'd expect.
354
355 sub foo {
356 super; # Same as $self->SUPER::foo(@_);
357 super('hello'); # Same as $self->SUPER::foo('hello');
358 $self->bar(42);
359 }
360
361 sub new() {
362 my $self = super;
363 $self->init;
364 return $self;
365 }
366
367 "super" will simply do nothing if there is no super method. Finally,
368 "super" does the right thing in AUTOLOAD subroutines.
369
370 METHODS
371 This section lists all of the methods that any subclass of Spiffy
372 automatically inherits.
373
374 mixin
375 A method to mixin a class at runtime. Takes the same arguments as
376 "use mixin ...". Makes the target class a mixin of the caller.
377
378 $self->mixin('SomeClass');
379 $object->mixin('SomeOtherClass' => 'some_method');
380
381 parse_arguments
382 This method takes a list of arguments and groups them into pairs. It
383 allows for boolean arguments which may or may not have a value
384 (defaulting to 1). The method returns a hash reference of all the
385 pairs as keys and values in the hash. Any arguments that cannot be
386 paired, are returned as a list. Here is an example:
387
388 sub boolean_arguments { qw(-has_spots -is_yummy) }
389 sub paired_arguments { qw(-name -size) }
390 my ($pairs, @others) = $self->parse_arguments(
391 'red', 'white',
392 -name => 'Ingy',
393 -has_spots =>
394 -size => 'large',
395 'black',
396 -is_yummy => 0,
397 );
398
399 After this call, $pairs will contain:
400
401 {
402 -name => 'Ingy',
403 -has_spots => 1,
404 -size => 'large',
405 -is_yummy => 0,
406 }
407
408 and @others will contain 'red', 'white', and 'black'.
409
410 boolean_arguments
411 Returns the list of arguments that are recognized as being boolean.
412 Override this method to define your own list.
413
414 paired_arguments
415 Returns the list of arguments that are recognized as being paired.
416 Override this method to define your own list.
417
418 ARGUMENTS
419 When you "use" the Spiffy module or a subclass of it, you can pass it a
420 list of arguments. These arguments are parsed using the
421 "parse_arguments" method described above. The special argument "-base",
422 is used to make the current package a subclass of the Spiffy module
423 being used.
424
425 Any non-paired parameters act like a normal import list; just like those
426 used with the Exporter module.
427
428 USING SPIFFY WITH BASE.PM
429 The proper way to use a Spiffy module as a base class is with the
430 "-base" parameter to the "use" statement. This differs from typical
431 modules where you would want to "use base".
432
433 package Something;
434 use Spiffy::Module -base;
435 use base 'NonSpiffy::Module';
436
437 Now it may be hard to keep track of what's Spiffy and what is not.
438 Therefore Spiffy has actually been made to work with base.pm. You can
439 say:
440
441 package Something;
442 use base 'Spiffy::Module';
443 use base 'NonSpiffy::Module';
444
445 "use base" is also very useful when your class is not an actual module
446 (a separate file) but just a package in some file that has already been
447 loaded. "base" will work whether the class is a module or not, while the
448 "-base" syntax cannot work that way, since "use" always tries to load a
449 module.
450
451 base.pm Caveats
452 To make Spiffy work with base.pm, a dirty trick was played. Spiffy swaps
453 "base::import" with its own version. If the base modules are not Spiffy,
454 Spiffy calls the original base::import. If the base modules are Spiffy,
455 then Spiffy does its own thing.
456
457 There are two caveats.
458
459 Spiffy must be loaded first.
460 If Spiffy is not loaded and "use base" is invoked on a Spiffy
461 module, Spiffy will die with a useful message telling the author to
462 read this documentation. That's because Spiffy needed to do the
463 import swap beforehand.
464
465 If you get this error, simply put a statement like this up front in
466 your code:
467
468 use Spiffy ();
469
470 No Mixing
471 "base.pm" can take multiple arguments. And this works with Spiffy as
472 long as all the base classes are Spiffy, or they are all non-Spiffy.
473 If they are mixed, Spiffy will die. In this case just use separate
474 "use base" statements.
475
476 SPIFFY TODO LIST
477 Spiffy is a wonderful way to do OO programming in Perl, but it is still
478 a work in progress. New things will be added, and things that don't work
479 well, might be removed.
480
481 AUTHOR
482 Ingy döt Net <[email protected]>
483
484 COPYRIGHT AND LICENSE
485 Copyright 2004-2014. Ingy döt Net.
486
487 This program is free software; you can redistribute it and/or modify it
488 under the same terms as Perl itself.
489
490 See <http://www.perl.com/perl/misc/Artistic.html>
491