home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / Spiffy.pm < prev    next >
Encoding:
Perl POD Document  |  2004-06-10  |  25.5 KB  |  823 lines

  1. package Spiffy;
  2. use strict;
  3. use 5.006_001;
  4. use warnings;
  5. use Carp;
  6. require Exporter;
  7. our $VERSION = '0.18';
  8. our @EXPORT = ();
  9. our @EXPORT_OK = qw(id WWW XXX YYY ZZZ);
  10. our @EXPORT_BASE = qw(
  11.     field const stub super WWW XXX YYY ZZZ
  12. );
  13. our %EXPORT_TAGS = (XXX => [qw(WWW XXX YYY ZZZ)]);
  14.  
  15. my $class_map = {};
  16. my $options_map = {};
  17. my $stack_frame = 0; 
  18. my $dump = 'yaml';
  19.  
  20. sub WWW; sub XXX; sub YYY; sub ZZZ;
  21.  
  22. sub UNIVERSAL::is_spiffy {
  23.     my $self = shift;
  24.     $self->isa('Spiffy');
  25. }
  26.  
  27. sub new {
  28.     my $self = bless {}, shift;
  29.     while (@_) {
  30.         my $method = shift;
  31.         $self->$method(shift);
  32.     }
  33.     return $self;    
  34. }
  35.  
  36. my $filtered_files = {};
  37. sub import {
  38.     no strict 'refs'; 
  39.     no warnings;
  40.     local(*spiffy_constructor);
  41.     my $self_package = shift;
  42.  
  43.     my ($args, @export_list) = do {
  44.         local *boolean_arguments = sub { 
  45.             qw(-base -Base -selfless -dumper -yaml) 
  46.         };
  47.         local *paired_arguments = sub { qw(-package) };
  48.         $self_package->parse_arguments(@_);
  49.     };
  50.  
  51.     $dump = 'yaml' if $args->{-yaml};
  52.     $dump = 'dumper' if $args->{-dumper};
  53.  
  54.     spiffy_filter() if $args->{-selfless} and 
  55.       not $filtered_files->{(caller($stack_frame))[1]}++;
  56.  
  57.     my $caller_package = $args->{-package} || caller($stack_frame);
  58.     if ($args->{-Base} or $args->{-base}) {
  59.         push @{"${caller_package}::ISA"}, $self_package;
  60.         spiffy_filter() if $args->{-Base} and 
  61.           not $filtered_files->{(caller($stack_frame))[1]}++;
  62.         *{"${caller_package}::spiffy_constructor"} =
  63.           $self_package->spiffy_constructor_maker($caller_package)
  64.             unless defined &{"${caller_package}::spiffy_constructor"};
  65.     }
  66.  
  67.     for my $class (all_my_bases($self_package)) {
  68.         next unless $class->isa('Spiffy');
  69.         for my $sub (@{"$class\::EXPORT"}) {
  70.             $class_map->{$caller_package}{$sub} = $self_package;
  71.             $options_map->{$caller_package}{$sub} = [@_];
  72.         }
  73.         my @export = grep {
  74.             not defined &{"$caller_package\::$_"};
  75.         } ( @{"$class\::EXPORT"}, 
  76.             ($args->{-Base} or $args->{-base})
  77.               ? @{"$class\::EXPORT_BASE"} : (),
  78.           );
  79.         my @export_ok = grep {
  80.             not defined &{"$caller_package\::$_"};
  81.         } @{"$class\::EXPORT_OK"};
  82.         my %exportable = map { ($_, 1) } @export, @export_ok;
  83.         next unless keys %exportable;
  84.         my @export_save = @{"$class\::EXPORT"};
  85.         my @export_ok_save = @{"$class\::EXPORT_OK"};
  86.         @{"$class\::EXPORT"} = @export;
  87.         @{"$class\::EXPORT_OK"} = @export_ok;
  88.         my @list = grep {
  89.             (my $v = $_) =~ s/^[\!\:]//;
  90.             $exportable{$v} or ${"$class\::EXPORT_TAGS"}{$v};
  91.         } @export_list;
  92.         Exporter::export($class, $caller_package, @list);
  93.         @{"$class\::EXPORT"} = @export_save;
  94.         @{"$class\::EXPORT_OK"} = @export_ok_save;
  95.     }
  96. }
  97.  
  98. sub spiffy_filter {
  99.     require Filter::Util::Call;
  100.     my $done = 0;
  101.     Filter::Util::Call::filter_add(
  102.         sub {
  103.             return 0 if $done;
  104.             my $data;
  105.             while (my $status = Filter::Util::Call::filter_read()) {
  106.                 return $status if $status < 0;
  107.                 $data .= $_;
  108.                 last if /^__(?:END|DATA)__\r?$/;
  109.                 $_ = '';
  110.             }
  111.             $_ = $data;
  112.             s/^(sub\s+\w+\s+\{)(.*\n)/${1}my \$self = shift;$2/mg;
  113.             s/^(sub\s+\w+)\s*\(\s*\)(\s+\{.*\n)/${1}${2}/mg;
  114.             $done = 1;
  115.         }
  116.     );
  117. }
  118.  
  119. sub base {
  120.     push @_, '-base';
  121.     goto &import;
  122. }
  123.  
  124. sub all_my_bases {
  125.     my $class = shift;
  126.     my @bases = ($class);
  127.     no strict 'refs';
  128.     for my $base_class (@{"${class}::ISA"}) {
  129.         push @bases, all_my_bases($base_class);
  130.     }
  131.     my $used = {};
  132.     my @x = grep {not $used->{$_}++} @bases;
  133. }
  134.  
  135. sub field {
  136.     my $package = caller;
  137.     my ($args, @values) = do {
  138.         no warnings;
  139.         local *paired_arguments = sub { (qw(-package)) };
  140.         Spiffy->parse_arguments(@_);
  141.     };
  142.     my ($field, $default) = @values;
  143.     $package = $args->{-package} if defined $args->{-package};
  144.     no strict 'refs';
  145.     return if defined &{"${package}::$field"};
  146.     *{"${package}::$field"} = 
  147.       (ref($default) eq 'ARRAY' and not @$default)
  148.       ? sub {
  149.             my $self = shift;
  150.             $self->{$field} = []
  151.               unless exists $self->{$field};
  152.             return $self->{$field} unless @_;
  153.             $self->{$field} = shift;
  154.         }
  155.       : (ref($default) eq 'HASH' and not @{[%$default]})
  156.         ? sub {
  157.               my $self = shift;
  158.               $self->{$field} = {}
  159.                 unless exists $self->{$field};
  160.               return $self->{$field} unless @_;
  161.               $self->{$field} = shift;
  162.           }
  163.         : sub {
  164.               my $self = shift;
  165.               $self->{$field} = $default
  166.                 unless exists $self->{$field};
  167.               return $self->{$field} unless @_;
  168.               $self->{$field} = shift;
  169.           }
  170. }
  171.  
  172. sub const {
  173.     my $package = caller;
  174.     my ($args, @values) = do {
  175.         no warnings;
  176.         local *paired_arguments = sub { (qw(-package)) };
  177.         Spiffy->parse_arguments(@_);
  178.     };
  179.     my ($field, $default) = @values;
  180.     $package = $args->{-package} if defined $args->{-package};
  181.     no strict 'refs';
  182.     return if defined &{"${package}::$field"};
  183.     *{"${package}::$field"} = sub { $default }
  184. }
  185.  
  186. sub stub {
  187.     my $package = caller;
  188.     my ($args, @values) = do {
  189.         no warnings;
  190.         local *paired_arguments = sub { (qw(-package)) };
  191.         Spiffy->parse_arguments(@_);
  192.     };
  193.     my ($field, $default) = @values;
  194.     $package = $args->{-package} if defined $args->{-package};
  195.     no strict 'refs';
  196.     return if defined &{"${package}::$field"};
  197.     *{"${package}::$field"} = 
  198.     sub { 
  199.         require Carp;
  200.         Carp::confess 
  201.           "Method $field in package $package must be subclassed";
  202.     }
  203. }
  204.  
  205. sub spiffy_constructor_maker {
  206.     my $spiffy_package = shift;
  207.     my $caller_package = shift;
  208.     no strict 'refs';
  209.     sub {
  210.         my $name = shift;
  211.         return if defined &{"${caller_package}::$name"};
  212.         *{"${caller_package}::$name"} =
  213.             sub {
  214.                 my $package = caller;
  215.                 my $class = $class_map->{$package}{$name}
  216.                   or die "No class for ${package}::$name";
  217.                 my $defaults = $options_map->{$package}{$name};
  218.                 $class->new(@$defaults, @_);
  219.             };
  220.     }
  221. }
  222.  
  223. sub parse_arguments {
  224.     my $class = shift;
  225.     my ($args, @values) = ({}, ());
  226.     my %booleans = map { ($_, 1) } $class->boolean_arguments;
  227.     my %pairs = map { ($_, 1) } $class->paired_arguments;
  228.     while (@_) {
  229.         my $elem = shift;
  230.         if (defined $elem and defined $booleans{$elem}) {
  231.             $args->{$elem} = (@_ and $_[0] =~ /^[01]$/)
  232.             ? shift
  233.             : 1;
  234.         }
  235.         elsif (defined $elem and defined $pairs{$elem} and @_) {
  236.             $args->{$elem} = shift;
  237.         }
  238.         else {
  239.             push @values, $elem;
  240.         }
  241.     }
  242.     return wantarray ? ($args, @values) : $args;        
  243. }
  244.  
  245. sub boolean_arguments { () }
  246. sub paired_arguments { () }
  247.  
  248. # get a unique id for any node
  249. sub id {
  250.     if (not ref $_[0]) {
  251.         return 'undef' if not defined $_[0];
  252.         \$_[0] =~ /\((\w+)\)$/o or die;
  253.         return "$1-S";
  254.     }
  255.     require overload;
  256.     overload::StrVal($_[0]) =~ /\((\w+)\)$/o or die;
  257.     return $1;
  258. }
  259.  
  260. #===============================================================================
  261. # It's super, man.
  262. #===============================================================================
  263. package DB;
  264. sub super_args { my @dummy = caller(2); @DB::args }
  265. package Spiffy;
  266.  
  267. sub super {
  268.     @_ = DB::super_args unless @_;
  269.     my $class = ref $_[0] ? ref $_[0] : $_[0];
  270.     (my $method = (caller(1))[3]) =~ s/.*:://;
  271.     my $caller_class = caller;
  272.     my $seen = 0;
  273.     my @super_classes = reverse grep {
  274.         ($seen or $seen = ($_ eq $caller_class)) ? 0 : 1;
  275.     } reverse all_my_bases($class);
  276.     for my $super_class (@super_classes) {
  277.         no strict 'refs';
  278.         next if $super_class eq $class;
  279.         goto &{"${super_class}::$method"}
  280.           if defined &{"${super_class}::$method"};
  281.     }
  282.     return;
  283. }
  284.  
  285. #===============================================================================
  286. # This code deserves a spanking, because it is being very naughty.
  287. # It is exchanging base.pm's import() for its own, so that people
  288. # can use base.pm with Spiffy modules, without being the wiser.
  289. #===============================================================================
  290. my $real_base_import;
  291.  
  292. BEGIN {
  293.     require base unless defined $INC{'base.pm'};
  294.     $real_base_import = \&base::import;
  295.     no warnings;
  296.     *base::import = \&spiffy_base_import;
  297. }
  298.  
  299. my $i = 0;
  300. while (my $caller = caller($i++)) {
  301.     next unless $caller eq 'base';
  302.     croak 
  303.     "Spiffy.pm must be loaded before calling 'use base' with a Spiffy module\n",
  304.     "See the documentation of Spiffy.pm for details\n  ";
  305. }
  306.  
  307. sub spiffy_base_import {
  308.     my @base_classes = @_;
  309.     shift @base_classes;
  310.     no strict 'refs';
  311.     goto &$real_base_import
  312.       unless grep {
  313.           eval "require $_" unless %{"$_\::"};
  314.           $_->isa('Spiffy');
  315.       } @base_classes;
  316.     my $inheritor = caller(0);
  317.     for my $base_class (@base_classes) {
  318.         next if $inheritor->isa($base_class);
  319.         croak "Can't mix Spiffy and non-Spiffy classes in 'use base'.\n", 
  320.               "See the documentation of Spiffy.pm for details\n  "
  321.           unless $base_class->isa('Spiffy');
  322.         $stack_frame = 1; # tell import to use different caller
  323.         import($base_class, '-base');
  324.         $stack_frame = 0;
  325.     }
  326. }
  327. # END of naughty code.
  328.  
  329. #===============================================================================
  330. # Debugging support
  331. #===============================================================================
  332. sub spiffy_dump {
  333.     no warnings;
  334.     if ($dump eq 'dumper') {
  335.         require Data::Dumper;
  336.         $Data::Dumper::Sortkeys = 1;
  337.         $Data::Dumper::Indent = 1;
  338.         return Data::Dumper::Dumper(@_);
  339.     }
  340.     require YAML;
  341.     $YAML::UseVersion = 0;
  342.     return YAML::Dump(@_) . "...\n";
  343. }
  344.  
  345. sub at_line_number {
  346.     my ($file_path, $line_number) = (caller(1))[1,2];
  347.     "  at $file_path line $line_number\n";
  348. }
  349.  
  350. sub WWW {
  351.     warn spiffy_dump(@_) . at_line_number;
  352.     return wantarray ? @_ : $_[0];
  353. }
  354.  
  355. sub XXX {
  356.     die spiffy_dump(@_) . at_line_number;
  357. }
  358.  
  359. sub YYY {
  360.     print spiffy_dump(@_) . at_line_number;
  361.     return wantarray ? @_ : $_[0];
  362. }
  363.  
  364. sub ZZZ {
  365.     require Carp;
  366.     Carp::confess spiffy_dump(@_);
  367. }
  368.  
  369. 1;
  370.  
  371. __END__
  372.  
  373. =head1 NAME
  374.  
  375. Spiffy - Spiffy Perl Interface Framework For You
  376.  
  377. =head1 SYNOPSIS
  378.  
  379.     package Keen;
  380.     use Spiffy '-Base';
  381.     field 'mirth';
  382.     const mood => ':-)';
  383.     
  384.     sub happy {
  385.         if ($self->mood eq ':-(') {
  386.             $self->mirth(-1);
  387.             print "Cheer up!";
  388.         }
  389.         super;
  390.     }
  391.  
  392. =head1 DESCRIPTION
  393.  
  394. "Spiffy" is a framework and methodology for doing object oriented
  395. programming in Perl. Spiffy combines the best parts of Exporter.pm,
  396. base.pm, mixin.pm and SUPER.pm into one magic foundation class. It
  397. attempts to fix all the nits and warts of traditional Perl OO, in
  398. a clean, straightforward and (perhaps someday) standard way.
  399.  
  400. Spiffy borrows ideas from other OO languages like Python, Ruby,
  401. Java and Perl 6. It also adds a few tricks of its own. 
  402.  
  403. The most striking difference between Spiffy and other Perl object
  404. oriented base classes, is that it has the ability to export functions.
  405. If you create a subclass of Spiffy, all the functions that Spiffy
  406. exports will automatically be exported by your subclass, in addition to
  407. any more functions that you want to export. And if someone creates a
  408. subclass of your subclass, all of those functions will be exported
  409. automatically, and so on.
  410.  
  411. To use Spiffy or any subclass of Spiffy as a base class of your class,
  412. you specify the C<-base> argument to the C<use> command. 
  413.  
  414.     use MySpiffyBaseModule '-base';
  415.  
  416. You can also use the traditional C<use base 'MySpiffyBaseModule';>
  417. syntax and everything will work exactly the same. The only caveat is
  418. that Spiffy.pm must already be loaded. That's because Spiffy rewires
  419. base.pm on the fly to do all the Spiffy magics.
  420.  
  421. In object oriented Perl almost every subroutine is a method. Each method
  422. gets the object passed to it as its first argument. That means
  423. practically every subroutine starts with the line:
  424.  
  425.      my $self = shift;
  426.  
  427. Spiffy provides a simple, optional filter mechanism to insert that line
  428. for you, resulting in cleaner code. If you figure an average method has
  429. 10 lines of code, that's 10% of your code! To turn this option on, you
  430. just use the C<-Base> option instead of the C<-base> option. If source
  431. filtering makes you queazy, don't use the feature. I personally find it
  432. addictive in my quest for writing squeaky clean, maintainable code.
  433.  
  434. A useful feature of Spiffy is that it exports two functions: C<field>
  435. and C<const> that can be used to declare the attributes of your class,
  436. and automatically generate accessor methods for them. The only
  437. difference between the two functions is that C<const> attributes can not
  438. be modified; thus the accessor is much faster.
  439.  
  440. One interesting aspect of OO programming is when a method calls the same
  441. method from a parent class. This is generally known as calling a super
  442. method. Perl's facility for doing this is butt ugly:
  443.  
  444.     sub cleanup {
  445.         my $self = shift;
  446.         $self->scrub;
  447.         $self->SUPER::cleanup(@_);
  448.     }
  449.  
  450. Spiffy makes it, er, super easy to call super methods. You just use
  451. the C<super> function. You don't need to pass it any arguments
  452. because it automatically passes them on for you. Here's the same
  453. function with Spiffy:
  454.  
  455.     sub cleanup {
  456.         $self->scrub;
  457.         super;
  458.     }
  459.  
  460. Spiffy has an interesting function that it exports called
  461. C<spiffy_constructor>. This function will generate a shortcut function
  462. that will call the class's "new" constructor. It is smart enough to know
  463. which class to use. All the arguments you pass to the generated
  464. function, get passed on to each invocation of the constructor. In
  465. addition, all the arguments you passed in the use statement for that
  466. class also get passed to the constructor. The C<io()> function of
  467. IO::All is a good example of using C<spiffy_constructor>.
  468.  
  469. Spiffy has a special method for parsing arguments called
  470. C<parse_arguments>, that it also uses for parsing its own arguments. You
  471. declare which arguments are boolean (singletons) and which ones are
  472. paired, with two special methods called C<boolean_arguments> and
  473. C<paired_arguments>. Parse arguments pulls out the booleans and pairs
  474. and returns them in an anonymous hash, followed by a list of the
  475. unmatched arguments.
  476.  
  477. Finally, Spiffy exports a few debugging functions C<WWW>, C<XXX>, C<YYY>
  478. and C<ZZZ>. Each of them produces a YAML dump of its arguments. WWW
  479. warns the output, XXX dies with the output, YYY prints the output, and
  480. ZZZ confesses the output. If YAML doesn't suit your needs, you can switch all
  481. the dumps to Data::Dumper format with the C<-dumper> option.
  482.  
  483. That's Spiffy!
  484.  
  485. =head1 Spiffy EXPORTING
  486.  
  487. Spiffy implements a completely new idea in Perl. Modules that act both
  488. as object oriented classes and that also export functions. But it
  489. takes the concept of Exporter.pm one step further; it walks the entire
  490. C<@ISA> path of a class and honors the export specifications of each
  491. module. Since Spiffy calls on the Exporter module to do this, you can
  492. use all the fancy interface features that Exporter has, including tags
  493. and negation.
  494.  
  495. Spiffy considers all the arguments that don't begin with a dash to
  496. comprise the export specification.
  497.  
  498.     package Vehicle;
  499.     use Spiffy '-base';
  500.     our $SERIAL_NUMBER = 0;
  501.     our @EXPORT = qw($SERIAL_NUMBER);
  502.     our @EXPORT_BASE = qw(tire horn);
  503.  
  504.     package Bicycle;
  505.     use Vehicle '-base', '!field';
  506.     $self->inflate(tire);
  507.  
  508. In this case, C<Bicycle->isa('Vehicle')> and also all the things
  509. that C<Vehicle> and C<Spiffy> export, will go into C<Bicycle>,
  510. except C<field>.
  511.  
  512. Exporting can be very helpful when you've designed a system with
  513. hundreds of classes, and you want them all to have access to some
  514. functions or constants or variables. Just export them in your main base
  515. class and every subclass will get the functions they need.
  516.  
  517. You can do almost everything that Exporter does because Spiffy delegates
  518. the job to Exporter (after adding some Spiffy magic). Spiffy offers a
  519. C<@EXPORT_BASE> variable which is like C<@EXPORT>, but only for usages
  520. that use C<-base>.
  521.  
  522. =head1 Spiffy FILTERING
  523.  
  524. By using the C<-Base> flag instead of C<-base> you never need to write the
  525. line:
  526.  
  527.     my $self = shift;
  528.  
  529. This statement is added to every subroutine in your class by using a source
  530. filter. The magic is simple and fast, so there is litte performance penalty
  531. for creating clean code on par with Ruby and Python.
  532.  
  533.     package Example;
  534.     use Spiffy '-Base';
  535.  
  536.     sub crazy {
  537.         $self->nuts;
  538.     }
  539.     sub wacky { }
  540.     sub new() {
  541.         bless [], shift;
  542.     }
  543.  
  544. is exactly the same as:
  545.  
  546.     package Example;
  547.     use Spiffy '-base';
  548.  
  549.     sub crazy {my $self = shift;
  550.         $self->nuts;
  551.     }
  552.     sub wacky {my $self = shift; }
  553.     sub new {
  554.         bless [], shift;
  555.     }
  556.  
  557. Note that the empty parens after the subroutine C<new> keep it from
  558. having a $self added. Also not that the extra code is added to existing lines
  559. to ensure that line numbers are not altered.
  560.  
  561. =head1 Spiffy DEBUGGING
  562.  
  563. The XXX function is very handy for debugging because you can insert it
  564. almost anywhere, and it will dump your data in nice clean YAML. Take the
  565. following statement:
  566.  
  567.     my @stuff = grep { /keen/ } $self->find($a, $b);
  568.  
  569. If you have a problem with this statement, you can debug it in any of the
  570. following ways:
  571.  
  572.     XXX my @stuff = grep { /keen/ } $self->find($a, $b);
  573.     my @stuff = XXX grep { /keen/ } $self->find($a, $b);
  574.     my @stuff = grep { /keen/ } XXX $self->find($a, $b);
  575.     my @stuff = grep { /keen/ } $self->find(XXX $a, $b);
  576.  
  577. XXX is easy to insert and remove. It is also a tradition to mark
  578. uncertain areas of code with XXX. This will make the debugging dumpers
  579. easy to spot if you forget to take them out.
  580.  
  581. WWW and YYY are nice because they dump their arguments and then return the
  582. arguments. This way you can insert them into many places and still have the
  583. code run as before. Use ZZZ when you need to die with both a YAML dump and a
  584. full stack trace.
  585.  
  586. The debugging functions are exported by default if you use the C<-base>
  587. option. To export all 4 functions use the export tag:
  588.  
  589.     use SomeSpiffyModule ':XXX';
  590.  
  591. To force the debugging functions to use Data::Dumper instead of YAML:
  592.  
  593.     use SomeSpiffyModule '-dumper';
  594.  
  595. =head1 Spiffy FUNCTIONS
  596.  
  597. This section describes the functions the Spiffy exports. The C<field>,
  598. C<const>, C<super> and C<spiffy_constructor> functions are only exported when
  599. you use the C<-base> or C<-Base> options.
  600.  
  601. =over 4
  602.  
  603. =item * field
  604.  
  605. Defines accessor methods for a field of your class:
  606.  
  607.     package Example;
  608.     use Spiffy '-Base';
  609.     
  610.     field 'foo';
  611.     field bar => [];
  612.  
  613.     sub lalala {
  614.         $self->foo(42);
  615.         push @{$self->{bar}}, $self->foo;
  616.     }
  617.  
  618. The first parameter passed to C<field> is the name of the attribute
  619. being defined. Accessors can be given an optional default value.
  620. This value will be returned if no value for the field has been set
  621. in the object.
  622.  
  623. =item * const
  624.  
  625.     const bar => 42;
  626.  
  627. The C<const> function is similar to <field> except that it is immutable.
  628. It also does not store data in the object. You probably always want to
  629. give a C<const> a default value, otherwise the generated method will be
  630. somewaht useless.
  631.  
  632. =item * stub
  633.  
  634.     stub 'cigar';
  635.  
  636. The C<stub> function generates a method that will die with an appropriate
  637. message. The idea is that subclasses must implement these methods so that the
  638. stub methods don't get called.
  639.  
  640. =item * super
  641.  
  642. This function is called without any arguments. It will call the same
  643. method that it is in, higher up in the ISA tree, passing it all the same
  644. arguments.
  645.  
  646.     sub foo {
  647.         super;             # Same as $self->SUPER::foo(@_);
  648.         $self->bar(42);
  649.     }
  650.  
  651.     sub new() {
  652.         my $self = super;
  653.         $self->init;
  654.         return $self;
  655.     }
  656.  
  657. C<super> will simply do nothing if there is no super method.
  658.  
  659. =item * spiffy_constructor
  660.  
  661. This function generates a function that calls the C<new()> method for your
  662. class. It passes all its arguments on to C<new>, as well as any arguments
  663. passed to the C<use> statement of your class.
  664.  
  665.     package Example;
  666.     use Spiffy '-base';
  667.     our @EXPORT = qw(foo);
  668.     
  669.     spiffy_constructor 'foo';
  670.  
  671. The C<spiffy_constructor> function is only exported if you use the 
  672. '-base' option.
  673.  
  674. =back
  675.  
  676. =head1 Spiffy METHODS
  677.  
  678. This section list all of the methods that any subclass of Spiffy
  679. automatically inherits.
  680.  
  681. =over 4
  682.  
  683. =item * is_spiffy
  684.  
  685. Returns true if an object is Spiffy. This method is UNIVERSAL so it can be
  686. called on all objects.
  687.  
  688. =item * parse_arguments
  689.  
  690. This method takes a list of arguments and groups them into pairs. It
  691. allows for boolean arguments which may or may not have a value
  692. (defaulting to 1). The method returns a hash reference of all the pairs
  693. as keys and values in the hash. Any arguments that cannot be paired, are
  694. returned as a list. Here is an example:
  695.  
  696.     sub boolean_arguments { qw(-has_spots -is_yummy) }
  697.     sub paired_arguments { qw(-name -size) }
  698.     my ($pairs, @others) = $self->parse_arguments(
  699.         'red', 'white',
  700.         -name => 'Ingy',
  701.         -has_spots =>
  702.         -size => 'large',
  703.         'black',
  704.         -is_yummy => 0,
  705.     );
  706.  
  707. After this call, C<$pairs> will contain:
  708.  
  709.     {
  710.         -name => 'Ingy',
  711.         -has_spots => 1,
  712.         -size => 'large',
  713.         -is_yummy => 0,
  714.     }
  715.  
  716. and C<@others> will contain 'red', 'white', and 'black'.
  717.  
  718. =item * boolean_arguments
  719.  
  720. Returns the list of arguments that are recognized as being boolean. Override
  721. this method to define your own list.
  722.  
  723. =item * paired_arguments
  724.  
  725. Returns the list of arguments that are recognized as being paired. Override
  726. this method to define your own list.
  727.  
  728. =back
  729.  
  730. =head1 Spiffy ARGUMENTS
  731.  
  732. When you C<use> the Spiffy module or a subclass of it, you can pass it a
  733. list of arguments. These arguments are parsed using the C<parse_arguments>
  734. method described above. Any arguments that are pairs are passed on to
  735. calls to a spiffy_constructor generated function. The special argument
  736. C<-base>, is used to make the current package a subclass of the Spiffy
  737. module being used.
  738.  
  739. Any non-paired parameters act like a normal import list; just like those
  740. used with the Exporter module.
  741.  
  742. =head1 USING Spiffy WITH base.pm
  743.  
  744. The proper way to use a Spiffy module as a base class is with the C<-base>
  745. parameter to the C<use> statement. This differs from typical modules where you
  746. would want to C<use base>.
  747.  
  748.     package Something;
  749.     use Spiffy::Module '-base';
  750.     use base 'NonSpiffy::Module';
  751.  
  752. Now it may be hard to keep track of what's Spiffy and what is not.
  753. Therefore Spiffy has actually been made to work with base.pm. You can
  754. say:
  755.  
  756.     package Something;
  757.     use base 'Spiffy::Module';
  758.     use base 'NonSpiffy::Module';
  759.  
  760. C<use base> is also very useful when your class is not an actual module (a
  761. separate file) but just a package in some file that has already been loaded.
  762. C<base> will work whether the class is a module or not, while the C<-base>
  763. syntax cannot work that way, since C<use> always tries to load a module.
  764.  
  765. =head2 base.pm Caveats
  766.  
  767. To make Spiffy work with base.pm, a dirty trick was played. Spiffy swaps
  768. C<base::import> with its own version. If the base modules are not Spiffy,
  769. Spiffy calls the original base::import. If the base modules are Spiffy,
  770. then Spiffy does its own thing.
  771.  
  772. There are two caveats.
  773.  
  774. =over 4
  775.  
  776. =item * Spiffy must be loaded first.
  777.  
  778. If Spiffy is not loaded and C<use base> is invoked on a Spiffy module,
  779. Spiffy will die with a useful message telling the author to read this
  780. documentation. That's because Spiffy needed to do the import swap
  781. beforehand.
  782.  
  783. If you get this error, simply put a statement like this up front in
  784. your code:
  785.  
  786.     use Spiffy ();
  787.  
  788. =item * No Mixing
  789.  
  790. C<base.pm> can take multiple arguments. And this works with Spiffy as
  791. long as all the base classes are Spiffy, or they are all non-Spiffy. If
  792. they are mixed, Spiffy will die. In this case just use separate C<use
  793. base> statements.
  794.  
  795. =back
  796.  
  797. =head1 Spiffy TODO LIST
  798.  
  799. Spiffy is a wonderful way to do OO programming in Perl, but it is still
  800. a work in progress. New things will be added, and things that don't work
  801. well, might be removed.
  802.  
  803. One thing I really want to add is B<mixins>. Mixins are good medicine for
  804. multiple inheritance headaches. The syntax will use C<-mixin> instead of
  805. C<-base>. But I still need to think on exactly how this should work, before
  806. implementing it. If you are an OO guru and have good ideas about how this
  807. should work, please send me an email.
  808.  
  809. =head1 AUTHOR
  810.  
  811. Brian Ingerson <INGY@cpan.org>
  812.  
  813. =head1 COPYRIGHT
  814.  
  815. Copyright (c) 2004. Brian Ingerson. All rights reserved.
  816.  
  817. This program is free software; you can redistribute it and/or modify it
  818. under the same terms as Perl itself.
  819.  
  820. See L<http://www.perl.com/perl/misc/Artistic.html>
  821.  
  822. =cut
  823.