blib/lib/Package/Strictures.pm untested

filter: Statementstmt 0.0% 0 / 72 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 110 SCAR 47.0
CC
10
Cov
0%
import
30.0
_setup_from
12.0
_setup_for
6.0
CRAP 110.0
1 use 5.008; # 8 = utf8, 6 = pragmas, our, 5 = qr, 4 = __PACAKGE__,for my
2 use strict;
3 use warnings;
4 use utf8;
5  
6 package Package::Strictures;
7 $Package::Strictures::VERSION = '1.000000';
8 # ABSTRACT: Facilitate toggling validation code at users request, without extra performance penalties.
9  
10 our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
11  
12 use Package::Strictures::Registry;
13 use Carp ();
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69 sub import {
70 my ( $self, %params ) = @_;
71 if ( not %params ) {
72 Carp::carp( __PACKAGE__ . ' called with no parameters, skipping magic' );
73 return;
74 }
75 if ( ( not exists $params{-for} ) && ( not exists $params{-from} ) ) {
76 Carp::croak( 'no -for or -from parameter to ' . __PACKAGE__ );
77 }
78 if ( exists $params{-for} ) {
79 $self->_setup_for( $params{-for} );
80 return;
81 }
82 if ( exists $params{-from} ) {
83 $self->_setup_from( $params{-from} );
84 return;
85 }
86 Carp::croak('Ugh!?');
87 }
88  
89 sub _setup_for {
90 my ( $self, $params ) = @_;
91 my $reftype = ref $params;
92 if ( $reftype ne 'HASH' ) {
93 Carp::croak("-for presently only takes HASH, got `$reftype`");
94 }
95 for my $package ( keys %{$params} ) {
96 $self->_setup_for_package( $params->{$package}, $package );
97 }
98 return;
99 }
100  
101 sub _setup_from {
102 my ( $self, $from ) = @_;
103 my $reftype = ref $from;
104 if ($reftype) {
105 Carp::croak("-from can only take a scalar, not a ref, got `$reftype`");
106 }
107 ## no critic (RegularExpressions::RequireDotMatchAnything RegularExpressions::RequireLineBoundaryMatching)
108 if ( $from =~ /[.]ini$/x ) {
109 $self->_setup_from_ini($from);
110 return;
111 }
112 Carp::croak('Only know how to load setup from .ini files at present');
113 return;
114 }
115  
116 sub _setup_from_ini {
117 my ( $self, $from ) = @_;
118 require Config::INI::Reader;
119 my ($conf) = Config::INI::Reader->read_file($from);
120 if ( exists $conf->{'_'} ) {
121 Carp::carp("Strictures not addressed at a package found in `$from`");
122 }
123 $self->_setup_for($conf);
124 return;
125 }
126  
127 sub _setup_for_package {
128 my ( undef, $params, $package ) = @_;
129 my $reftype = ref $params;
130 if ( $reftype ne 'HASH' ) {
131 my $mesg = q[];
132 $mesg .= q[-for => { Some::Name => X } presently only takes HASH,];
133 $mesg .= q[got `%s` on package `%s` ];
134 Carp::croak( sprintf $mesg, $reftype, $package );
135 }
136 for my $value ( keys %{$params} ) {
137 Package::Strictures::Registry->set_value( $package, $value, $params->{$value} );
138 }
139 return;
140 }
141  
142 1;
143  
144 __END__