Wei Huang > Class-Builder > Class::Builder

Download:
Class-Builder-1.010.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 1.010   Source  

NAME ^

  Class::Builder - auto-generator of class accessors/special methods

SYNOPSIS ^

Creating Class Members (fields):

  package SystemUser;
  use Class::Builder {
    uid    => { number => undef },
    uname  => { string => 'default user' },
    group  => { arrayref => []},
    ctime  => { number => time },
    disable => { boolean => 0 },
    log_count => { counter => 0},
  };
  1;

  # then in your script:
  package main;
  use SystemUser;

  my $user = new SystemUser ({name => 'chopin'});
  print $user->uname(), "\n"; # print chopin;
  print scalar localtime $user->ctime(), "\n";

  foreach my $group ($user->group){
    &do_something($group);
  }

  my $user = new SystemUser ({uname => 'chopin'});
  print $user->uname(), "\n"; # print chopin;
  print scalar localtime $user->ctime(), "\n";

  sub system_user_loggin{
    if($user->disable){
      die "you account is disabled, contact system administrator.";
    }
    # ... do some thing
    $user->log_count_add;
    print "you are logged into the system. you have loged ",$user->log_count," times.";
  }

  system_user_loggin(); # print $user->log_count as 1.

Special Methods

  package SystemUser;
  use Class::Builder {
    '-methods' => {
      constructor => 'create',
      dumper => 'as_string',
      clone  => 'copy',
    },
    uid    => { number => undef },
    uname  => { string => '' },
  };
  1;

  package main;
  use SystemUser;

  my $user = SystemUser->create({name=>'mozart'});

  my $user_cp = $user->copy;
   # deep copy the structure, not only the pointer

  print $user_cp->as_string() # dump the contents of $user

There also a function `struct' opened for you, let you create modules `on the fly':

  package SystemUser;
  use Class::Builder;

  struct Machine => {
    machine_name => { string => 'default name' },
    location => {string => '' }
  }; # define Machine before use it.

  struct {
    uname => {string => ''},
    main_machine => { Machine => undef },
  }; # implicit class name: 'SystemUser' in this case.


  package main;
  my $machine = new Machine( machine_name => 'veryslow' );
  my $user = new SystemUser( uname => 'mozart', main_machine => $machine );

  print $user->main_machine->machine_name();

DESCRIPTION ^

Class::Builder is a module helps OOP programmers to create `class's (packages, in terms of perl) in several ways of automation.

If you've used one of Class::MethodMaker, Class::Struct or Class::Accessor, the concept of Class::Builer is not newer to you. In fact, this module can be viewed as a combination of the above modules. I'm trying to include most frequently used functions, while keep the module as lightweight as possible.

Field Methods:

To create a new member field for you class, simply say:

  use Class::Builder {
    <fieldname> => { <fieldtype> => <default_vaule>, [ final => 1 ]},
  };

where <fieldname> is the name you want to set as a member field, it must be legal as a name of perl function.

If you defined the field as `final', you can not use the accessor methods to change the field's value. But you still can change it by directly access data stored in the object (a blessed hashref).

`final' attribute is ignored by `hashref' and `arrayref' field, under current implementation.

The following field types are available now:

Two special functions applies for all member fields:

Note that currently all types are implemented as a key of a perl hash, so you can assign any scalar value to any field without causing errors. Typically, number field and string field have no difference it all, it is introduced only for future development (such as dynamic linkage with databases). I.e, you can sign a perl string to a number field, but we do not recommend you do that.

On-the-Fly Classes

You can use function struct() in three styles:

Special Methods

Class::Builder also has a ability to create special methods:

INHERITAGE ^

To create a subclass of Class::Builder, at lease you must define two functions:

BUGS ^

  This package is not yet well tested.

AUTHOR ^

  Wei, Huang < huang@toki.waseda.jp >

SEE ALSO ^

syntax highlighting: