Su::Model - A module to treat user data.


  Su::Model::load_model('Pkg::SomeModel', {share => 1} )->{field_A} = $value;

  my $value  = Su::Model::load_model('Pkg::SomeModel')->{field_A};


Su::Model holds the data used in your application. For convenience, Su provides method to generate Model class.

 use Su::Model;




Save the passed data in application scope.

 Su::Model->attr( 'key1', 'value1' );
 my $value = Su::Model->attr('key1');

 Su::Model->attr->{key4} = 'value4';
 my $value = Su::Model->attr->{key4};

A Constructor.

  generate_model('SomeModel', qw(field1 string field2 number field3 date));

  generate_model(NAME, &rest @(FIELD, VALUE));

Generate the model class using the passed model name. If the optional parameters are passed, then generate the model class using the passed parameter as the value of the model field of the Model. VALUE can be specified as scalar or hash reference.

The model field of the generated Model is like the following.

  my $model=
      field1 =>  "value1",
      field2 => {somekey => "hashvalue"},

You can generate Model class from command line using the following command.

  perl -I../lib -MSu::Model -e '{generate_model("ModelClass",field1,"value1",field2,"value2")}'
  perl -I../lib -MSu::Model -e '{generate_model("Pkg::ModelClass",field1,"value1",field2,"value2")}'

If you want to specify the directory to generate the Model class, then pass the base parameter like the following sample.

  perl -MSu::Model=base,lib -e '{generate_model("ModelClass",field1,value1,field2,value2)}'

You can specify the package name using the dir parameter.

  perl -MSu::Model=dir,PkgName -e '{generate_model("ModelClass",field1,value1,field2,value2)}'

Note that if the model name is specified with qualified package name, then this dir parameter not effect.

If generation is success, this subroutine return the generated file name, else should die or return undef.


Loat the Model object from the passed model name and return it's model field. Note that this mothod do not return the instance of the loaded model object itself.

Functional style usage is like the following.

 my $model_href = Su::Model::load_model('SomeModel');

OO Style usage is like the following.

 my $mdl = Su::Model->new;
 $model_href = $mdl->load_model('Pkg/Mdl2');
 $model_href = $mdl->load_model('Pkg::Mdl3');

If you want to set some data to the model and access the data from the model, then the sample code becomes as follwings:

  Su::Model::load_model('Pkg::SomeModel')->{value} = $value;

  my $value  = Su::Model::load_model('Pkg::SomeModel')->{value};

If you want to suppress dying because of module require error, then pass the second parameter like the following.

  my $model = $mdl->load_model( 'Pkg::SomeModel', {suppress_error => 1} );

When the second parameter is passed and load error occured, then this method return undef.

If you want to share and reuse model data, then pass the share parameter as the second parameter.

  my $model = $mdl->load_model( 'Pkg::SomeModel', {share => 1} );