Leo Charre > LEOCHARRE-Class2-1.02 > LEOCHARRE::Class2

Download:
LEOCHARRE-Class2-1.02.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
Report a bug
Module Version: 1.02   Source   Latest Release: LEOCHARRE-Class2-1.03

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.