Leo Charre > LEOCHARRE-Class2-1.03 > LEOCHARRE::Class2

Download:
LEOCHARRE-Class2-1.03.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
Report a bug
Module Version: 1.03   Source  

NAME ^

LEOCHARRE::Class2

METHODS ^

make_constructor()

   __PACKAGE__->make_constructor();

creates normal blessed object from optional hashref

make_accessor_setget()

argument is name of accessor to create in class. can provide a list of names.

optionally, if the argument is an array ref, the second element should be the default value to set.

This example creates 'model', 'year' and 'make' setget methods in the current class:

   __PACKAGE__->make_accessor_setget(
      'model', 
      'make', 
      'year',
   );

This is example is the same but sets defaults for make and year

   __PACKAGE__->make_accessor_setget(
      'model', 
      [ 'make' => 'toyota' ], 
      [ 'year' => '1999'   ],
   );

If those values are passed to the constructor or via method, they change..

   my $o = Thing->new({ make => 'ford' });
   $o->year(2001);   # changes to 2001
   $o->year;         # returns 2001
   $o->model;        # returns undef
   $o->make;         # returns ford

You can also pass a hashref as argument to make setget methods, keys are names, vals are the default values..

   __PACKAGE__->make_accessor_setget({
      model => undef,
      make  => 'toyota',
      year  => '1999',
   });

How the method created works

This checks for a value in order of 1) in (a)rgument to method 1) in (o)bject instance data (self) 2) in (c)lass package

If a value is provided, the object's data is changed, not the class. If no value is provided, we return the object's data, if none, the class, if none, undef.

This example creates an object setget accessor that defaults to the name jimmy, stored in the class.

   __PACKAGE__->make_accessor_setget(['name' => 'jimmy']);

Our class being named 'My::House', we now have

   &My::House::name
   $My::House::name

Value in object instance is stored in self Thus, if you provide to the constructor a 'self' hashref that specifies a 'name', the method name would return that value.

CAVEATS ^

class wide defaults

Please note, if you set a default value to be an anon ref, this is indeed set for the whole class. BUT, when an object is intanced, the instance data will actually hold a COPY of the value.

For example:

   Neighborhood->make_accessor_setget([ houses => ['green','red'] ]);

Would normally cause all object instances of Neighborhood to refer to the same houses anon array ref. We don't want that, we just want to use that as a default. So the above will actually result in

$Neighborhood::houses = $your_ref

Don't think too hard about it.

resetting methods that had defaults

If you provide a default and then you set it to undef, we do not load the defaults again..

   __PACKAGE__->make_accessor_setget([ name => 'leo' ]);

   $self->{name} = undef;

   $self->name; # returns undef

Basically it means that if the blessed hashref does not have the method data key, THEN we do attempt to load a default. This is so you don't get unexpected results.