NAME

Data::Polymorph - Yet another approach for polymorphism.

VERSION

Version 0.01

SYNOPSIS

my $poly = Data::Polymorph->new;

## defining external method 'freeze'

$poly->define( 'FileHandle' => freeze => sub{
  "do{ require Symbol; bless Symbol::gensym(), '".ref($_[0])."'}"
}  );

$poly->define( "UNIVERSAL" => freeze => sub{
  use Data::Dumper;
  sprintf( 'do{ my %s }', Dumper $_[0]);
});

## it returns `undef'
FileHandle->can('freeze');
UNIVERSAL->('freeze');

###
### applying defined method.
###

## returns "do{ requier Symbol; bless Symbol::gensym(), 'FileHandle'}"
$poly->apply( FileHandle->new , 'freeze' );

DESCRIPTION

This module provides gentle way of polymorphic behaviors definition for special cases that aren't original concerns.

Applying this solution dissipates necessity for making an original namespace dirty.

ATTRIBUTES

runs_native
##
##  If external method "foo" is not defined into the $poly...
##

$poly->runs_native(1);
$poly->apply($obj, foo => $bar ); # ... same as $obj->foo($bar)
$poly->runs_native(0);
$poly->apply($obj, foo => $bar ); # ... die

If this value is true and the object uses UNIVERSAL::can when the method is not defined.

class_methods

The dictionary of class methods.

type_methods

The dictionary of type methods.

METHODS

new
$poly = Data::Polymorph->new();
$poly = Data::Polymorph->new( runs_native => 0 ); 
$poly = Data::Polymorph->new( runs_native => 1 ); 

Constructs and returns a new instance of this class.

type
$type = $poly->type( 123  ); # returns 'Num'

Returns the type name of the given object. Types are below.

Any
  Undef
  Defined
    Value
      Num
      Str
      Glob
    Ref
      ScalarRef
      HashRef
      ArrayRef
      CodeRef
      RefRef

They seem like Moose Types.

Actually, I designed these types based on the man pages from Moose::Util::TypeConstraints. Because these were not designed for constraint, they never relate with Moose types.

is_type
$poly->is_type('Any') ; # => 1
$poly->is_type('Str') ; # => 1
$poly->is_type('UNIVERSAL') ; # => 0

Returns true if given name is a defined type name. Otherwise, returns false.

super_type
$type = $poly->super_type('Str');   # => Value
$type = $poly->super_type('Undef'); # => Any

Returns name of the type which is the super type of the given type name.

class
$type = $poly->class( $obj );

Returns class name or type name of the given object.

define_type_method
$poly->define_type_method('ArrayRef' => 'values' => sub{ @$_[0]});
$poly->define_type_method('HashRef'  => 'values' => sub{ values %$_[0]});
$poly->define_type_method('Any'      => 'values' => sub{ $_[0] });

Defines a method for the given type.

define_class_method
$poly->define_class_method( 'Class::Name' => 'method' => sub{
  #                    code reference
}  );

Defines an external method for a given class which can be appliabled by the instance of this class.

define
$poly->define('Class::Name' => 'method' => sub{ ... } );
$poly->define('Undef'       => 'method' => sub{ ... } );

Defines a method for a type or a class.

type_method
$meth = $poly->type_method( 'ArrayRef' => 'values' );

Returns a CODE reference which is invoked as the method of given type.

super_type_method
$meth = $poly->super_type_method( 'ArrayRef' => 'values' );

Returns a CODE reference which is invoked as the super method of given type.

class_method
$meth = $poly->class_method( 'A::Class' => 'method' );
($poly->apply( 'A::Class' => $method ) or
 sub{ confess "method $method is not defined" } )->( $args .... );

Returns a CODE reference which is invoked as the method of given class.

super_class_method
$super = $poly->super_class_method( 'A::Class' => 'method' );
($poly->apply( 'A::Class' => $method ) or
 sub{ confess "method $method is not defined" } )->( $args .... );

Returns a CODE reference which is invoked as the super method of given class.

method
$code = $poly->method( []              => 'values' );
$code = $poly->method( qr{foo}         => 'values' );
$code = $poly->method( FileHandle->new => 'values' );

Returns a CODE reference which is invoked as the method of given object.

super_method
$code = $poly->super_method( []              => 'values' );
$code = $poly->super_method( qr{foo}         => 'values' );
$code = $poly->super_method( FileHandle->new => 'values' );
$code = $poly->super_method( 'Any' => 'values' ); # always undef

Returns a CODE reference which is invoked as the super method of given object.

apply
$poly->apply( $obj => 'method' => $arg1, $arg1 , $arg3 .... );

Invokes a method which was defined.

super
$poly->super( $obj => 'method' => $arg1, $arg1 , $arg3 .... );

Invokes a super method which was defined..

AUTHOR

lieutar, <lieutar at 1dk.jp>

BUGS

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

and...

Even if I am writing strange English because I am not good at English, I'll not often notice the matter. (Unfortunately, these cases aren't testable automatically.)

If you find strange things, please tell me the matter.

SUPPORT

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

perldoc Data::Polymorph

You can also look for information at:

COPYRIGHT & LICENSE

Copyright 2008 lieutar, all rights reserved.

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