| 1 | # | ||||||||||||
| 2 | # Copyright (c) 2021 Christian Jaeger, copying@christianjaeger.ch | ||||||||||||
| 3 | # | ||||||||||||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||||||||||||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||||||||||||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||||||||||||
| 7 | # bundled with this file. | ||||||||||||
| 8 | # | ||||||||||||
| 9 | |||||||||||||
| 10 | =head1 NAME | ||||||||||||
| 11 | |||||||||||||
| 12 | Chj::Packages | ||||||||||||
| 13 | |||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||
| 15 | |||||||||||||
| 16 | 1 | ▶ | use Chj::Packages qw( | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | package_to_requirepath | ||||||||||||
| 18 | require_package | ||||||||||||
| 19 | fallible_require_package_by_path | ||||||||||||
| 20 | xrequire_package_by_path | ||||||||||||
| 21 | 1 | ); | |||||||||||
| 22 | my $packagename = "Chj::Packages"; | ||||||||||||
| 23 | my $possibly_previously_loaded_path = require_package $packagename; | ||||||||||||
| 24 | my $true_or_failure = fallible_require_package_by_path $packagename; | ||||||||||||
| 25 | # xrequire_package_by_path turns the failure into an exception. | ||||||||||||
| 26 | |||||||||||||
| 27 | # related (mess?): | ||||||||||||
| 28 | # use FP::Predicates qw($package_re); | ||||||||||||
| 29 | |||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||
| 31 | |||||||||||||
| 32 | Dealing with packages, and their loading, yet again. | ||||||||||||
| 33 | |||||||||||||
| 34 | =head1 TODO | ||||||||||||
| 35 | |||||||||||||
| 36 | What is the proper upstream way? And do I have other code somewhere? | ||||||||||||
| 37 | |||||||||||||
| 38 | Also, FP::Fallible may not be the best idea (instead make a proper sum | ||||||||||||
| 39 | type and offer an `Ok` value? A modified L<FP::Either>?) | ||||||||||||
| 40 | |||||||||||||
| 41 | =head1 SEE ALSO | ||||||||||||
| 42 | |||||||||||||
| 43 | L<FP::Fallible> | ||||||||||||
| 44 | |||||||||||||
| 45 | =head1 NOTE | ||||||||||||
| 46 | |||||||||||||
| 47 | This is alpha software! Read the status section in the package README | ||||||||||||
| 48 | or on the L<website|http://functional-perl.org/>. | ||||||||||||
| 49 | |||||||||||||
| 50 | =cut | ||||||||||||
| 51 | |||||||||||||
| 52 | package Chj::Packages; | ||||||||||||
| 53 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 54 | 3 | ▶ | use utf8; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 55 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 56 | 3 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 57 | 3 | ▶ | use experimental 'signatures'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 58 | 3 | ▶ | use Exporter "import"; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 59 | |||||||||||||
| 60 | 3 | our @EXPORT = qw(); | |||||||||||
| 61 | 3 | our @EXPORT_OK = qw( | |||||||||||
| 62 | package_to_requirepath | ||||||||||||
| 63 | require_package | ||||||||||||
| 64 | fallible_require_package_by_path | ||||||||||||
| 65 | xrequire_package_by_path | ||||||||||||
| 66 | ); | ||||||||||||
| 67 | 3 | our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); | |||||||||||
| 68 | |||||||||||||
| 69 | 3 | ▶ | use FP::Docstring; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 70 | 3 | ▶ | use Chj::TEST ":all"; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 71 | |||||||||||||
| 72 | #use Chj::Package::OfPath qw(package_of_path); | ||||||||||||
| 73 | # Uh, that's expensive, so: XXX better | ||||||||||||
| 74 | 8 | ▶ | sub path_to_package__cheap($path) { | ||||||||||
|
Subroutine
path_to_package__cheap: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||
| 75 | 8 | $path =~ s{^\./}{}; | |||||||||||
| 76 | 8 | my $package = $path; | |||||||||||
| 77 | 8 | ▶ | while ($package =~ s{^\../}{}) { } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | 8 | $package =~ s{^(lib|meta)/}{}; | |||||||||||
| 79 | 8 | $package =~ s/\.pm$//; | |||||||||||
| 80 | 8 | $package =~ s|/|::|sg; | |||||||||||
| 81 | 8 | $package | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | 1 | ▶ | TEST { path_to_package__cheap "meta/FunctionalPerl/Indexing.pm" } | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 85 | 3 | 'FunctionalPerl::Indexing'; | |||||||||||
| 86 | |||||||||||||
| 87 | 8 | ▶ | sub package_to_requirepath($package) { | ||||||||||
|
Subroutine
package_to_requirepath: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 88 | 8 | $package =~ s|::|/|g; | |||||||||||
| 89 | 8 | $package .= ".pm"; | |||||||||||
| 90 | 8 | $package | |||||||||||
| 91 | } | ||||||||||||
| 92 | |||||||||||||
| 93 | 8 | ▶ | sub require_package($package) { | ||||||||||
|
Subroutine
require_package: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 94 | 8 | __ 'require the package whose namespace is given; return the path | |||||||||||
| 95 | actually loaded'; | ||||||||||||
| 96 | 8 | my $requirepath = package_to_requirepath($package); | |||||||||||
| 97 | 8 | require $requirepath; | |||||||||||
| 98 | 8 | $INC{$requirepath} | |||||||||||
| 99 | } | ||||||||||||
| 100 | |||||||||||||
| 101 | 3 | ▶ | use Cwd 'abs_path'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 102 | 3 | ▶ | use FP::Failure; # worrying dependency | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 103 | |||||||||||||
| 104 | 7 | ▶ | sub fallible_require_package_by_path($path) { | ||||||||||
|
Subroutine
fallible_require_package_by_path: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||
| 105 | 7 | __ 'require_package($path), but check that the returned path is | |||||||||||
| 106 | the same as $path, after taking abs_path of both; not finding | ||||||||||||
| 107 | either path is an error. If the paths are not the same, | ||||||||||||
| 108 | returns an FP::Failure'; | ||||||||||||
| 109 | 7 | ▶ | my $path_abs = abs_path($path) // die "abs_path('$path'), given path: $!"; | ||||||||||
|
Condition50%1/2
abs_path($path) // die("abs_path('${path}'), given path: $!")
MC/DC0%0/1
abs_path($path) // die("abs_path('${path}'), given path: $!")
abs_path($path)
Truth table50%1/2
A
A= abs_path($path)
| |||||||||||||
| 110 | 7 | my $package = path_to_package__cheap $path; # XX hack, how to generalize? | |||||||||||
| 111 | # Can't load by path (at least not 'properly'), so, load and then | ||||||||||||
| 112 | # check: | ||||||||||||
| 113 | 7 | my $path2 = require_package $package; | |||||||||||
| 114 | 7 | ▶ | my $path_abs2 = abs_path($path2) | ||||||||||
|
Condition50%1/2
abs_path($path2) // die("abs_path('${path2}'), path retrieved after loading: $!")
MC/DC0%0/1
abs_path($path2) // die("abs_path('${path2}'), path retrieved after loading: $!")
abs_path($path2)
Truth table50%1/2
A
A= abs_path($path2)
| |||||||||||||
| 115 | // die "abs_path('$path2'), path retrieved after loading: $!"; | ||||||||||||
| 116 | |||||||||||||
| 117 | 7 | ▶ | if ($path_abs eq $path_abs2) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 118 | 6 | 1 | |||||||||||
| 119 | } else { | ||||||||||||
| 120 | 1 | failure "require_package_by_path('$path'): the actually loaded package " | |||||||||||
| 121 | . "is '$path_abs2', not '$path_abs'" | ||||||||||||
| 122 | } | ||||||||||||
| 123 | } | ||||||||||||
| 124 | |||||||||||||
| 125 | 6 | ▶ | sub xrequire_package_by_path ($path) { | ||||||||||
|
Subroutine
xrequire_package_by_path: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||
| 126 | 6 | my $res = fallible_require_package_by_path($path); | |||||||||||
| 127 | 6 | ▶ | $res or die $res | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 128 | } | ||||||||||||
| 129 | |||||||||||||
| 130 | 3 | 1 | |||||||||||