File Coverage

/tmp/a_XHUBVCaa/lib/My/Prefix/HashBase.pm
Criterion Covered Total %
statement 100 160 62.5
branch 27 70 38.5
condition 9 21 42.8
subroutine 13 24 54.1
pod 1 4 25.0
total 150 279 53.7


line stmt bran cond sub pod time code
1             package My::Prefix::HashBase;
2 1     1   1944 use strict;
  1         2  
  1         50  
3 1     1   6 use warnings;
  1         34  
  1         115  
4              
5             our $VERSION = '123';
6              
7             #################################################################
8             # #
9             # This is a generated file! Do not modify this file directly! #
10             # Use hashbase_inc.pl script to regenerate this file. #
11             # The script is part of the Object::HashBase distribution. #
12             # Note: You can modify the version number above this comment #
13             # if needed, that is fine. #
14             # #
15             #################################################################
16              
17             {
18 1     1   8 no warnings 'once';
  1         2  
  1         127  
19             $My::Prefix::HashBase::HB_VERSION = '0.015';
20             *My::Prefix::HashBase::ATTR_SUBS = \%Object::HashBase::ATTR_SUBS;
21             *My::Prefix::HashBase::ATTR_LIST = \%Object::HashBase::ATTR_LIST;
22             *My::Prefix::HashBase::VERSION = \%Object::HashBase::VERSION;
23             *My::Prefix::HashBase::CAN_CACHE = \%Object::HashBase::CAN_CACHE;
24             }
25              
26              
27             require Carp;
28             {
29 1     1   7 no warnings 'once';
  1         26  
  1         260  
30             $Carp::Internal{+__PACKAGE__} = 1;
31             }
32              
33             BEGIN {
34             {
35             # Make sure none of these get messed up.
36 1     1   4 local ($SIG{__DIE__}, $@, $?, $!, $^E);
  1         10  
37 1 50       3 if (eval { require Class::XSAccessor; Class::XSAccessor->VERSION(1.19); 1 }) {
  1         7  
  1         39  
  1         9  
38             *CLASS_XS_ACCESSOR = sub() { 1 }
39 1         8 }
40             else {
41             *CLASS_XS_ACCESSOR = sub() { 0 }
42 0         0 }
43             }
44              
45             # these are not strictly equivalent, but for out use we don't care
46             # about order
47             *_isa = ($] >= 5.010 && require mro) ? \&mro::get_linear_isa : sub {
48 1     1   8 no strict 'refs';
  1         3  
  1         209  
49 0         0 my @packages = ($_[0]);
50 0         0 my %seen;
51 0         0 for my $package (@packages) {
52 0         0 push @packages, grep !$seen{$_}++, @{"$package\::ISA"};
  0         0  
53             }
54 0         0 return \@packages;
55             }
56 1 50 33     555 }
57              
58             my %SPEC = (
59             '^' => {reader => 1, writer => 0, dep_writer => 1, read_only => 0, strip => 1},
60             '-' => {reader => 1, writer => 0, dep_writer => 0, read_only => 1, strip => 1},
61             '>' => {reader => 0, writer => 1, dep_writer => 0, read_only => 0, strip => 1},
62             '<' => {reader => 1, writer => 0, dep_writer => 0, read_only => 0, strip => 1},
63             '+' => {reader => 0, writer => 0, dep_writer => 0, read_only => 0, strip => 1},
64             '~' => {reader => 1, writer => 1, dep_writer => 0, read_only => 0, strip => 1, no_xs => 1},
65             );
66              
67 2     2 0 15 sub spec { \%SPEC }
68              
69             sub import {
70 2     2   4326 my $class = shift;
71 2         5 my $into = caller;
72 2         9 $class->do_import($into, @_);
73             }
74              
75             sub do_import {
76 2     2 0 3 my $class = shift;
77 2         4 my $into = shift;
78              
79             # Make sure we list the OLDEST version used to create this class.
80 2   33     7 my $ver = $My::Prefix::HashBase::HB_VERSION || $My::Prefix::HashBase::VERSION;
81 2 50 33     18 $My::Prefix::HashBase::VERSION{$into} = $ver if !$My::Prefix::HashBase::VERSION{$into} || $My::Prefix::HashBase::VERSION{$into} > $ver;
82              
83 2         16 my $isa = _isa($into);
84 2   50     12 my $attr_list = $My::Prefix::HashBase::ATTR_LIST{$into} ||= [];
85 2   50     9 my $attr_subs = $My::Prefix::HashBase::ATTR_SUBS{$into} ||= {};
86              
87 2         64 my @pre_init;
88             my @post_init;
89              
90 2         6 my $add_new = 1;
91              
92 2 100       23 if (my $have_new = $into->can('new')) {
93 1   50     4 my $new_lookup = $My::Prefix::HashBase::NEW_LOOKUP //= {};
94 1 50       5 $add_new = 0 unless $new_lookup->{$have_new};
95             }
96              
97             my %subs = (
98             ($add_new ? ($class->_build_new($into, \@pre_init, \@post_init)) : ()),
99 2 50       10 (map %{$My::Prefix::HashBase::ATTR_SUBS{$_} || {}}, @{$isa}[1 .. $#$isa]),
  1 50       7  
  2         9  
100             ($class->args_to_subs($attr_list, $attr_subs, \@_, $into)),
101             );
102              
103 1     1   9 no strict 'refs';
  1         2  
  1         2020  
104 2         12 while (my ($k, $v) = each %subs) {
105 17 100       39 if (ref($v) eq 'CODE') {
106 13         15 *{"$into\::$k"} = $v;
  13         117  
107             }
108             else {
109 4         13 my ($sub, @args) = @$v;
110 4         40 $sub->(@args);
111             }
112             }
113             }
114              
115             sub args_to_subs {
116 2     2 0 5 my $class = shift;
117 2         4 my ($attr_list, $attr_subs, $args, $into) = @_;
118              
119 2         17 my $use_gen = $class->can('gen_accessor') ;
120              
121 2         4 my %out;
122              
123 2         6 while (@$args) {
124 2         4 my $x = shift @$args;
125 2         6 my $p = substr($x, 0, 1);
126              
127 2   50     18 my $spec = $class->spec->{$p} || {reader => 1, writer => 1};
128 2 50       58 substr($x, 0, 1) = '' if $spec->{strip};
129              
130 2         9 push @$attr_list => $x;
131 2         6 my ($sub, $attr) = (uc $x, $x);
132              
133 2     0   22 $attr_subs->{$sub} = sub() { $attr };
  0         0  
134 2         5 $out{$sub} = $attr_subs->{$sub};
135              
136 2         3 my $copy = "$attr";
137 2 50       8 if ($spec->{reader}) {
138 2 50       6 if ($use_gen) {
    50          
139 0         0 $out{$attr} = $class->gen_accessor(reader => $copy, $spec, $args);
140             }
141             elsif (CLASS_XS_ACCESSOR && !$spec->{no_xs}) {
142 2         10 $out{$attr} = [\&Class::XSAccessor::newxs_getter, "$into\::$attr", $copy];
143             }
144             else {
145 0     0   0 $out{$attr} = sub { $_[0]->{$attr} };
  0         0  
146             }
147             }
148              
149 2 50       8 if ($spec->{writer}) {
    0          
    0          
150 2 50       6 if ($use_gen) {
    50          
151 0         0 $out{"set_$attr"} = $class->gen_accessor(writer => $copy, $spec, $args);
152             }
153             elsif(CLASS_XS_ACCESSOR && !$spec->{no_xs}) {
154 2         9 $out{"set_$attr"} = [\&Class::XSAccessor::newxs_setter, "$into\::set_$attr", $copy, 0];
155             }
156             else {
157 0     0   0 $out{"set_$attr"} = sub { $_[0]->{$attr} = $_[1] };
  0         0  
158             }
159             }
160             elsif($spec->{read_only}) {
161 0 0   0   0 $out{"set_$attr"} = $use_gen ? $class->gen_accessor(read_only => $copy, $spec, $args) : sub { Carp::croak("'$attr' is read-only") };
  0         0  
162             }
163             elsif($spec->{dep_writer}) {
164 0 0   0   0 $out{"set_$attr"} = $use_gen ? $class->gen_accessor(dep_writer => $copy, $spec, $args) : sub { Carp::carp("set_$attr() is deprecated"); $_[0]->{$attr} = $_[1] };
  0         0  
  0         0  
165             }
166              
167 2 50       10 if ($spec->{custom}) {
168 0         0 my %add = $class->gen_accessor(custom => $copy, $spec, $args);
169 0         0 $out{$_} = $add{$_} for keys %add;
170             }
171             }
172              
173 2         45 return %out;
174             }
175              
176             sub attr_list {
177 0     0 1 0 my $class = shift;
178              
179 0         0 my $isa = _isa($class);
180              
181 0         0 my %seen;
182 0         0 my @list = grep { !$seen{$_}++ } map {
183 0         0 my @out;
  0         0  
184              
185 0 0 0     0 if (0.004 > ($My::Prefix::HashBase::VERSION{$_} || 0)) {
186 0         0 Carp::carp("$_ uses an inlined version of My::Prefix::HashBase too old to support attr_list()");
187             }
188             else {
189 0         0 my $list = $My::Prefix::HashBase::ATTR_LIST{$_};
190 0 0       0 @out = $list ? @$list : ()
191             }
192              
193 0         0 @out;
194             } reverse @$isa;
195              
196 0         0 return @list;
197             }
198              
199             sub _build_new {
200 2     2   5 my $class = shift;
201 2         5 my ($into, $pre_init, $post_init) = @_;
202              
203 2     0   11 my $add_pre_init = sub(&) { push @$pre_init => $_[-1] };
  0         0  
204 2     0   9 my $add_post_init = sub(&) { push @$post_init => $_[-1] };
  0         0  
205              
206 2         10 my $__pre_init = $into->can('_pre_init');
207 2 100   0   10 my $_pre_init = $__pre_init ? sub { ($__pre_init->(), @$pre_init) } : sub { @$pre_init };
  0         0  
  0         0  
208              
209 2         12 my $__post_init = $into->can('_post_init');
210 2 100   0   7 my $_post_init = $__post_init ? sub { ($__post_init->(), @$post_init) } : sub { @$post_init };
  0         0  
  0         0  
211              
212             my $new = sub {
213 0     0   0 my $class = shift;
214              
215 0         0 my $self;
216              
217 0 0       0 if (@_ == 1) {
218 0         0 my $arg = shift;
219 0         0 my $type = ref($arg);
220              
221 0 0       0 if ($type eq 'HASH') {
222 0         0 $self = bless({%$arg}, $class);
223             }
224             else {
225 0 0       0 Carp::croak("Not sure what to do with '$type' in $class constructor")
226             unless $type eq 'ARRAY';
227              
228 0         0 my %proto;
229 0         0 my @attributes = attr_list($class);
230 0         0 while (@$arg) {
231 0         0 my $val = shift @$arg;
232 0 0       0 my $key = shift @attributes or Carp::croak("Too many arguments for $class constructor");
233 0         0 $proto{$key} = $val;
234             }
235              
236 0         0 $self = bless(\%proto, $class);
237             }
238             }
239             else {
240 0         0 $self = bless({@_}, $class);
241             }
242              
243             $My::Prefix::HashBase::CAN_CACHE{$class} = $self->can('init')
244 0 0       0 unless exists $My::Prefix::HashBase::CAN_CACHE{$class};
245              
246 0         0 $self->$_() for $_pre_init->();
247 0 0       0 $self->init() if $My::Prefix::HashBase::CAN_CACHE{$class};
248 0         0 $self->$_() for reverse $_post_init->();
249              
250 0         0 $self;
251 2         9 };
252              
253 2   100     10 my $new_lookup = $My::Prefix::HashBase::NEW_LOOKUP //= {};
254 2         6 $new_lookup->{$new} = 1;
255              
256 2         3 my %out;
257              
258             {
259 1     1   10 no strict 'refs';
  1         2  
  1         314  
  2         3  
260 2 50       3 $out{new} = $new unless defined(&{"${into}\::new"});
  2         10  
261 2 50       5 $out{add_pre_init} = $add_pre_init unless defined(&{"${into}\::add_pre_init"});
  2         12  
262 2 50       3 $out{add_post_init} = $add_post_init unless defined(&{"${into}\::add_post_init"});
  2         8  
263 2 50       4 $out{_pre_init} = $_pre_init unless defined(&{"${into}\::_pre_init"});
  2         5  
264 2 50       3 $out{_post_init} = $_post_init unless defined(&{"${into}\::_post_init"});
  2         9  
265             }
266              
267 2         42 return %out;
268             }
269              
270             1;
271              
272             __END__