qbit/Packages.pm

filter: Statementstmt 90.4% 19 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.8% 26 / 28 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« qbit/Log.pm ↑ Summary qbit/StringUtils.pm »
1  
2 =head1 Name
3  
4 qbit::Packages - Functions to manipulate data in packages.
5  
6 =cut
7  
8 package qbit::Packages;
9 $qbit::Packages::VERSION = '2.7';
10 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use File::Find qw(find);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 8 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 8 ▶use qbit::StringUtils qw(fix_utf);
Subroutine BEGIN: called CC=1 SCAR=2.6
20 use qbit::Exceptions;
21 use qbit::GetText qw(gettext);
22  
23 BEGIN {
24 our (@EXPORT, @EXPORT_OK);
25  
26 @EXPORT = qw(
27 dynamic_loading
28 package_merge_isa_data
29 package_stash
30 package_sym_table
31 require_class
32 );
33 @EXPORT_OK = @EXPORT;
34 }
35  
36 =head1 Functions
37  
38 =head2 package_sym_table
39  
40 B<Arguments:>
41  
42 =over
43  
44 =item
45  
46 B<$package> - string, package name.
47  
48 =back
49  
50 B<Return value:> hash ref, all package's symbols.
51  
52 =cut
53  
54 sub package_sym_table($) {
55 my ($package) = @_;
56  
57 no strict 'refs';
58 return \%{$package . '::'};
59 }
60  
61 =head2 package_stash
62  
63 B<Arguments:>
64  
65 =over
66  
67 =item
68  
69 B<$package> - string, package name.
70  
71 =back
72  
73 B<Return value:> hash ref, package stash.
74  
75 =cut
76  
77 sub package_stash($) {
78 my ($package) = @_;
79  
80 no strict 'refs';
81 *{$package . '::QBitStash'} = {} unless *{$package . '::QBitStash'};
82 return \%{$package . '::QBitStash'};
83 }
84  
85 =head2 package_merge_isa_data
86  
87 B<Arguments:>
88  
89 =over
90  
91 =item
92  
93 B<$package> - string, package name;
94  
95 =item
96  
97 B<$res> - scalar, result's stash;
98  
99 =item
100  
101 B<$func> - code, function to merge. Arguments:
102  
103 =over
104  
105 =item
106  
107 B<$package> - string, package name;
108  
109 =item
110  
111 B<$res> - scalar, result's stash;
112  
113 =back
114  
115 =item
116  
117 B<$baseclass> - string, upper level package name.
118  
119 =back
120  
121 Recursive merge data into $res from all levels packages hierarchy.
122  
123 =cut
124  
125 sub package_merge_isa_data {
126 my ($package, $res, $func, $baseclass) = @_;
127  
128 my $isa;
129 {
130 no strict 'refs';
131 $isa = \@{$package . '::ISA'};
132 }
133 foreach my $pkg (@$isa) {
134 next if defined($baseclass) && !$pkg->isa($baseclass);
135 package_merge_isa_data($pkg, $res, $func, $baseclass);
136 }
137  
138 $func->($package, $res);
139 }
140  
141 =head2 require_class
142  
143 B<Arguments:>
144  
145 =over
146  
147 =item
148  
149 B<$class> - string, class name.
150  
151 =back
152  
153 Convert class name to .pm file path and require it.
154  
155 B<Return value:> return value of CORE::require if all is Ok or throw Exception if cannot load .pm file.
156  
157 =cut
158  
159 sub require_class {
160 my ($class) = @_;
161  
162 my $file_name = "$class.pm";
163 $file_name =~ s/::/\//g;
164  
165 my $result;
166 try {
167 $result = require($file_name);
168 }
169 catch {
170 throw $_[0];
171 };
172  
173 return $result || throw Exception gettext('Cannot requre class "%s": %s', $class, fix_utf($@ || $!));
174 }
175  
176 =head2 dynamic_loading
177  
178 B<Arguments:>
179  
180 =over
181  
182 =item
183  
184 B<$package_prefix> - string.
185  
186 =back
187  
188 Dynamic loading all packages from directory $package_prefix.
189  
190 B<Example:>
191  
192 dynamic_loading('QBit::Application::Model::DBManager::Filter');
193  
194 =cut
195  
196 sub dynamic_loading {
197 my ($pakage_prefix) = @_;
198  
199 my $stash = package_stash(__PACKAGE__);
200  
201 unless ($stash->{$pakage_prefix}) {
202 my $dir = File::Spec->catdir(split(/::/, $pakage_prefix));
203  
204 my @dirs = map {File::Spec->catdir($_, $dir)} @INC;
205  
206 my %package_names = ();
207 foreach my $basedir (@dirs) {
208 next unless -d $basedir;
209  
210 find(
211 {
212 wanted => sub {
213 my $name = File::Spec->abs2rel($_, $basedir);
214  
215 return unless $name && $name ne File::Spec->curdir();
216  
217 return unless /\.pm$/ && -r;
218  
219 $name =~ s/\.pm$//;
220 $name = join('::', File::Spec->splitdir($name));
221  
222 $package_names{$name} = 1;
223 },
224 no_chdir => 1,
225 follow => 1
226 },
227 $basedir
228 );
229 }
230  
231 my @packages = map "$pakage_prefix\::$_", keys(%package_names);
232  
233 foreach my $package (sort @packages) {
234 require_class($package);
235 }
236  
237 $stash->{$pakage_prefix} = 1;
238 }
239 }
240  
241 1;
« qbit/Log.pm ↑ Summary qbit/StringUtils.pm »