| 1 | |||
| 2 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | package Data::Polymorph; | ||
| 6 | |||
| 7 | 3 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 3 | ▶ | use Scalar::Util qw( blessed looks_like_number ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 3 | ▶ | use UNIVERSAL qw( isa can ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Data::Polymorph - Yet another approach for polymorphism. | ||
| 14 | |||
| 15 | =head1 VERSION | ||
| 16 | |||
| 17 | Version 0.01 | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | our $VERSION = '0.01'; | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | my $poly = Data::Polymorph->new; | ||
| 26 | |||
| 27 | ## defining external method 'freeze' | ||
| 28 | |||
| 29 | $poly->define( 'FileHandle' => freeze => sub{ | ||
| 30 | "do{ require Symbol; bless Symbol::gensym(), '".ref($_[0])."'}" | ||
| 31 | } ); | ||
| 32 | |||
| 33 | $poly->define( "UNIVERSAL" => freeze => sub{ | ||
| 34 | use Data::Dumper; | ||
| 35 | sprintf( 'do{ my %s }', Dumper $_[0]); | ||
| 36 | }); | ||
| 37 | |||
| 38 | ## it returns `undef' | ||
| 39 | FileHandle->can('freeze'); | ||
| 40 | UNIVERSAL->('freeze'); | ||
| 41 | |||
| 42 | ### | ||
| 43 | ### applying defined method. | ||
| 44 | ### | ||
| 45 | |||
| 46 | ## returns "do{ requier Symbol; bless Symbol::gensym(), 'FileHandle'}" | ||
| 47 | $poly->apply( FileHandle->new , 'freeze' ); | ||
| 48 | |||
| 49 | =head1 DESCRIPTION | ||
| 50 | |||
| 51 | This module provides gentle way of polymorphic behaviors definition | ||
| 52 | for special cases that aren't original concerns. | ||
| 53 | |||
| 54 | Applying this solution dissipates necessity for making an original | ||
| 55 | namespace dirty. | ||
| 56 | |||
| 57 | =head1 ATTRIBUTES | ||
| 58 | |||
| 59 | =over 4 | ||
| 60 | |||
| 61 | =item C<runs_native> | ||
| 62 | |||
| 63 | ## | ||
| 64 | ## If external method "foo" is not defined into the $poly... | ||
| 65 | ## | ||
| 66 | |||
| 67 | $poly->runs_native(1); | ||
| 68 | $poly->apply($obj, foo => $bar ); # ... same as $obj->foo($bar) | ||
| 69 | $poly->runs_native(0); | ||
| 70 | $poly->apply($obj, foo => $bar ); # ... die | ||
| 71 | |||
| 72 | If this value is true and the object uses C<UNIVERSAL::can> | ||
| 73 | when the method is not defined. | ||
| 74 | |||
| 75 | =item C<class_methods> | ||
| 76 | |||
| 77 | The dictionary of class methods. | ||
| 78 | |||
| 79 | =item C<type_methods> | ||
| 80 | |||
| 81 | The dictionary of type methods. | ||
| 82 | |||
| 83 | =back | ||
| 84 | |||
| 85 | =head1 METHODS | ||
| 86 | |||
| 87 | =over 4 | ||
| 88 | |||
| 89 | =item C<new> | ||
| 90 | |||
| 91 | $poly = Data::Polymorph->new(); | ||
| 92 | $poly = Data::Polymorph->new( runs_native => 0 ); | ||
| 93 | $poly = Data::Polymorph->new( runs_native => 1 ); | ||
| 94 | |||
| 95 | Constructs and returns a new instance of this class. | ||
| 96 | |||
| 97 | =cut | ||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | { | ||
| 102 | my @Template = | ||
| 103 | ( | ||
| 104 | [ class_methods => sub{{}} ], | ||
| 105 | [ type_methods => sub{ | ||
| 106 | return | ||
| 107 | [ | ||
| 108 | [Undef => sub{ !defined( $_[1] ); },{},'Any'], | ||
| 109 | [ScalarRef => sub{ isa( $_[1], 'SCALAR' ) },{},'Ref'], | ||
| 110 | [CodeRef => sub{ isa( $_[1], 'CODE' ) },{},'Ref'], | ||
| 111 | [ArrayRef => sub{ isa( $_[1], 'ARRAY' ) },{},'Ref'], | ||
| 112 | [HashRef => sub{ isa( $_[1], 'HASH' ) },{},'Ref'], | ||
| 113 | [GlobRef => sub{ isa( $_[1], 'GLOB' ) },{},'Ref'], | ||
| 114 | [RefRef => sub{ isa( $_[1], 'REF' ) },{},'Ref'], | ||
| 115 | [Ref => sub{ ref( $_[1] ) and 1 },{},'Defined'], | ||
| 116 | [Num => sub{ looks_like_number( $_[1] ) },{},'Value'], | ||
| 117 | [Glob => sub{ isa(\$_[1],'GLOB' ) },{},'Value'], | ||
| 118 | [Str => sub{ isa(\$_[1],'SCALAR'); },{},'Value'], | ||
| 119 | [Value => sub{ 1 },{},'Defined'], | ||
| 120 | [Defined => sub{ 1 },{},'Any'], | ||
| 121 | [Any => sub{ 1 },{},undef], | ||
| 122 | ] | ||
| 123 | }], | ||
| 124 | |||
| 125 | [ _dic => sub{ | ||
| 126 | my $self = shift; | ||
| 127 | return { map{ ($_->[0] , $_)} @{$self->type_methods} }; | ||
| 128 | }], | ||
| 129 | |||
| 130 | [ runs_native => sub{0} ], | ||
| 131 | ); | ||
| 132 | |||
| 133 | sub{ | ||
| 134 | my ( $caller ) = caller; | ||
| 135 | foreach (@_){ | ||
| 136 | my $field = $_; | ||
| 137 | my $glob = do{ no strict 'refs'; \*{"${caller}::$field"} }; | ||
| 138 | *{$glob} = sub ($;$){ | ||
| 139 | my $self = shift; | ||
| 140 | return $self->{$field} unless @_; | ||
| 141 | $self->{$field} = shift; | ||
| 142 | }; | ||
| 143 | } | ||
| 144 | }->( map { $_->[0]} @Template ); | ||
| 145 | |||
| 146 | sub new { | ||
| 147 | my ($self, %args) = @_; | ||
| 148 | $self = bless {} , (blessed $self) || $self; | ||
| 149 | foreach my $spec ( @Template ){ | ||
| 150 | $self->{$spec->[0]} = $spec->[1]->($self); | ||
| 151 | } | ||
| 152 | $self->runs_native(1) if $args{runs_native}; | ||
| 153 | $self; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | |||
| 159 | =item C<type> | ||
| 160 | |||
| 161 | $type = $poly->type( 123 ); # returns 'Num' | ||
| 162 | |||
| 163 | Returns the type name of the given object. Types are below. | ||
| 164 | |||
| 165 | Any | ||
| 166 | Undef | ||
| 167 | Defined | ||
| 168 | Value | ||
| 169 | Num | ||
| 170 | Str | ||
| 171 | Glob | ||
| 172 | Ref | ||
| 173 | ScalarRef | ||
| 174 | HashRef | ||
| 175 | ArrayRef | ||
| 176 | CodeRef | ||
| 177 | RefRef | ||
| 178 | |||
| 179 | They seem like L<Moose> Types. | ||
| 180 | |||
| 181 | Actually, I designed these types based on the man pages from | ||
| 182 | L<Moose::Util::TypeConstraints>. | ||
| 183 | Because these were not designed for constraint, they never relate with | ||
| 184 | L<Moose> types. | ||
| 185 | |||
| 186 | =item C<is_type> | ||
| 187 | |||
| 188 | $poly->is_type('Any') ; # => 1 | ||
| 189 | $poly->is_type('Str') ; # => 1 | ||
| 190 | $poly->is_type('UNIVERSAL') ; # => 0 | ||
| 191 | |||
| 192 | Returns true if given name is a defined type name. Otherwise, | ||
| 193 | returns false. | ||
| 194 | |||
| 195 | =item C<super_type> | ||
| 196 | |||
| 197 | $type = $poly->super_type('Str'); # => Value | ||
| 198 | $type = $poly->super_type('Undef'); # => Any | ||
| 199 | |||
| 200 | Returns name of the type which is the super type of the given type name. | ||
| 201 | |||
| 202 | =item C<class> | ||
| 203 | |||
| 204 | $type = $poly->class( $obj ); | ||
| 205 | |||
| 206 | Returns class name or type name of the given object. | ||
| 207 | |||
| 208 | =cut | ||
| 209 | |||
| 210 | sub type { | ||
| 211 | my ( $self, $obj ) = @_; | ||
| 212 | foreach my $slot ( @{$self->type_methods} ) { | ||
| 213 | return $slot->[0] if $slot->[1]->($self, $obj) ; | ||
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | sub is_type { | ||
| 218 | my ($self, $type) = @_; | ||
| 219 | (exists $self->_dic->{$type}) ? 1 : 0; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub super_type { | ||
| 223 | my ($self, $type) = @_; | ||
| 224 | confess "$type is not a type" unless $self->is_type( $type ); | ||
| 225 | ($self->_dic->{$type} || [])->[3]; | ||
| 226 | } | ||
| 227 | |||
| 228 | sub class { | ||
| 229 | my ( $self, $obj ) = @_; | ||
| 230 | blessed( $obj ) or $self->type( $obj ); | ||
| 231 | } | ||
| 232 | |||
| 233 | =item C<define_type_method> | ||
| 234 | |||
| 235 | $poly->define_type_method('ArrayRef' => 'values' => sub{ @$_[0]}); | ||
| 236 | $poly->define_type_method('HashRef' => 'values' => sub{ values %$_[0]}); | ||
| 237 | $poly->define_type_method('Any' => 'values' => sub{ $_[0] }); | ||
| 238 | |||
| 239 | Defines a method for the given type. | ||
| 240 | |||
| 241 | =item C<define_class_method> | ||
| 242 | |||
| 243 | $poly->define_class_method( 'Class::Name' => 'method' => sub{ | ||
| 244 | # code reference | ||
| 245 | } ); | ||
| 246 | |||
| 247 | Defines an external method for a given class which can be appliabled | ||
| 248 | by the instance of this class. | ||
| 249 | |||
| 250 | =item C<define> | ||
| 251 | |||
| 252 | $poly->define('Class::Name' => 'method' => sub{ ... } ); | ||
| 253 | $poly->define('Undef' => 'method' => sub{ ... } ); | ||
| 254 | |||
| 255 | Defines a method for a type or a class. | ||
| 256 | |||
| 257 | =cut | ||
| 258 | |||
| 259 | sub define_type_method { | ||
| 260 | my ( $self, $class, $method , $code ) = @_; | ||
| 261 | foreach my $slot ( @{$self->type_methods}) { | ||
| 262 | next unless $slot->[0] eq $class; | ||
| 263 | return $slot->[2]->{$method} = $code; | ||
| 264 | } | ||
| 265 | confess "unknown type: $class"; | ||
| 266 | } | ||
| 267 | |||
| 268 | sub define_class_method { | ||
| 269 | my ( $self, $class, $method , $code ) = @_; | ||
| 270 | my $slot = ($self->class_methods->{$method} ||= []); | ||
| 271 | my $i = 0; | ||
| 272 | for(; $i < scalar @$slot ; $i++){ | ||
| 273 | my $klass = $slot->[$i]->[0]; | ||
| 274 | |||
| 275 | if( $klass eq $class ){ | ||
| 276 | $slot->[$i]->[1] = $code; | ||
| 277 | return; | ||
| 278 | } | ||
| 279 | |||
| 280 | last if isa $class => $klass; | ||
| 281 | } | ||
| 282 | splice @$slot, $i, 0, [$class => $code]; | ||
| 283 | } | ||
| 284 | |||
| 285 | sub define { | ||
| 286 | my ( $self, $class, $method, $code ) = @_; | ||
| 287 | goto ( $self->is_type( $class ) | ||
| 288 | ? \&define_type_method | ||
| 289 | : \&define_class_method ); | ||
| 290 | } | ||
| 291 | |||
| 292 | |||
| 293 | =item C<type_method> | ||
| 294 | |||
| 295 | $meth = $poly->type_method( 'ArrayRef' => 'values' ); | ||
| 296 | |||
| 297 | Returns a CODE reference which is invoked as the method of given type. | ||
| 298 | |||
| 299 | =item C<super_type_method> | ||
| 300 | |||
| 301 | $meth = $poly->super_type_method( 'ArrayRef' => 'values' ); | ||
| 302 | |||
| 303 | Returns a CODE reference which is invoked as the super method of given type. | ||
| 304 | |||
| 305 | =cut | ||
| 306 | |||
| 307 | sub type_method { | ||
| 308 | my ( $self, $type, $method ) = @_; | ||
| 309 | confess "$type is not a type" unless $self->is_type( $type ); | ||
| 310 | while ( $type ){ | ||
| 311 | my $slot = $self->_dic->{$type}; | ||
| 312 | my $code = $slot->[2]->{$method}; | ||
| 313 | return $code if $code; | ||
| 314 | $type = $slot->[3]; | ||
| 315 | } | ||
| 316 | undef; | ||
| 317 | } | ||
| 318 | |||
| 319 | sub super_type_method { | ||
| 320 | my ($self, $type, $method ) = @_; | ||
| 321 | confess "$type is not a type" unless $self->is_type( $type ); | ||
| 322 | my $count = 0; | ||
| 323 | for (my $slot; $type ; $type = $slot->[3] ){ | ||
| 324 | $slot = $self->_dic->{$type}; | ||
| 325 | my $code = $slot->[2]->{$method}; | ||
| 326 | next unless $code; | ||
| 327 | return $code if $count; | ||
| 328 | $count++; | ||
| 329 | } | ||
| 330 | undef; | ||
| 331 | } | ||
| 332 | |||
| 333 | =item C<class_method> | ||
| 334 | |||
| 335 | $meth = $poly->class_method( 'A::Class' => 'method' ); | ||
| 336 | ($poly->apply( 'A::Class' => $method ) or | ||
| 337 | sub{ confess "method $method is not defined" } )->( $args .... ); | ||
| 338 | |||
| 339 | Returns a CODE reference which is invoked as the method of given class. | ||
| 340 | |||
| 341 | =item C<super_class_method> | ||
| 342 | |||
| 343 | $super = $poly->super_class_method( 'A::Class' => 'method' ); | ||
| 344 | ($poly->apply( 'A::Class' => $method ) or | ||
| 345 | sub{ confess "method $method is not defined" } )->( $args .... ); | ||
| 346 | |||
| 347 | Returns a CODE reference which is invoked as the super method of given class. | ||
| 348 | |||
| 349 | =cut | ||
| 350 | |||
| 351 | sub class_method { | ||
| 352 | my ( $self, $class, $method ) = @_; | ||
| 353 | my $slot = ($self->class_methods->{$method} ||= []); | ||
| 354 | foreach my $meth ( @$slot ){ | ||
| 355 | next unless isa( $class, $meth->[0] ); | ||
| 356 | return $meth->[1]; | ||
| 357 | } | ||
| 358 | } | ||
| 359 | |||
| 360 | sub super_class_method { | ||
| 361 | my ( $self, $class, $method ) = @_; | ||
| 362 | my $slot = ($self->class_methods->{$method} ||= []); | ||
| 363 | my $count = 0; | ||
| 364 | foreach my $meth ( @$slot ){ | ||
| 365 | next unless isa( $class, $meth->[0] ); | ||
| 366 | return $meth->[1] if $count; | ||
| 367 | $count++; | ||
| 368 | } | ||
| 369 | } | ||
| 370 | |||
| 371 | =item C<method> | ||
| 372 | |||
| 373 | $code = $poly->method( [] => 'values' ); | ||
| 374 | $code = $poly->method( qr{foo} => 'values' ); | ||
| 375 | $code = $poly->method( FileHandle->new => 'values' ); | ||
| 376 | |||
| 377 | Returns a CODE reference which is invoked as the method of given object. | ||
| 378 | |||
| 379 | =item C<super_method> | ||
| 380 | |||
| 381 | $code = $poly->super_method( [] => 'values' ); | ||
| 382 | $code = $poly->super_method( qr{foo} => 'values' ); | ||
| 383 | $code = $poly->super_method( FileHandle->new => 'values' ); | ||
| 384 | $code = $poly->super_method( 'Any' => 'values' ); # always undef | ||
| 385 | |||
| 386 | Returns a CODE reference which is invoked as the super method of given object. | ||
| 387 | |||
| 388 | =cut | ||
| 389 | |||
| 390 | sub method { | ||
| 391 | my ( $self, $obj, $method ) = @_; | ||
| 392 | my $class = blessed( $obj ); | ||
| 393 | my $type = $self->type( $obj ); | ||
| 394 | ($class | ||
| 395 | ? ( $self->class_method( $class, $method ) or | ||
| 396 | $self->type_method( $type, $method ) or | ||
| 397 | ( $self->runs_native and UNIVERSAL::can( $obj , $method ) )) | ||
| 398 | : $self->type_method( $type, $method )); | ||
| 399 | } | ||
| 400 | |||
| 401 | sub _native_super { | ||
| 402 | |||
| 403 | my ( $class, $method ) = @_; | ||
| 404 | my $glob = do{ no strict 'refs'; \*{"$class::$method"} }; | ||
| 405 | my @isa = do{ no strict 'refs'; @{"${class}::ISA"} }; | ||
| 406 | |||
| 407 | if( *{$glob}{CODE} ){ | ||
| 408 | foreach my $parent ( @isa ){ | ||
| 409 | my $code = UNIVERSAL::can( $parent, $method ); | ||
| 410 | return $code if $code; | ||
| 411 | } | ||
| 412 | } | ||
| 413 | else { | ||
| 414 | foreach my $parent ( @isa ){ | ||
| 415 | my $code = _native_super( $parent, $method ); | ||
| 416 | return $code if $code; | ||
| 417 | } | ||
| 418 | } | ||
| 419 | } | ||
| 420 | |||
| 421 | sub super_method { | ||
| 422 | my ( $self, $obj, $method ) = @_; | ||
| 423 | my $class = blessed( $obj ); | ||
| 424 | my $type = $self->type( $obj ); | ||
| 425 | |||
| 426 | if ( $class ){ | ||
| 427 | my $uni = $self->class_method( UNIVERSAL => $method ); | ||
| 428 | if( $class eq 'UNIVERSAL' ) { | ||
| 429 | |||
| 430 | return $self->type_method( $type => $method ) if $uni; | ||
| 431 | |||
| 432 | } | ||
| 433 | else { | ||
| 434 | |||
| 435 | my $code = $self->super_class_method( $class, $method ); | ||
| 436 | return $code if $code; | ||
| 437 | |||
| 438 | if( $self->runs_native ) { | ||
| 439 | $code = _native_super( $class, $method ); | ||
| 440 | return $code if $code; | ||
| 441 | } | ||
| 442 | |||
| 443 | return $self->type_method( $type => $method ) if $uni; | ||
| 444 | } | ||
| 445 | } | ||
| 446 | |||
| 447 | $self->super_type_method( $type => $method ); | ||
| 448 | } | ||
| 449 | |||
| 450 | |||
| 451 | =item C<apply> | ||
| 452 | |||
| 453 | $poly->apply( $obj => 'method' => $arg1, $arg1 , $arg3 .... ); | ||
| 454 | |||
| 455 | Invokes a method which was defined. | ||
| 456 | |||
| 457 | =item C<super> | ||
| 458 | |||
| 459 | $poly->super( $obj => 'method' => $arg1, $arg1 , $arg3 .... ); | ||
| 460 | |||
| 461 | Invokes a super method which was defined.. | ||
| 462 | |||
| 463 | =back | ||
| 464 | |||
| 465 | =cut | ||
| 466 | |||
| 467 | |||
| 468 | sub apply { | ||
| 469 | my $self = shift; | ||
| 470 | my $obj = $_[0]; | ||
| 471 | my $method = splice @_, 1, 1; | ||
| 472 | goto ( $self->method( $obj => $method ) or | ||
| 473 | sub{ confess sprintf( 'method "%s" is not defined in %s', | ||
| 474 | $method, | ||
| 475 | $self->class($obj)) }); | ||
| 476 | } | ||
| 477 | |||
| 478 | sub super { | ||
| 479 | my $self = shift; | ||
| 480 | my $obj = $_[0]; | ||
| 481 | my $method = splice @_, 1, 1; | ||
| 482 | goto ( $self->super_method( $obj => $method ) or | ||
| 483 | sub{ confess sprintf( 'method "SUPER::%s" is not defined in %s', | ||
| 484 | $method, | ||
| 485 | $self->class($obj)) }); | ||
| 486 | } | ||
| 487 | |||
| 488 | 1; # End of Data::Polymorph | ||
| 489 | |||
| 490 | __END__ | ||