NAME

Class::GAPI - Generic API, Base class with autoloaded methods, stub objects, cloning etc.


SYNOPSIS

        package Guppy ;
        use Class::GAPI                 ; # All of its cool stuff 
        our @ISA = qw(Class::GAPI)      ; # is now in our namespace
        our @Children = qw(Class::GAPI::Fin Class::List::Eyeballs CGI)  ; # Autoconstruct Subordinates
        our @Default_Properties = qw(scaly small sushi)                         ; # Call at constructor time
        use strict ;
 
        sub _init { # Last stage of initialization
                  my $self = shift ; 
                  $self->fillet(1) if defined $self->{'sushi'}; # sushi exists but is undefined
                return 1;
        }
        1 ;
        package Petstore ; 
        use Guppy        ; 
        my $pet = Guppy->new(color => 'orange', price => '.50', small => 1, -sushi => 1) ; # envoke these functions
        $pet->Eyeballs->[0] = "left"    ; # Access a special list subclass
        $pet->Eyeballs->[1] = "right"   ; # 
        $pet->Fin->dorsal("polkadot")   ; # Access a subordinate Class::GAPI object
        $pet->Fin->tail("orange")       ; #


DESCRIPTION

This is a foundation class. It is intended to be inhertied and used as a framework for other objects. This module features autoloaded methods (set+get as one method), three styles of initialization, tools for handling stub objects, and cloning. It is particularly well suited to handling record list type structures, deeply nested trees and those on-the-fly data structures that give Perl a reputation as being a language of line noise. GAPI breaks a few rules and create a few others. Overall it just makes coding complex nested data structures a heck of a lot easier.


AUTOLOADED METHODS

Probably the most used part of this module is the autoloaded methods. One can access them from a few places. First by constructing the widget with a hash

        my $pet = Guppy->new(foo => "bar") ;

This is the same as saying:

        my $pet = Guppy->new() ;
        $pet->foo("bar")       ;

which is the same thing as saying:

        my $pet = Guppy->new() ;
        $pet->{'foo'} = 'bar'  ;

So all methods are autoloaded. A side effect is that typo'd function calls generally will not cause a crash, but rather quitely create an additional property. This can also be viewed as a feature, in that you can call nonexistant functions in GAPI objects, thereby allowing you to write you code a bit more top-down and it will be more tolerable of things you haven't added yet.

All autoloaded methods add properties, never deleting them. To undefine something call it as a hash. (the variable ``_init'' is reserved and does not autoload, you'll see why later)

        undef $pet->{'foo'}  ; # no foo for you 
        delete $pet->{'foo'} ; # de-exist foo.

Passing a hash or array to a function returns a reference to the respective type, as does just calling an empty function on a property that contains a hash or array. And they may be constructed on the fly. So you can:

        my $hashref = $pet->magician(tophat => 'bunny') ;

But don't do this. Forget I mentioned it. Instead use the sprout() function which is GAPI for creating GAPI based subclasses. sprout()ed classes will then also support autoloaded methods and other GAPI functions.


        $pet->sprout('magician', tophat => 'bunny')        ; # $pet->{'magician'} is now a Class::GAPI object
        my $wascallywabit = $pet->magician->tophat()       ; # get the rabbit  
        $pet->magician->tophat('dove')                     ; # replace it with a dove

Now, back to the constructor:

        my $pet = Guppy->new(foo => "bar") ;

This does not just set $pet->{'foo'} to ``bar'', it invoke the function 'foo' on ``bar'', and the autoloaded function is what does the set/get. So it is important to note that one can preempt this behavior simply by defining a function as follows:

        sub foo { 
                my $self = shift ; 
                my $bar = shift  ; 
                print "a guppy walks into a $bar and says: Ouch.\n" ;
        }


OBJECT INITIALIZATION

Class::GAPI has three stages of initialization at constructor time. The first which we just discussed is by calling passed arguments as functions. The second is by evaluating two class wide predefined arrays. They are:

        our @Default_Properties = qw(scaly small sushi) ; # execute some functions during new()
        our @Children = qw(Class::GAPI::Fin Class::List::Eyeballs) ; # make some branches on our tree

@Default_Properties is easy. Anything named here is called just as if it was passed as an option with an undefined value. So the example above is the same as:

        my $pet = Guppy->new(scaly => undef, small => undef, sushi => undef) ;

@Default_Properties is not used that often, in that the other Initialization stages can do more than @Default_Properties. It is handy from time to time when you want to add something complicated to the objects initialization and don't need to pass any special arguments. (Like I said, rarely used) It is also trumped by any same-named passed option pair from stage 1. So you you can define this as a hail marry for any function that should be run at constructor time, even if the caller doesn't send an option pair.

@Children is a list of subordinate objects to call ->new() on at constructor time. This allows Class::GAPI based objects to include other classes in a sem-codeless fashion. Just ``use'' something and stick it in Children, and you will get one built. (No options will be passed, but it will built.) So for example you can do this:

        package Guppy ;
        use CGI                    ;
        use Class::GAPI            ; 
        our @ISA = qw(Class::GAPI) ;  
        our @Children = qw(CGI)    ;
        1 ;

Which will then allow you to do this:

        my $pet = Guppy->new()      ; 
        my $SwimTowardstheLight = $pet->CGI->param("fishhook") ; # Extract CGI parameter "fishhook" 
 
Class::GAPI will always use the right-most namespace fragment as the option in the option => value pair. (This may 
cause a namespace conflict from time to time, in those cases just use the third stage _init instead.) So for example:
        package SpyGuppy           ; 
        use Crypt::CBC             ; # block handler
        use Crypt::DES             ; # Encryption Algorythm. 
        use Class::GAPI            ; 
        our @ISA = qw(Class::GAPI) ;  
        our @Children = qw(Crypt::CBC Crypt::DES) ;
        1 ;

and then do:

        my $pet = SpyGuppy->new()       ; 
        $pet->CBC->something()          ;
        $pet->DES->somethingelse()      ;

@Children also conveiniently has 2 special class names. Class::GAPI::Foo, and Class::List::Foo. In this case ``Foo'' can be anything you like, and will correspondingly be used to create a sprout()ed object. Note that Class::GAPI::Foo is a a sprouted hash, while Class::List::Foo is a sprouted array. This is very convenient for making lists of objects. The technique below can be used to quickly create a variety of styles of record manager classes.

        package Guppy::School   ; 
        use Guppy               ; 
        our @ISA = qw(Guppy)    ; # We are derived from a Guppy, which is derived from a GAPI  
        our @Children = qw(Class::List::School) ; # $self->{'School'} is now an array
        sub doSpawn { # Add a new Guppy Object
                my $self = shift                         ;       
                my $fish = Guppy->new()          ; 
                push @{$self->School()}, $fish ; 
        }
        sub fishNet { # Get a specific Guppy object 
                my $self = shift                 ; 
                my $n = shift                    ; 
                my $fish = $self->School->[$n] ;
                return($fish)                    ;   
        }
        1 ;

The third stage of initialization is by defining a local &_init subroutine. This gets called after everything else. So if one desires to do something with passed variables after the class is blessed, this is where to do it. If you call an autoloaded function here, it takes place after autoloaded functions from ->new(), and Default_Properties. So you do have access to data passed or processed during invokation.

passed at invokation:

        package Guppy           ; 
        use Class::GAPI         ; 
        our @ISA = (Class::GAPI);
        use strict              ; 
 
        sub _init {
                my $self = shift ; 
                $self->chopchopchop() if $self->sushi() && $self->filet() ; 
        }
        1 ;
        package PetShop ; 
        use Guppy       ;
        my $pet = Guppy->new(-sushi => 0, -filet => undef) ; 
        my $lunch = Guppy->new(-sushi => 1, -filet => 1)   ;

In this case the execution of method chopchopchop would occur in the case of lunch but not in the case of pet.


OTHER FUNCTIONS

Cloning is supported for Class::GAPI objects and any subordinate objects based on Class::GAPI or that Inherit Class::GAPI. This includes Class::List objects. This is function is eval()d, so it will not crash if you have other stuff in their, just don't expect that other stuff copy.

        my $twin = $pet->clone(); # Make the FDA nervous

The overlay() function allows one to execute a block of functions by passing hash. This is equivilant to what happens when constructed with new(). This is typically usefull when you want to copy a hash into several objects as you might in a record table:

        package Guppy::School   ; 
        use Guppy               ; 
        our @ISA = qw(Guppy)    ; # We are derived from a Guppy, which is derived from a GAPI  
        our @Children = qw(Class::List::School) ; # $self->{'School'} is now an array
        sub doSpawn { # Add a new Guppy Object
                my $self = shift                ;
                my $fish = Guppy->new(@_)       ; # Pass options pairs to the new fish 
                push @{$self->School()}, $fish  ; 
        }
        sub fishGrow { # Add a block of options like so: fishGrow(2, foo => 'bar') ;  
                my $self = shift                ; 
                my $n = shift                   ; 
                $self->School->[$n]->overlay(@_); 
                return($fish)                   ;   
        }
        1 ;

The warn_self() function is pretty much what it sounds like. You can call it at any level with a tree of nested GAPI and it will produce a table of the object as a warning. Obviously this handy for debugging:

        $self->warn_self() ;
        $self->Foo->Bar->warn_self() ;


NOTES

It is worth noting that GAPI uses a lot of eval() calls. So it is fairly slow. Also special care should be given to using this module in CGI because of that. You should probably read the code and understand how the constructor works before even considering using this thing in cgi code. Consider yourself warned.

This was written on an Win32 box running cygwin and Activestate, and it works on both with Perl 5.8. I expect it should work with anything later than 5.6.1, but It hasn't been tested.

Autoloaded methods tend to cause silent failure modes. Essentailly typos that would have normally crashed perl will often just end up creating a dangling property somewhere. Use $self->warn_self() to take snapshots of objects if something is not getting properly populated. If you see two similarly named properties, you've found the culprit.

No animals were harmed in the development of this module.


AUTHOR

Matthew Sibley matt@itoperators.com


COPYRIGHT AND LICENCE

Copyright (C) 2005 IT Operators http://www.itoperators.com

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.