blib/lib/perl5i/2/Meta.pm untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 170 CC 14 SCAR 29.9
CC
14
Cov
0%
methods
56.0
super
30.0
linear_isa
12.0
CRAP 19.9
1 package perl5i::2::Meta;
2  
3 use strict;
4 use warnings;
5 use 5.010_000;
6  
7 # Be very careful not to import anything.
8 require Carp::Fix::1_25;
9 require mro;
10  
11 require perl5i::2::Meta::Instance;
12 require perl5i::2::Meta::Class;
13 use perl5i::2::autobox;
14  
15 sub UNIVERSAL::mo {
16 # Be careful to pass through an alias, not a copy
17 return perl5i::2::Meta::Instance->new($_[0]);
18 }
19  
20 sub UNIVERSAL::mc {
21 return perl5i::2::Meta::Class->new($_[0]);
22 }
23  
24 sub new {
25 my $class = shift;
26 # Be careful to take a reference to an alias, not a copy
27 return bless \\$_[0], $class;
28 }
29  
30 sub ISA {
31 my $class = $_[0]->class;
32  
33 no strict 'refs';
34 return wantarray ? @{$class.'::ISA'} : \@{$class.'::ISA'};
35 }
36  
37 sub linear_isa {
38 my $self = shift;
39 my $class = $self->class;
40  
41 # get_linear_isa() does not return UNIVERSAL
42 my @extra;
43 @extra = qw(UNIVERSAL) unless $class eq 'UNIVERSAL';
44  
45 my $isa = [@{mro::get_linear_isa($class)}, @extra];
46 return wantarray ? @$isa : $isa;
47 }
48  
49 sub methods {
50 my $self = shift;
51 my $opts = shift // {};
52 my $top = $self->class;
53  
54 my %exclude;
55  
56 state $defaults = {
57 with_UNIVERSAL => 0,
58 just_mine => 0,
59 };
60  
61 $opts = { %$defaults, %$opts };
62 $exclude{UNIVERSAL} = !$opts->{with_UNIVERSAL};
63  
64 my @classes = $opts->{just_mine} ? $self->class : $self->linear_isa;
65  
66 my %all_methods;
67 for my $class (@classes) {
68 next if $exclude{$class} && $class ne $top;
69  
70 my $sym_table = $class->mc->symbol_table;
71 for my $name (keys %$sym_table) {
72 my $glob = $sym_table->{$name};
73 next unless ref \$glob eq "GLOB";
74 next unless my $code = *{$glob}{CODE};
75 my $sig = $code->signature;
76 next if $sig and !$sig->is_method;
77 $all_methods{$name} = $class;
78 }
79 }
80  
81 return wantarray ? keys %all_methods : [keys %all_methods];
82 }
83  
84 sub symbol_table {
85 my $self = shift;
86 my $class = $self->class;
87  
88 no strict 'refs';
89 return \%{$class.'::'};
90 }
91  
92 # A single place to put the "method not found" error.
93 my $method_not_found = sub {
94 my $class = shift;
95 my $method = shift;
96  
97 Carp::Fix::1_25::croak(
98 sprintf q[Can't locate object method "%s" via package "%s"],
99 $method, $class
100 );
101 };
102  
103  
104 # caller() will return if its inside an eval, need to skip over those.
105 my $find_method = sub {
106 my $method;
107 my $height = 2;
108 do {
109 $method = (caller($height))[3];
110 $height++;
111 } until( !defined $method or $method ne '(eval)' );
112  
113 return $method;
114 };
115  
116  
117 sub super {
118 my $self = shift;
119 my $class = $self->class;
120  
121 my $fq_method = $find_method->();
122 Carp::Fix::1_25::croak("super() called outside a method") unless $fq_method;
123  
124 my($parent, $method) = $fq_method =~ /^(.*)::(\w+)$/;
125  
126 Carp::Fix::1_25::croak(
127 sprintf qq["%s" is not a parent class of "%s"],
128 $parent, $class
129 ) unless $class->isa($parent);
130  
131 my @isa = $self->linear_isa();
132  
133 while(@isa) {
134 my $class = shift @isa;
135 last if $class eq $parent;
136 }
137  
138 for (@isa) {
139 my $code = $_->can($method);
140 @_ = ($$$self, @_);
141 goto &$code if $code;
142 }
143  
144 $class->$method_not_found($method);
145 }
146  
147 1;