Class-Maker

 view release on metacpan or  search on metacpan

lib/Class/Maker.pm  view on Meta::CPAN

=head2 FIELDS

Fields are the keys in the hashref given as the second (or first if the first argument (classname) is omitted) argument to "class". Here are the basic fields (for adding new fields read the Class::Maker::Basic::Fields).

=head3 isa => aref

Same as the @ISA array in an package (see perltoot). 

Some short-cut syntax is available:

a) when the name is started with an '.' or '*' the package name is extrapolated to that name:

 package Far::Far::Away;

 class Galaxy,
 {
   isa => [qw( .AnotherGalaxy )],
 };

Then '.AnotherGalaxy' becomes expanded to 'Far::Far::Away::AnotherGalaxy'.

=head3 public => href
 
 public => 
 {
   int => [qw(id)],
 },

leads to a attribute-handler which can be used like:

 $obj->id( 123 );

 my $value = $obj->id;

Because the default handler is an lvalue function, the following call is also valid:

 $obj->id = 5678;


These keys are 'type-identifiers' (no fear, its simple), which help you to sort things. In general these are used to create handlers for the type. It is somehow like the get/set like method functions to access class-properties, but its more generaliz...

   public =>
   {
    scalar => ...
    array => ...
    hash => ...

    _anthing_here_ => ..    
   }

Because b<array> and b<hash> are internally decleared and creating special mutators/handlers they will be not create scalar handlers, 
but 'scalar' and '_anything_here' will create scalara mutators, as they are forwarded to the default scalar handlers; both are internally not explicitly defined. 
The mechanism is extendable, see L<Class::Maker::Basic::Fields>.

=head3 private => href

All properties in the 'private' section, get a '_' prepended to their names.

 private =>
 {
   int => [qw(uid gid)],
 },

So you must access 'uid' with $obj->_uid();

 public =>
 {
   int => [qw(uid gid)],

   string => [qw(name lastname)],

   ref => [qw(father mother)],

   array => [qw(friends)],

   custom => [qw(anything)],
 },

Nothing more, nothing less. The significant part is that no encapsulation as such is present (as in cpp). The only encapsulation is the "secret" that
you have to prepend and '_' in front of the name.

=head3 configure => href

This Field is for general options. Basicly following options are supported:

a) new: The name of the default constructor is 'new'. With this option you can change the name to something of your choice. For instance:

 configure => 
 {
   new => 'connect'
 },

Could be used for database objects. So you would use

 my $obj AnyClass->connect( );

to create an AnyClass object.

PS. Class::Maker provides a very sophisticated default constructor that does a lot (including the inhertance issues) and is explained somewhere else.

c) I<private>: Prefix string (default '_') for private functions can be changed with this.

 private =>
 {
   int => [qw(dummy1)],
 },
 
 configure =>
 {
   private => { prefix => '__' },
 },

would force to access 'dummy1' via ->__dummy1().

=head3 automethod

Reserved.

=head3 has

Reserved. Is planned to be used for 'has a' relationships.

=head3 default => href

Give default values for class attributes. It is the same as the handler was called with the value within the L<_postinit> function.

 default =>
 {



( run in 1.720 second using v1.01-cache-2.11-cpan-5735350b133 )