File Coverage

blib/lib/Heritable/Types.pm
Criterion Covered Total %
statement 30 31 96.7
branch 6 6 100.0
condition 2 3 66.6
subroutine 7 8 87.5
pod 0 1 0.0
total 45 49 91.8


line stmt bran cond sub pod time code
1             package Heritable::Types;
2 2     2   50514 no strict; # !!!
  2         6  
  2         74  
3              
4 2     2   10 use Scalar::Util;
  2         4  
  2         151  
5 2     2   11 use Carp;
  2         7  
  2         145  
6              
7 2     2   9 use vars qw/*CORE::GLOBAL::bless $VERSION/;
  2         2  
  2         579  
8              
9             $VERSION = '1.00';
10              
11 2     2   166 BEGIN { @{"${_}::ISA"} = 'Object' for
  12         798  
12             qw/HASH ARRAY SCALAR CODE IO GLOB/ }
13              
14             sub bless
15 14     14 0 56454 { my($thing, $class) = @_;
16 14   66     49 $class ||= caller;
17 14         57 my $type = Scalar::Util::reftype($thing);
18              
19 14 100       91 push @{"$class\::ISA"}, $type unless UNIVERSAL::isa($class, $type);
  12         247  
20 14         168 CORE::bless $thing, $class };
21              
22             *CORE::GLOBAL::bless = \&bless;
23              
24 0     0   0 sub UNIVERSAL::DESTROY { }
25              
26             sub UNIVERSAL::AUTOLOAD
27 2     2   1739 { my($thing, @args) = @_;
28 2         7 my $type = Scalar::Util::reftype($thing);
29 2         4 my $class = ref($thing);
30 2         2 my $method = $UNIVERSAL::AUTOLOAD;
31 2         10 $method =~ s/.*:://;
32              
33 2 100       17 &bless($thing, $class) unless $class->isa($type); # Teehee
34              
35 2         23 $method = $class->can($method);
36 2 100       5 if ($method)
37 1         5 { goto &$method }
38             else
39 1         19 { croak qq{Can't locate object method "$method" via package $class} } }
40              
41             =head1 NAME
42              
43             Heritable::Types - Make object dispatch look at an object's type
44              
45             =head1 SYNOPSIS
46              
47             use Heritable::Types
48              
49             sub Object::as_string
50             { my($self) = @_;
51             join " ", 'a', ref($self), $self->content_string; }
52              
53             sub HASH::content_string
54             { my($self) = @_;
55             my $str = join ', ', map {"$_ => $self->{$_}", keys %$self;
56             return "{ $str }" }
57              
58             sub ARRAY::content_string
59             { my($self) = @_;
60             return '[ ', join(', ', @$self), ' ]' }
61              
62             =head1 DESCRIPTION
63              
64             Heritable::Types sets about making Perl's method dispatch system
65             consistent with the way C works. Right now, if you have an object
66             which you represent as, say, a blessed Hash, then, according to
67             C, that object is a HASH. But if you implement, say
68             C, a method that only exists in the HASH namespace, then
69             C will not see it, nor will it get called if you do C<<
70             $obj->foo >>. This strikes me as an unsatisfactory state of affairs,
71             hence Heritable::Types.
72              
73             =head1 USAGE
74              
75             There's nothing to it, see the synopsis for how it works. Note that,
76             if once one module uses Heritable::Types then *all* objects will do
77             method lookup via their types.
78              
79             If you want to have a method which all types can inherit from, but
80             which will ensure that individual types can override that method, then
81             you should implement it in the Object class, rather than in UNIVERSAL
82             (if you implement a method in UNIVERSAL there's a good chance that the
83             specific type's methods will never get called, which isn't what anyone
84             wants.
85              
86             =head1 BUGS
87              
88             None sighted so far. There are bound to be some though.
89              
90             =head1 SUPPORT
91              
92             What support there is for this module is provided on a "When the
93             author has time" basis. If you do have problems with it, please, drop
94             me a line. Support requests that come with a failing test are
95             I appreciated. Bug reports that come with a new test and a
96             patch to fix it will earn my undying gratitude.
97              
98             =head1 AUTHOR
99              
100             Piers Cawley
101             CPAN ID: PDCAWLEY
102             pdcawley@bofh.org.uk
103             http://pc1.bofhadsl.ftech.co.uk:8080/
104              
105             =head1 COPYRIGHT
106              
107             This program is free software; you can redistribute
108             it and/or modify it under the same terms as Perl itself.
109              
110             The full text of the license can be found in the
111             LICENSE file included with this module.
112              
113              
114             =head1 SEE ALSO
115              
116             perl(1).
117              
118             =cut
119              
120             ############################################# main pod documentation end ##
121              
122              
123             1; #this line is important and will help the module return a true value
124             __END__