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

NAME

Attribute.pm - generates all get- and set-methods for new classes created by classgen.

VERSION

3.03

SYNOPSIS

Within classgen called as:

        use Attribute;                  # work with object Attribute
        my $attr = Attribute->new();    # derive a new Attribute instance $attr

Let Ex.pm be a generated class, with internal variables $var, %entry and @list:

        use Ex.pm;                      # use generated class
        my $ex=Ex->new();               # creating a new object
        $ex->set_var('this is a test'); # setting instance variable $var
        $ex->set_h_entry( 12, twelve ); # like $entry{12}='twelve'
        $ex->set_h_entry( 60, sixty );  # 
        $ex->set_l_list( 3, -100 );     # like $list[3]=-100;
        $x=$ex->get_var();              # like $x=$var;
        $x=$ex->get_h_entry(12);
        @keys=$ex->get_keys_h_entry();  # get all keys of internal %entry
etc.

DESCRIPTION

It is good OOP-style to access and manipulate instance variables of a class NEVER by a direct call, but via appropriate methods. You should always follow this concept to avoid problems when inheriting from your object oriented code.

classgen has been designed for exactly this purpose: To have all necessary methods available for all instance variables. Right from the start.

General syntax for generated methods

The methods name itself should uniquely identify the type of instance variable accessed or modified. So the general method convention is:

  • methodname_scalarname to work on a scalar instance

  • methodname_h_hashname to work on a hash instance

  • methodname_l_listname to work on a list instance

With this convention you will always know what to expect, e.g.:

        use Example;
        $ex = Example->new();
        ...
        print $ex->get_number;          # access instance variable $number
        print $ex->get_l_cities;        # access instance variable @cities
        print $ex->get_keys_h_country;  # get keys of inst.var. %country

For %hashes and @lists it is sometimes necessary to access the reference of the instance variable. Then use:

  • methodname_rh_hashname to find the reference of a %hash instance

  • methodname_rl_listname to find the reference of a @list instance

Some other methods deviate from these rules when purpose and involved type of instance variable are self evident. See below.

Generated methods for $scalars

For all $scalar instance variables in classgens control file Attribute.pm generates (the phrase 'scalar' is replaced by the actual name of the $scalar instance variable):

  • clear_scalar(): generated clear() method for $scalars

  • get_scalar(): generated accessor method for $scalars

  • set_scalar($value): generated manipulator for $scalars

Example: control file contains $var of class Example.

Attribute generates clear_var(), get_var() and set_var. Class Example can be used like:

        use Example;    # use object Example
        use strict;     # recommended

        my $ex = Example->new();        # creating a new Example instance
        $ex->clear_var();               # set $var in Example to undef
        $ex->set_var( "what a wonderful day" );

        print $ex->get_var();

Generated methods for %hashes

For all %hash instance variables in classgens control file Attribute.pm generates (the phrase 'hash' is replaced by the actual name of the %hash instance variable):

  • clear_h_hash(): generated clear() method for %hashes

  • delete_h_hash_at($key): deletes $key from internal %hash.

  • get_h_hash(): generated accessor to the %hash itself

  • get_h_hash_at($key): generated key-based accessor to %hash

  • get_keys_h_hash(): generated key-accessor for %hashes

  • get_rh_hash(): generated accessor to \%hashes

  • set_h_hash($key, $value): generated manipulator for %hashes

Generated methods for @lists

For all @list instance variables in classgens control file Attribute.pm generates (the phrase 'list' is replaced by the actual name of the @list instance variable):

  • clear_l_list(): generated clear() method for @lists

  • get_list_at($index): generated indexed-accessor for @lists

  • get_l_list(): generated list-accessor for @lists

  • get_rl_list(): generated accessor to \@lists

  • pop_list(): generated pop-accessor for @lists

  • push_list($value): generated push-manipulator for @lists

  • set_l_list($index, $value): generated manipulator method for @lists

Internal methods

A few methods are needed to provide all this:

  • sub new: constructor

  • sub get_type: to determine type of instance variable

  • sub get_var: access instance-variable

  • sub generate_blessed_h: for blessing an anonymous hash

  • sub generate_key: used to define appropriate method names

  • sub name: generate a standard name

  • sub simplify_var: to remove '^[\$%@]_+'

  • sub set_var: manipulate instance-variable

ENVIRONMENT

Nothing special. Just use Perl5.

DIAGNOSTICS

There is no special diagnostics. Attribute.pm is used within classgen which is called with the -w option.

BUGS

No bugs known.

FILES

Please refer to classgen.

SEE ALSO

perldoc classgen

AUTHOR

Name: Michael Schlueter email: mschlue@cpan.org

COPYRIGHT

Copyright (c) 2000, Michael Schlueter. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.

2 POD Errors

The following errors were encountered while parsing the POD:

Around line 532:

'=item' outside of any '=over'

Around line 556:

You forgot a '=back' before '=head1'