Class::PObject::Type - Column type specification
pobject User => { columns => ['id', 'login', 'psswd', 'email', 'bio'], tmap => { id => 'INTEGER', login => 'VARCHAR(18)', psswd => 'ENCRYPT', email => 'VARCHAR(40)', bio => 'TEXT', key => 'MD5' } };
Class::PObject allows you to specify types for values that each column will hold. There are several uses for this:
Allows specific drivers to be able to optimize object storage as well as retrieval
Allows to filter certain column values before being inserted. Good example would be, MD5 and ENCRYPT column types, where each value should be encrypted before being stored in the database. This encryption will also apply while loading objects
The rest of this manual will talk about how column specification is designed. This information will be useful only if you want to be able to extend this type-mapping functionality.
Each column type is treated as the name of the class with the same specs as any other class created using pobject construct. Any attributes, if applicable, should be enclosed into parenthesis.
pobject
For example, types, VARCHAR(100), INTEGER, ENCRYPT, MD5 and TEXT are all valid column types.
VARCHAR(100)
INTEGER
ENCRYPT
MD5
TEXT
Let's assume the following class declaration:
pobject Author => { columns => ['id', 'name'], tmap => { name => 'VARCHAR(32)' } };
While declaring classes, we don't have to declare types of all columns. If we don't all the columns would default to VARCHAR(255), with the exception of id column, which defaults to INTEGER.
We said each type was a class. These classes have the same interface as any other class generated through pobject construct. Instead of being generated out of Class::PObject::Template, however, these particular classes inherit from Class::PObject::Type.
So, how pobject will interpret our VARCHAR(32) anyway?
It assumes that there is a class called VARCHAR, and creates a new object of this class by calling its new() - constructor with the value passed to this column.
new()
For example, if we were to do something like:
$author = new Author(); $author->name("Sherzod Ruzmetov");
When we called name(), that's what happens behind the scenes:
name()
require VARCHAR; $type = new VARCHAR(id=>"Sherzod Ruzmetov", args=>32);
And when we save the object into database, it would call its id() method and uses its return value to store into disk.
id()
When the column is loaded, pobject will only load the strings as are, and will inflate the object only once needed.
So in other words, when we do:
$author = Author->load(217);
pobject first just loads the column values as are, and when we try to access the value of the column by saying:
$name = $author->name();
it will do something like:
require VARCHAR; $type = VARCHAR->load("Sherzod Ruzmetov"); return $type
So, in other words, above name() method returns an object. However, this object in string context, will always return the value of its id thanks to operator overloading.
Because type classes provide the same interface as any other pobject class, we could define object relationships as easily as defining column types. Refer to Class::PObject's online manual for further details.
Since each column is an object, each column can have its own methods, which we'd like to call member functions. As of this release following member functions are supported by all column types:
member functions
Returns a substring off the column's string value. Range of this sub-string is defined by $offset and $length. Example:
$offset
$length
pobject Peprson => { columns => ['id', 'name'] }; $me = Person->new(); $me->name("Sherzod Ruzmetov"); $substr = $me->name()->substr(0, 5);
In the above example $substr will hold first 6 letters of whatever $me->name() could've returned.
$substr
$me->name()
Identical to Perl's built-in lc() function
Identical to Perl's buil-in uc() function
Identical to Perl's built-in ucfirst() function
Identical to Perl's built-in lcfirst() function
Class::PObject::Type::INTEGER, Class::PObject::Type::VARCHAR, Class::PObject::Type::ENCRYPT, Class::PObject::Type::MD5, Class::PObject::Type::SHA1
For author and copyright information refer to Class::PObject's online manual.
To install Class::PObject, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::PObject
CPAN shell
perl -MCPAN -e shell install Class::PObject
For more information on module installation, please visit the detailed CPAN module installation guide.