File Coverage

blib/lib/Switch/Perlish/Smatch/Object.pm
Criterion Covered Total %
statement 27 27 100.0
branch 1 2 50.0
condition 2 3 66.6
subroutine 13 13 100.0
pod 0 1 0.0
total 43 46 93.4


line stmt bran cond sub pod time code
1             package Switch::Perlish::Smatch::Object;
2              
3             $VERSION = '1.0.0';
4              
5 11     11   61 use strict;
  11         21  
  11         361  
6 11     11   61 use warnings;
  11         23  
  11         289  
7              
8 11     11   60 use Carp 'croak';
  11         24  
  11         1101  
9 11     11   76 use Scalar::Util 'reftype';
  11         17  
  11         7469  
10              
11             ## DESC - Check if $t has $m as a method.
12             sub _VALUE {
13 2     2   5 my($t, $m) = @_;
14 2         28 return $t->can($m);
15             }
16              
17             ## DESC - croak("Can't compare OBJECT with an undef") # Suggestions welcome.
18             sub _UNDEF {
19 1     1   239 croak("Can't compare OBJECT with an undef");
20             }
21              
22             ## DESC - Check if the $m points to the $t.
23             sub _SCALAR {
24 2     2   5 my($t, $m) = @_;
25 2         22 return $t == $$m;
26             }
27              
28             ## Just delegate back to the blessed type - This is a quite horrible
29             ## way to compare because it breaks encapsulation, but these are default cmps.
30             sub do_delegation {
31 4     4 0 7 my($t, $m, $type) = @_;
32 4 50       29 return ( reftype($t) eq $type ?
33             Switch::Perlish::Smatch->dispatch($type => $type => $t, $m)
34             :
35             () );
36             }
37              
38             ## DESC - If the $t is a blessed ARRAY, delegate to the C<< ARRAY<=>ARRAY >> comparator.
39 2     2   6 sub _ARRAY { do_delegation @_, 'ARRAY' }
40              
41             ## DESC - If the $t is a blessed HASH, delegate to the C<< HASH<=>HASH >> comparator.
42 2     2   6 sub _HASH { do_delegation @_, 'HASH' }
43              
44             ## DESC - Call the $t on &$m i.e C<< $t->$m >>.
45             sub _CODE {
46 2     2   5 my($t, $m) = @_;
47 2         8 return $t->$m;
48             }
49              
50             ## DESC - Check if the $t->isa($m) or the same class (better suggestions welcome).
51             sub _OBJECT {
52 2     2   6 my($t, $m) = @_;
53 2   66     51 return( ref($t) eq ref($m) or $t->isa($m) );
54             }
55              
56             ## DESC - Match the class of $t against the $m.
57             sub _Regexp {
58 2     2   3 my($t, $m) = @_;
59 2         18 return ref($t) =~ /$m/;
60             }
61              
62             Switch::Perlish::Smatch->register_package( __PACKAGE__, 'OBJECT' );
63              
64             1;
65              
66             =pod
67              
68             =head1 NAME
69              
70             Switch::Perlish::Smatch::Object - The C comparatory category package.
71              
72             =head1 VERSION
73              
74             1.0.0 - Initial release.
75              
76             =head1 DESCRIPTION
77              
78             This package provides the default implementation for the C comparator
79             category. For more information on the comparator implementation see.
80             L.
81              
82             =head1 SEE. ALSO
83              
84             L
85              
86             L
87              
88             =head1 AUTHOR
89              
90             Dan Brook C<< >>
91              
92             =head1 COPYRIGHT
93              
94             Copyright (c) 2006, Dan Brook. All Rights Reserved. This module is free
95             software. It may be used, redistributed and/or modified under the same
96             terms as Perl itself.
97              
98             =cut