The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Kite::Base - base class module implementing common functionality

SYNOPSIS

    package Kite::MyModule;

    use Kite::Base;
    use base qw( Kite::Base );
    use vars qw( $PARAMS $ERROR );

    $PARAMS = {
        TITLE => 'Default Title',
        ALPHA => 3.14,
        OMEGA => 2.718,
    };

    package main;
    
    # specify config as a hash reference...
    my $module = Kite::MyModule->new({
        TITLE => 'Grand Title',
        ALPHA => 3,
    }) || die $Kite::MyModule::ERROR, "\n";

    # ...or as a list of items;  parameter case is insignificant
    my $module = Kite::MyModule->new( TITLE => 'Grand Title' )
        || die $Kite::MyModule::ERROR, "\n";
    
    print $module->title();
    print $module->alpha();
    print $module->omega();

DESCRIPTION

Base class module which implements a constructor and error reporting functionality for various Kite modules.

PUBLIC METHODS

new(\%config)

Constructor method which accepts a reference to a hash array or a list of name => value parameters which are folded into a hash. The init() method is then called, passing the configuration hash and should return true/false to indicate success or failure. A new object reference is returned, or undef on error. Any error message raised can be examined via the error() class method or directly via the package variable ERROR in the derived class.

    package Kite::MyModule;

    use Kite::Base;
    use base qw( Kite::Base );

    package main;

    my $module1 = Kite::MyModule->new({ param => 'value' })
        || die Kite::MyModule->error(), "\n";

    my $module2 = Kite::MyModule->new( param => 'value' )
        || die "constructor error: $Kite::MyModule::ERROR\n";

init(\%config)

This method is called by the new() constructor to initialise the object. A reference to a hash array of configuration items is passed as a parameter.

The method looks for a hash reference defined as the $PARAMS package variable in the package of the derived class. If defined, this hash array should contain keys which define the acceptable configuration paramaters and values which provide default values for that item. The method then iterates through the items in this hash, copying any defined value in the $config hash or otherwise the default value in the $PARAMS hash, into the $self object.

All parameter names should be specified in the $PARAMS hash in UPPER CASE. The user may specify UPPER or lower case parameters names and these will both be correctly handled. Parameter names which are prefixed with an underscore will be considered 'private'. The default value, defined in the $PARAMS hash will be copied into the $self object, but any value provided in the $config hash will be ignored.

    package Kite::MyModule;

    use Kite::Base;
    use base qw( Kite::Base );    
    use vars qw( $ERROR $PARAMS );

    $PARAMS = {
        TITLE  => 'Default Title',
        AUTHOR => undef,                # no default
        _COUNT => 1,                    # private variable
    };

    package main;
    
    my $mod = Kite::MyModule->new(title => 'The Title') 
        || die $Kite::MyModule::ERROR;

Derived classes may elect to redefine the init() subroutine to provide their own custom initialisation routines. They can, of course, explicitly call the init() method on the parent class if they need to do so.

    package Kite::MyModule;

    ...

    sub init {
        my ($self, $config) = @_;
    
        $self->SUPER::init($config)
            || return undef;

        # more configuration...

        return $self;
    }

error($msg)

May be called as an object method to get/set the internal _ERROR member or as a class method to get/set the $ERROR variable in the derived class's package.

    my $module = Kite::MyModule->new({ ... })
        || die Kite::MyModule->error(), "\n";

    $module->do_something() 
        || die $module->error(), "\n";

When called with parameters (multiple params are concatenated), this method will set the relevant variable and return undef. This is most often used within object methods to report errors to the caller.

    package Kite::MyModule;

    ...

    sub foobar {
        my $self = shift;
        ...
        return $self->error('some kind of error...')
            if $some_condition;
        ...
    }

AUTHOR

Andy Wardley <abw@kfs.org>

REVISION

$Revision: 1.3 $

COPYRIGHT

Copyright (C) 2000 Andy Wardley. All Rights Reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

Kite