Yuki Kimoto > Simo-Util-0.0401 > Simo::Util

Download:
Simo-Util-0.0401.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
Report a bug
Module Version: 0.0401   Source  

NAME ^

Simo::Util - Utility for Simo [DISCOURAGED]

CAUTION ^

This module is discouraged now, because I develope new module Object::Simple now.

Object::Simple is very simple class builder. It is clean, compact, and fast.

VERSION ^

Version 0.0401

DESCRIPTION ^

Simo::Util is Utitly class for Simo.

This class provide some utility function for Simo.

CAUTION ^

Simo::Util is yet experimental stage.

Please wait until this is stable.

SYNOPSIS ^

    use Simo::Util qw( get_values get_hash set_values );
    
    my( $title, $author ) = get_values( $book, 'title', 'author' );
    
    my %hash = get_hash( $book, 'title', 'author' );
    my $hash_ref = get_hash( $book, 'title', 'author' );
    
    set_values( $book, title => 'Simple OO', author => 'kimoto' );
    
    use Simo::Util qw( err );
    
    # check error
    if( err ){
        if( err->type eq 'err_type' ){
            my $msg = err->msg;
            my $pos = err->pos;
            my $pkg = err->pkg;
            my $attr = err->attr;
            my $val = err->val;
            
            my $a = err->info->{ a };
            my $b = err->ingo->{ b };
            
            # do something
        }
    }

EXPORT ^

By default, no funcion is exported.

All functions can be exported.

    use Simo::Util qw( get_values get_hash set_values );

FUCNTION ^

err

err function convert $@ to Simo::Error object.

If $@ is already Simo::Error object, this function do nothing.

If you accesse $@ using err function, You do not have to distinguish that $@ is string or Simo::Error object.

The following is eneral sample using o function and err function.

new_and_validate method construct objcet and validate its values. and check error using err function.

    use Simo::Util( o err );
    
    my $book = eval{
        new_and_validate(
            'Book',
            { title => 'aaaaaaaaa' },
            { title => sub { length $_ < 5 } }
        );
    };
    
    if( err ){
        if( err->attr eq 'title' ){
            my $type = err->type;
            my $msg = err->msg;
            my $pos = err->pos;
            my $pkg = err->pkg;
            my $val = err->val;
            
            my $a = err->info->{ a };
            my $b = err->ingo->{ b };
            
            # do something
        }
    }

type, msg, pos, pkg, attr,val is accessors of Simo::Error object.

See also Simo::Error

validate

'validate' is the method for validating.

    my $book = Book->new( title => 'Good time', price => 3000);
    validate(
        $book,
        title => sub{ length $_ < 30 },
        prcie => sub{ $_ > 0 && $_ < 3000 }
    );

If validator function return false value, 'validate' throw Simo::Error object.

'value_invalid' is set to 'type' field of Simo::Error object.

new_and_validate

'new_and_validate' construct object and validate object.

You can use 2 type of argument.

First: key-value-validator

    my $book = new_and_validate(
        'Book',
        title => 'a', sub{ length $_ < 30 },
        price => 1000, sub{ $_ > 0 && $_ < 50000 },
        auhtor => 'Kimoto', sub{ 1 }
    );

If you do not validate some field, you pass sub{ 1 } to validator.

Second: { key => value }, { key => validator }

    my $book = new_and_validate(
        'Book',
        { title => 'a', price => 'b' },
        { title=> sub{ length $_ < 30 }, price => sub{ $_ > 0 && $_ < 50000 } }
    );

This method return constructed object.

define_class

'define_class' define class having some accessors.

    define_class( 'Book', qw/title author/ );

You can use Book class after this.

    my $book = Book->new( title => 'Good news', author => 'Kimoto' );

get_values

'get_values' get the values.

    my ( $title, $author ) = get_values( $book, qw/ title author / );

get_hash

'get_hash' get the hash of specified fields.

    my $book = Book->new( title => 'Good cat', author => 'Kimoto', price => 3000 );
    my $hash = get_hash( $book, qw/ title author / );

$hash is that

    {
        title => 'Good cat',
        auhtor => 'Kimoto'
    }

set_values

'set_values' set values of the object.

    set_values( $book, title => 'Good news', author => 'kimoto' );

You can also pass hash reference

    set_values( $book, { title => 'Good news', author => 'kimoto' } );

new_from_objective_hash

'new_from_objective_hash' construct object from a objective hash.

    my $book = new_from_objective_hash( undef, $objective_hash );

You maybe hear the name of objective hash at first.

objective hash is the hash that contain the information of object accroding to the following rules.

1. '__CLASS' is class name.
2. '__CLASS_CONSTRUCTOR' is object constructor name. If this is ommited, 'new' is used as constructor name.

objective hash sample is

    my $objective_hash = { 
        __CLASS => 'Book',
        __CLASS_CONSTRUCTOR => 'new',
        
        title => 'Good thing',
        
        author => {
            __CLASS => 'Person',
            
            name => 'Kimoto',
            age => 19,
            country => 'Japan'
        },
        
        price => 2600
    };

'Person' object is automatically constructed and set to 'author' field.

After that, 'Book' object is constructed .

new_from_xml

'new_from_xml' construct object from a XML file.

    my $book = new_from_xml( undef, $xml_file );

XML file sample is

    <?xml version="1.0" encoding='UTF-8' ?>
    <root __CLASS="Book" >
      <title>Good man</title>
      
      <author __CLASS="Person">
        <name>Kimoto</name>
        <age>28</age>
        <country>Japan</country>
      </author>
    </root>

You can use the xml using the form of objective hash. See also 'new_from_objective_hash'.

The xml parser of this method is 'XML::Simple'. See also XML::Simple

set_values_from_objective_hash

'set_values_from_objective_hash' set values from a objective hash.

    set_values_from_objective_hash( $book, $objective_hash );

See also 'new_from_objective_hash'.

set_values_from_xml

'set_values_from_xml' set values loading from XML file.

    set_values_from_xml( $book, $xml_file );

You can use the xml using the form of objective hash. See also 'new_from_objective_hash'.

The xml parser of this method is 'XML::Simple'. See also XML::Simple

run_methods

'run_methods' call multiple methods.

    my $result = run_methods(
        $book_list,
        find => [ 'author' => 'kimoto' ],
        sort => [ 'price', 'desc' ],
        'get_result'
    );

This method return the return value of last method ( this example, retrun value of 'get_result' )

call

'call' is aliase of 'run_methods'

filter_values

'filter_values' convert multiple values.

    filter_values( $book, sub{ uc $_ }, qw/ title author / );

$book->title and $book->author is converted to upper case.

This method also filter the values of array ref.

    $book->author( [ 'Kimoto', 'Matuda' ] );
    filter_values( $book, sub{ uc $_ }, qw/ author / );

'Kimoto' and 'Matuda' is converted to upper case.

This method also filter the values of hash ref.

    $book->info( { country => 'Japan', quality => 'Good' } );
    filter_values( $book, sub{ uc $_ }, qw/ info / );

'Japan' and 'Good' is converted to upper case.

These 'filter_values' logic is used by 'encode_values' and 'decode_values'.

encode_values

'encode_values' encode multiple values.

    encode_values( $book, 'utf8', qw/ title author / );

$book->title and $book->author is encoded.

This method also encode the values of array ref.

    $book->author( [ 'Kimoto', 'Matuda' ] );
    encode_values( $book, 'utf8', qw/ author / );

'Kimoto' and 'Matuda' is encoded.

This method also encode the values of hash ref.

    $book->info( { country => 'Japan', quality => 'Good' } );
    encode_values( $book, 'utf8', qw/ info / );

'Japan' and 'Good' is encoded.

decode_values

'decode_values' decode multipul values.

    decode_values( $book, 'utf8', qw/ title author / );

$book->title and $book->author is decoded.

This method also decode the values of array ref.

    $book->author( [ 'Kimoto', 'Matuda' ] );
    decode_values( $book, 'utf8', qw/ author / );

'Kimoto' and 'Matuda' is decoded.

This method also decode the values of hash ref.

    $book->info( { country => 'Japan', quality => 'Good' } );
    decode_values( $book, 'utf8', qw/ info / );

'Japan' and 'Good' is decoded.

clone

'clone' copy the object deeply.

    my $book_copy = clone( $book );

'clone' is the same as Storable::clone. See also Storable

freeze

'freeze' serialize the object.

    my $book_freezed = freeze( $book );

'freeze' is the same as Storable::freeze. See also Storable

thaw

'thaw' resotre the freezed object.

    my $book = thaw( undef, $book_freezed );

'thaw' is the same as Storable::thaw. See also Storable

AUTHOR ^

Yuki Kimoto, <kimoto.yuki at gmail.com>

BUGS ^

Please report any bugs or feature requests to bug-simo-util at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Simo-Util. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT ^

You can find documentation for this module with the perldoc command.

    perldoc Simo::Util

You can also look for information at:

COPYRIGHT & LICENSE ^

Copyright 2009 Yuki Kimoto, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.