blib/lib/App/hopen/HopenFileKit.pm

filter: Statementstmt 100% 75 / 75 Branchbran 68.7% 11 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod n/a totaltotal 95.3% 103 / 108 CC 11 SCAR 11.5
CC
11
Cov
95%
_language_import
7.3
import
4.0
_create_language
2.0
CRAP 3.2
« blib/lib/App/hopen/MYhopen.pm ↑ Summary blib/lib/App/hopen/G/Cmd.pm »
1 # App::hopen::HopenFileKit - set up a hopen file
2 package App::hopen::HopenFileKit;
3 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Data::Hopen::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Import::Into;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Package::Alias ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Imports. Note: `()` marks packages we export to the caller but
10 # don't use ourselves. These are in the same order as in import().
11 1 ▶use App::hopen::BuildSystemGlobals;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use App::hopen::Util::BasedPath ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Path::Class ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use App::hopen::Phases ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Data::Hopen qw(:default loadfrom);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 1 our $VERSION = '0.000013'; # TRIAL
20  
21 1 ▶use parent 'Exporter'; # Exporter-exported symbols {{{1
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 our (@EXPORT, @EXPORT_OK, %EXPORT_TAGS);
23 BEGIN {
24 1 ▶ @EXPORT = qw($__R_on_result *FILENAME);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 # ^ for Phases::on()
26 # ^ glob so it can be localized
27 1 @EXPORT_OK = qw();
28 1 %EXPORT_TAGS = (
29 default => [@EXPORT],
30 all => [@EXPORT, @EXPORT_OK]
31 );
32 } # }}}1
33  
34 # Docs {{{1
35  
36 =head1 NAME
37  
38 App::hopen::HopenFileKit - Kit to be used by a hopen file
39  
40 =head1 SYNOPSIS
41  
42 This is a special-purpose test kit used for interpreting hopen files.
43 See L<App::hopen/_run_phase>. Usage:
44  
45 use App::hopen::HopenFileKit "<filename>"[, other args]
46  
47 C<< <filename> >> is the name you want to use for the package using
48 this module, and will be loaded into constant C<$FILENAME> in that
49 package.
50  
51 C<[other args]> are per Exporter, and should be omitted unless you
52 really know what you're doing!
53  
54 =head1 FUNCTIONS
55  
56 =cut
57  
58 # }}}1
59  
60 # Which languages we've loaded
61 1 my %_loaded_languages;
62  
63 sub _language_import { # {{{1
64  
65 =head2 _language_import
66  
67 C<import()> routine for the fake "language" package
68  
69 =cut
70  
71 6 ▶ my $target = caller;
Subroutine _language_import: called CC=7 SCAR=19.9
72 #say "language invoked from $target";
73 6 shift; # Drop our package name
74 6 ▶ croak "I need at least one language name" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 6 6
75  
76 6 ▶ die "TODO permit aliases" if ref $_[0]; # TODO take { alias => name }
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 6 6
77  
78 6 foreach my $language (@_) {
79 6 ▶ next if $_loaded_languages{$language};
Branch: 1 branch
BranchTrueFalseTotal
if $_loaded_languages{$language} 5 1 6
80 # Only load any given language once. This avoids cowardly warnings
81 # from Package::Alias, but still causes warnings if a language
82 # overrides an unrelated package. (For example, saying
83 # `use language "Graph"` would be a Bad Idea :) .)
84  
85 # Find the package for the given language
86 1 my ($src_package, $dest_package);
87 1 ▶ $src_package = loadfrom($language, "${Toolset}::", '')
Branch: 1 branch
BranchTrueFalseTotal
$src_package = loadfrom($language, "$App::hopen::HopenFileKit::Toolset::", "") or croak("Can't find a package for language ``${language}'' " . "in toolset ``$App::hopen::HopenFileKit::Toolset''") 0 1 1
88 or croak "Can't find a package for language ``$language'' " .
89 "in toolset ``$Toolset''";
90  
91 # Import the given language into the root namespace.
92 # Use only the last ::-separated component if :: are present.
93 1 ▶ $dest_package = ($src_package =~ m/::([^:]+)$/) ? $1 : $src_package;
Branch: 1 branch
BranchTrueFalseTotal
$src_package =~ /::([^:]+)$/u ? : 1 0 1
94 1 Package::Alias->import::into($target, $dest_package => $src_package);
95 # TODO add to Package::Alias the ability to pass parameters
96 # to the package being loaded.
97  
98 1 $_loaded_languages{$language} = true;
99 } #foreach requested language
100 } #_language_import }}}1
101  
102 sub _create_language { # {{{1
103  
104 =head2 _create_language
105  
106 Create a package "language" so that the calling package can invoke it.
107  
108 =cut
109  
110 #say "_create_language";
111 9 ▶ return if %language::; #idempotent
Subroutine _create_language: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if %main::language:: 8 1 9
112  
113 {
114 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
115 1 *{'language::import'} = \&_language_import;
116 }
117  
118 1 $INC{'language.pm'} = 1;
119 } #_create_language() }}}1
120  
121 sub import { # {{{1
122  
123 =head2 import
124  
125 Set up the calling package. See L</SYNOPSIS> for usage.
126  
127 =cut
128  
129 9 ▶ my $target = caller;
Subroutine import: called CC=4 SCAR=13.9
130 9 ▶ my $target_friendly_name = $_[1] or croak "Need a filename";
Branch: 1 branch
BranchTrueFalseTotal
my $target_friendly_name = $_[1] or croak("Need a filename") 0 9 9
131 # 0=__PACKAGE__, 1=filename
132 9 my @args = splice @_, 1, 1;
133 # Remove the filename; leave the rest of the args for Exporter's use
134  
135 # Export our stuff
136 9 __PACKAGE__->export_to_level(1, @args);
137  
138 # Re-export packages
139 9 $_->import::into($target) foreach qw(
140 Data::Hopen::Base
141 App::hopen::BuildSystemGlobals
142 App::hopen::Util::BasedPath
143 Path::Class
144 );
145  
146 9 App::hopen::Phases->import::into($target, qw(:all :hopenfile));
147 9 Data::Hopen->import::into($target, ':all');
148  
149 # Initialize data in the caller
150 {
151 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
152 9 *{ $target . '::FILENAME' } = eval("\\\"\Q$target_friendly_name\E\"");
153 # Need `eval` to make it read-only - even \"$target..." isn't RO.
154 # \Q and \E since, on Windows, $friendly_name is likely to
155 # include backslashes.
156 }
157  
158 # Create packages at the top level
159 9 _create_language();
160 Package::Alias->import::into($target, 'H' => 'App::hopen::H')
161 9 ▶ unless eval { scalar keys %H:: };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { scalar keys %main::H:: } } 1 8 9
162 # Don't import twice, but without the need to set Package::Alias::BRAVE
163 # TODO permit handling the situation in which an actual package H is
164 # loaded, and the hopenfile needs to use something else.
165 } #import() # }}}1
166  
167 1 1;
168 __END__
169 # vi: set fdm=marker: #
« blib/lib/App/hopen/MYhopen.pm ↑ Summary blib/lib/App/hopen/G/Cmd.pm »