Denys Vorobyov > Class-Frame-1.0 > Class::Frame

Download:
Class-Frame-1.0.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 1.0   Source  

NAME ^

Class::Frame - Class template with default constructor and accessors autogenerated

SYNOPSIS ^

        In class Sith:
        --------------

        package Sith;

        use strict;

        use base qw( Class::Frame );
        use Class::Frame;

        DEFINE_FIELDS(
            name => 'Palpatine',
            occupation => 'Sith Lord',
            weapon => [ 'The Force', 'Lightsaber' ]
        );

        1;


        In a galaxy far far away:
        -------------------------

        use Sith;

        # Difficult to see the Dark Side is . . .
        my $emperor = Sith->new(
                name => 'Palpatine',
                occupation => 'Senator'
        );

        # Get name attribute
        print $emperor->name();

        # Another way of getting name
        print $emperor->get_name();

        # Revealed itself the Dark Side has . . .
        $emperor->set_name('Darth Sidios'); 
        $emperor->set_points(120);
        $emperor->set_occupation('Sith Lord');

        # Get default value for occupation field in Sith class
        my $occupation = $emperor->get_occupation_default();

        . . .


        Subclassing Sith:
        -----------------

        package Sith::Lord;

        use base qw( Sith );

        sub new {
                my $pkg = shift;
                my @args = @_;
                
                my $self = $pkg->SUPER::new(@args);
                bless $self, $pkg;
        }

        . . .

ABSTRACT ^

This is 'yet another accessor creation helper' module but its different ( of course :-)). For the class using Class::Frame as a base class it automatically creates default constructor wich accepts field values as input parameters and if some or all of the parameters were not set - uses default values specified during class structure declaration ( please see below for more details ). Also it creates three methods for each field: accessor ('get'), mutator ('set') and 'shortcut' method ( $obj-><field> is the same as $obj->get_<field> ). Supports further subclassing of Class::Frame derived class.

DESCRIPTION ^

Learn Class::Frame in 5 seconds!

To start using Class::Frame as base class you will need to make 8, err, 2 easy steps :-)

1) Create your class

        package Jedi;

        use strict;
        use base qw( Class::Frame );
        use Class::Frame; # Exports DEFINE_FIELDS helper function

        # Accepts a hash of field names with default values
        # that can be scalars or references to anything
        # ( array, hash, object ). If field doesn't have default
        # value - set it to undef

        DEFINE_FIELDS(
                name => undef,
                points => 140,
                weapon => Jedi::Weapon::Lightsaber
        );

2) In your code there you are going to use My::Class:

        use Jedi;

        . . .

        # Create instance of My::Class and override default name value
        my $kit_fisto = Jedi->new(name => 'Kit Fisto');
        $kit_fisto->weapon->activate();

        . . .
Class Declaration

DEFINE_FIELDS( <field_name> => <default_value>, ... )

        This method creates accessors and constructor for the class it has been
        called from. Input - hash of fields and their default_values. If default
        value is undef please specify so EXPLICITLY
Generated constructor

new( [<field_name> => <value> [ , ... ]] )

        Creates new instance of class derived from Class::Frame. Accepts a hash
        containing field names as keys and field initializing values as values.
        If field has init value then its default value will be overriden,
        otherwise - default value will be used (if provided in class declaration).
Accessors

<field_name>

        Gets field value

        Example:

        my $jedi_name = $jedi->name;

get_<field_name>

        Another way to get field value

        Examle:

        my $jedi_name = $jedi->get_name;

get_<field_name>_default

        Gets field default value from class declaration

        Examle:

                my $default_weapon = $jedi->get_weapon_default;

set_<field_name>( <value> )

        Sets field to value passed even if it is 'undef' ( which
        sometimes is handy )

        Example:

                $jedi->set_weapon(Jedi::Weapon::DualBladeLightSaber->new());
                $jedi->set_dark_side_points(undef);

BUGS ^

Please report them

SEE ALSO ^

Class::Accessor

AUTHOR ^

Denys Vorobyov, <denysv@primus.ca>

COPYRIGHT AND LICENSE ^

Copyright 2005 by Denys Vorobyov

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

syntax highlighting: