File Coverage

blib/lib/App/hopen/HopenFileKit.pm
Criterion Covered Total %
statement 68 68 100.0
branch 11 16 68.7
condition n/a
subroutine 16 16 100.0
pod n/a
total 95 100 95.0


line stmt bran cond sub pod time code
1             # App::hopen::HopenFileKit - set up a hopen file
2             package App::hopen::HopenFileKit;
3 1     1   6 use strict;
  1         3  
  1         30  
4 1     1   7 use Data::Hopen::Base;
  1         2  
  1         7  
5              
6 1     1   1288 use Import::Into;
  1         3  
  1         24  
7 1     1   509 use Package::Alias ();
  1         622  
  1         26  
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     1   7 use App::hopen::BuildSystemGlobals;
  1         2  
  1         104  
12 1     1   423 use App::hopen::Util::BasedPath ();
  1         3  
  1         21  
13 1     1   6 use Path::Class ();
  1         2  
  1         15  
14              
15 1     1   4 use App::hopen::Phases ();
  1         2  
  1         20  
16 1     1   6 use Data::Hopen qw(:default loadfrom);
  1         2  
  1         186  
17              
18              
19             our $VERSION = '0.000011';
20              
21 1     1   8 use parent 'Exporter'; # Exporter-exported symbols {{{1
  1         2  
  1         6  
22             our (@EXPORT, @EXPORT_OK, %EXPORT_TAGS);
23             BEGIN {
24 1     1   114 @EXPORT = qw($__R_on_result *FILENAME);
25             # ^ for Phases::on()
26             # ^ glob so it can be localized
27 1         2 @EXPORT_OK = qw();
28 1         303 %EXPORT_TAGS = (
29             default => [@EXPORT],
30             all => [@EXPORT, @EXPORT_OK]
31             );
32             } # }}}1
33              
34             # Docs {{{1
35              
36             =head1 NAME
37              
38             Data::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<Data::Hopen::App/_run_phase>. Usage:
44              
45             use Data::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             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 4     4   17 my $target = caller;
72             #say "language invoked from $target";
73 4         8 shift; # Drop our package name
74 4 50       21 croak "I need at least one language name" unless @_;
75              
76 4 50       14 die "TODO permit aliases" if ref $_[0]; # TODO take { alias => name }
77              
78 4         12 foreach my $language (@_) {
79 4 100       818 next if $_loaded_languages{$language};
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         2 my ($src_package, $dest_package);
87 1 50       9 $src_package = loadfrom($language, "${Toolset}::", '')
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 50       23 $dest_package = ($src_package =~ m/::([^:]+)$/) ? $1 : $src_package;
94 1         9 Package::Alias->import::into($target, $dest_package => $src_package);
95              
96 1         627 $_loaded_languages{$language} = true;
97             } #foreach requested language
98             } #_language_import }}}1
99              
100             sub _create_language { # {{{1
101              
102             =head2
103              
104             Create a package "language" so that the calling package can invoke it.
105              
106             =cut
107              
108             #say "_create_language";
109 6 100   6   26 return if %language::; #idempotent
110              
111             {
112 1     1   8 no strict 'refs';
  1         2  
  1         213  
  1         2  
113 1         2 *{'language::import'} = \&_language_import;
  1         5  
114             }
115              
116 1         2 $INC{'language.pm'} = 1;
117             } #_create_language() }}}1
118              
119             sub import { # {{{1
120              
121             =head2 import
122              
123             Set up the calling package. See L</SYNOPSIS> for usage.
124              
125             =cut
126              
127 6     6   21 my $target = caller;
128 6 50       28 my $target_friendly_name = $_[1] or croak "Need a filename";
129             # 0=__PACKAGE__, 1=filename
130 6         25 my @args = splice @_, 1, 1;
131             # Remove the filename; leave the rest of the args for Exporter's use
132              
133             # Export our stuff
134 6         529 __PACKAGE__->export_to_level(1, @args);
135              
136             # Re-export packages
137 6         73 $_->import::into($target) foreach qw(
138             Data::Hopen::Base
139             App::hopen::BuildSystemGlobals
140             App::hopen::Util::BasedPath
141             Path::Class
142             );
143              
144 6         13052 App::hopen::Phases->import::into($target, qw(:all :hopenfile));
145 6         1657 Data::Hopen->import::into($target, ':all');
146              
147             # Initialize data in the caller
148             {
149 1     1   8 no strict 'refs';
  1         2  
  1         155  
  6         1702  
150 6         421 *{ $target . '::FILENAME' } = eval("\\\"$target_friendly_name\"");
  6         31  
151             # Need `eval` to make it read-only - even \"$target..." isn't RO
152             }
153              
154             # Create packages at the top level
155 6         44 _create_language();
156             Package::Alias->import::into($target, 'H' => 'App::hopen::H')
157 6 100       13 unless eval { scalar keys %H:: };
  6         271  
158             # Don't import twice, but without the need to set Package::Alias::BRAVE
159             # TODO permit handling the situation in which an actual package H is
160             # loaded, and the hopenfile needs to use something else.
161             } #import() # }}}1
162              
163             1;
164             __END__
165             # vi: set fdm=marker: #