View on
Wei Huang > Class-Builder > Class::Builder



Annotate this POD


New  1
Open  0
View/Report Bugs
Module Version: 1.010   Source  


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


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},

  # 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){

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

  sub system_user_loggin{
      die "you account is disabled, contact system administrator.";
    # ... do some thing
    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 => '' },

  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();


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:


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


  This package is not yet well tested.


  Wei, Huang < >


syntax highlighting: