Quiq/PerlModule.pm

filter: Statementstmt 67.8% 40 / 59 Branchbran 56.2% 9 / 16 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 85.7% 12 / 14 Podpod 100% 9 / 9 totaltotal 69.3% 70 / 101 CC 12 SCAR 19.8
CC
12
Cov
64%
isCore
17.6
find
12.0
isPragma
2.0
CRAP 7.2
« Quiq/Record.pm ↑ Summary Quiq/Sftp/Client.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::PerlModule - Perl-Modul
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 DESCRIPTION
14  
15 Ein Objekt der Klasse repräsentiert ein Perl-Modul im Dateisystem.
16 Hinsichtlich seiner Installation.
17  
18 =cut
19  
20 # -----------------------------------------------------------------------------
21  
22 package Quiq::PerlModule;
23 3 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 3 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 3 our $VERSION = '1.240';
30  
31 # -----------------------------------------------------------------------------
32  
33 =head1 METHODS
34  
35 =head2 Konstruktor
36  
37 =head3 new() - Instantiiere Objekt
38  
39 =head4 Synopsis
40  
41 $mod = $class->new($name);
42  
43 =head4 Description
44  
45 Instantiiere Objekt für Perl-Modul $name und liefere eine Referenz
46 auf dieses Objekt zurück.
47  
48 =cut
49  
50 # -----------------------------------------------------------------------------
51  
52 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
53 4 my ($class,$name) = @_;
54 4 return bless \$name,$class;
55 }
56  
57 # -----------------------------------------------------------------------------
58  
59 =head2 Accessors
60  
61 =head3 name() - Liefere Name des Moduls
62  
63 =head4 Synopsis
64  
65 $name = $mod->name;
66  
67 =head4 Example
68  
69 A::B::C
70  
71 =cut
72  
73 # -----------------------------------------------------------------------------
74  
75 3 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
76 3 my $self = shift;
77 3 return $$self;
78 }
79  
80 # -----------------------------------------------------------------------------
81  
82 =head2 Methods
83  
84 =head3 isCore() - Teste, ob Core-Modul
85  
86 =head4 Synopsis
87  
88 $bool = $mod->isCore;
89  
90 =head4 Description
91  
92 Liefere "wahr", wenn das Modul ein Core-Modul ist, andernfalls falsch.
93  
94 Ein Perl-Modul ist ein Core-Modul, wenn es bei den Perl-Quellen
95 dabei ist, also mit dem Perl-Interpreter zusammen installiert wird.
96  
97 =cut
98  
99 # -----------------------------------------------------------------------------
100  
101 3 ▶sub isCore {
Subroutine isCore: called CC=7 SCAR=28.7
Pod documented
102 3 my $self = shift;
103  
104 3 ▶ if ($] >= 5.010) { # 5.10 und neuer
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.01") 3 0 3
105 3 require Module::CoreList;
106 3 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
107 3 ▶ return exists $Module::CoreList::version{$]+0}{$$self}? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $Module::CoreList::version{$] + 0}{$$self} ? : 2 1 3
108 }
109  
110 0 require Config;
111 0 ▶ my $corePath = $Config::Config{'installprivlib'} || die;
Condition0%0/2
$Config::Config{'installprivlib'} || die()
l!l
00
MC/DC0%0/1
$Config::Config{'installprivlib'} || die()
$Config::Config{'installprivlib'}
Truth table0%0/2
A
A= $Config::Config{'installprivlib'}
Aresult
11
00
112  
113 0 my $modPath = $self->nameToPath;
114 0 for my $incPath (@INC) {
115 0 my $file = "$incPath/$modPath";
116 0 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 0 0 0
117 0 ▶ return $file =~ /^\Q$corePath/? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
$file =~ /^\Q$corePath\E/ ? : 0 0 0
118 }
119 }
120 0 return 0;
121 }
122  
123 # -----------------------------------------------------------------------------
124  
125 =head3 isPragma() - Teste, ob Pragma
126  
127 =head4 Synopsis
128  
129 $bool = $mod->isPragma;
130  
131 =head4 Description
132  
133 Liefere "wahr", wenn das Modul ein Pragma ist, andernfalls falsch.
134  
135 Ein Perl-Modul ist ein Pragma, wenn sein Name keine
136 Großbuchstaben enthält.
137  
138 =cut
139  
140 # -----------------------------------------------------------------------------
141  
142 3 ▶sub isPragma {
Subroutine isPragma: called CC=2 SCAR=6.9
Pod documented
143 3 my $self = shift;
144 3 ▶ return $$self !~ /[[:upper:]]/? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
!($$self =~ /[[:upper:]]/) ? : 1 2 3
145 }
146  
147 # -----------------------------------------------------------------------------
148  
149 =head3 find() - Suche Modul im Dateisystem
150  
151 =head4 Synopsis
152  
153 $path = $mod->find;
154  
155 =head4 Description
156  
157 Liefere den Pfad, unter dem das Modul geladen würde (mit use
158 oder require).
159  
160 =head4 Example
161  
162 use A::B::C;
163 print Quiq::PerlModule->new('A::B::C')->find;
164 # '/usr/lib/perl5/site_perl/5.10.0/A/B/C.pm'
165  
166 =cut
167  
168 # -----------------------------------------------------------------------------
169  
170 0 ▶sub find {
Subroutine find: not called CC=3 SCAR=24.8
Pod documented
171 0 my $self = shift;
172  
173 0 my $modulePath = $self->nameToPath;
174 0 for my $incPath (@INC) {
175 0 my $path = "$incPath/$modulePath";
176 0 ▶ if (-f $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $path) 0 0 0
177 0 return $path;
178 }
179 }
180  
181 $self->throw(
182 0 'PERLMODULE-00002: Modul nicht gefunden',
183 Module=>$$self,
184 );
185 }
186  
187 # -----------------------------------------------------------------------------
188  
189 =head3 loadPath() - Liefere den Lade-Pfad
190  
191 =head4 Synopsis
192  
193 $path = $mod->loadPath;
194  
195 =head4 Description
196  
197 Liefere den Pfad, unter dem das Modul geladen wurde (mit use
198 oder require).
199  
200 Diese Methode ist nützlich, wenn einem nicht klar ist, aus
201 welchem Pfad heraus Perl ein Modul geladen hat, z.B. weil möglicherweise
202 mehrere Versionen des Moduls unter verschiedenen Pfaden installiert
203 sind.
204  
205 Ohne Quiq::PerlModule kann dieselbe Information auf folgendem Weg herausgefunden
206 werden - A::B::C sei das Modul:
207  
208 $INC{'A/B/C.pm'}
209  
210 Existiert der Eintrag nicht, wurde das Modul nicht geladen.
211  
212 =head4 Example
213  
214 use A::B::C;
215 print Quiq::PerlModule->new('A::B::C')->loadPath;
216 # '/usr/lib/perl5/site_perl/5.10.0/A/B/C.pm'
217  
218 =cut
219  
220 # -----------------------------------------------------------------------------
221  
222 3 ▶sub loadPath {
Subroutine loadPath: called CC=2 SCAR=6.9
Pod documented
223 3 my $self = shift;
224  
225 3 my $path = $self->nameToPath;
226 3 ▶ if (!exists $INC{$path}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $INC{$path}) 1 2 3
227 1 $self->throw(
228 'PERLMODULE-00001: Modul ist nicht geladen',
229 Module=>$$self,
230 );
231 }
232  
233 2 return $INC{$path};
234 }
235  
236 # -----------------------------------------------------------------------------
237  
238 =head3 moduleDir() - Liefere den Pfad zum Modulverzeichnis
239  
240 =head4 Synopsis
241  
242 $dir = $mod->moduleDir;
243  
244 =head4 Description
245  
246 Das Modulverzeichnis ist der Ladepfad des Moduls (s. loadPath())
247 ohne die Dateiendung C<.pm>.
248  
249 =head4 Example
250  
251 use A::B::C;
252 print Quiq::PerlModule->new('A::B::C')->moduleDir;
253 # '/usr/lib/perl5/site_perl/5.10.0/A/B/C'
254  
255 =cut
256  
257 # -----------------------------------------------------------------------------
258  
259 0 ▶sub moduleDir {
Subroutine moduleDir: not called CC=1 SCAR=6.9
Pod documented
260 0 my $self = shift;
261  
262 0 my $dir = $self->loadPath;
263 0 $dir =~ s/\.pm$//;
264  
265 0 return $dir;
266 }
267  
268 # -----------------------------------------------------------------------------
269  
270 =head3 nameToPath() - Liefere Modulpfad zum Modulnamen
271  
272 =head4 Synopsis
273  
274 $path = $class->nameToPath($name); # Klassenmethode
275 $path = $mod->nameToPath; # Objektmethode
276  
277 =head4 Description
278  
279 Wandele Modulname (wie er bei use angegeben wird) in Modulpfad
280 (wie er in %INC als Schlüssel vorkommt) um und liefere diesen zurück.
281  
282 =head4 Example
283  
284 'A::B::C' => 'A/B/C.pm'
285  
286 =cut
287  
288 # -----------------------------------------------------------------------------
289  
290 7 ▶sub nameToPath {
Subroutine nameToPath: called CC=2 SCAR=6.9
Pod documented
291 7 my $this = shift;
292 # @_: $name
293  
294 7 ▶ my $str = ref $this? $$this: shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $this ? : 6 1 7
295 7 $str =~ s|::|/|g;
296 7 $str .= '.pm';
297  
298 7 return $str;
299 }
300  
301 # -----------------------------------------------------------------------------
302  
303 =head3 pathToName() - Liefere Modulname zum Modulpfad
304  
305 =head4 Synopsis
306  
307 $name = $class->pathToName($path);
308  
309 =head4 Description
310  
311 Wandele Modulpfad (wie er in %INC als Schlüssel vorkommt) in Modulnamen
312 (wie er bei use angegeben wird) um und liefere diesen zurück.
313  
314 =head4 Example
315  
316 'A/B/C.pm' ==> 'A::B::C'
317  
318 =cut
319  
320 # -----------------------------------------------------------------------------
321  
322 1 ▶sub pathToName {
Subroutine pathToName: called CC=1 SCAR=0.0
Pod documented
323 1 my ($class,$str) = @_;
324  
325 1 $str =~ s/\.pm$//;
326 1 $str =~ s|/|::|g;
327  
328 1 return $str;
329 }
330  
331 # -----------------------------------------------------------------------------
332  
333 =head1 VERSION
334  
335 1.240
336  
337 =head1 AUTHOR
338  
339 Frank Seitz, L<http://fseitz.de/>
340  
341 =head1 COPYRIGHT
342  
343 Copyright (C) 2026 Frank Seitz
344  
345 =head1 LICENSE
346  
347 This code is free software; you can redistribute it and/or modify
348 it under the same terms as Perl itself.
349  
350 =cut
351  
352 # -----------------------------------------------------------------------------
353  
354 3 1;
355  
356 # eof
« Quiq/Record.pm ↑ Summary Quiq/Sftp/Client.pm »