| 1 | package PMLTQ::Loader; | ||||||||||||||||||||||||||||
| 2 | 27 | our $AUTHORITY = 'cpan:MATY'; | |||||||||||||||||||||||||||
| 3 | 27 | $PMLTQ::Loader::VERSION = '1.5.0'; | |||||||||||||||||||||||||||
| 4 | # ABSTRACT: Module loader for L<PMLTQ::Relation|PMLTQ::Relation>s inspired by L<Mojo::Loader> | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 27 | ▶ | use PMLTQ::Base -strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 27 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 27 | ▶ | use File::Basename 'fileparse'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 27 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | our @EXPORT_OK | ||||||||||||||||||||||||||||
| 14 | 27 | = qw(find_modules load_class); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 278 | ▶ | sub class_to_path { join '.', join('/', split /::|'/, shift), 'pm' } | ||||||||||||||||||||||||||
|
Subroutine
class_to_path: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | sub load_class { | ||||||||||||||||||||||||||||
| 19 | 319 | ▶ | my ($class) = @_; | ||||||||||||||||||||||||||
|
Subroutine
load_class: called CC=6 SCAR=22.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # Check module name | ||||||||||||||||||||||||||||
| 22 | 319 | ▶ | return if !$class || $class !~ /^\w(?:[\w:']*\w)?$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$class and $class =~ /^\w(?:[\w:']*\w)?$/
MC/DC0%0/2
$class and $class =~ /^\w(?:[\w:']*\w)?$/
$class
$class =~ /^\w(?:[\w:']*\w)?$/
Truth table33%1/3
A and B
A= $classB= $class =~ /^\w(?:[\w:']*\w)?$/
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # Loaded | ||||||||||||||||||||||||||||
| 25 | 319 | ▶ | return 1 if $class->can('new') || eval { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$class->can("new") or eval {
do {
my $file = class_to_path($class);
require $file;
1
}
}
MC/DC0%0/2
$class->can("new") or eval {
do {
my $file = class_to_path($class);
require $file;
1
}
}
$class->can("new")
eval {
do {
my $file = class_to_path($class);
require $file;
1
}
}
Truth table66%2/3
A or B
A= $class->can("new")B= eval {
do {
my $file = class_to_path($class);
require $file;
1
}
}
| |||||||||||||||||||||||||||||
| 26 | 278 | my $file = class_to_path($class); | |||||||||||||||||||||||||||
| 27 | 278 | require $file; | |||||||||||||||||||||||||||
| 28 | 278 | 1; | |||||||||||||||||||||||||||
| 29 | }; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # Exists | ||||||||||||||||||||||||||||
| 32 | 0 | ▶ | return if $@ =~ /^Can't locate \Q@{[class_to_path $class]}\E in \@INC/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Real error | ||||||||||||||||||||||||||||
| 35 | 0 | die $@; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub find_modules { | ||||||||||||||||||||||||||||
| 39 | 11 | ▶ | my ($ns) = @_; | ||||||||||||||||||||||||||
|
Subroutine
find_modules: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 11 | my %modules; | |||||||||||||||||||||||||||
| 42 | 11 | for my $directory (@INC) { | |||||||||||||||||||||||||||
| 43 | 121 | ▶ | next unless -d (my $path = File::Spec->catdir($directory, split(/::|'/, $ns))); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # List "*.pm" files in directory | ||||||||||||||||||||||||||||
| 46 | 44 | opendir(my $dir, $path); | |||||||||||||||||||||||||||
| 47 | 44 | for my $file (grep /\.pm$/, readdir $dir) { | |||||||||||||||||||||||||||
| 48 | 1276 | ▶ | next if -d File::Spec->catfile(File::Spec->splitdir($path), $file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 1276 | $modules{"${ns}::" . fileparse $file, qr/\.pm/}++; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 11 | return keys %modules; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 27 | 1; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | __END__ | ||||||||||||||||||||||||||||