Hopen/Phases.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 109 CC 8 SCAR 23.5
CC
8
Cov
0%
on
30.0
next_phase
12.0
phase_idx
6.0
CRAP 10.5
1 # Build::Hopen::Phases - definitions of phases
2 package Build::Hopen::Phases;
3 use Build::Hopen;
4 use Build::Hopen::Base;
5  
6 our $VERSION = '0.000008'; # TRIAL
7  
8 use parent 'Exporter';
9 our (@EXPORT, @EXPORT_OK, %EXPORT_TAGS);
10 BEGIN {
11 my @normal_export_ok = qw(is_phase is_last_phase phase_idx next_phase);
12 my @hopenfile_export = qw(on);
13  
14 @EXPORT = qw(@PHASES);
15 @EXPORT_OK = (@normal_export_ok, @hopenfile_export);
16 %EXPORT_TAGS = (
17 default => [@EXPORT],
18 all => [@EXPORT, @normal_export_ok],
19 hopenfile => [@hopenfile_export],
20 );
21 }
22  
23 use Build::Hopen::BuildSystemGlobals;
24 use List::MoreUtils qw(first_index);
25  
26 # Docs {{{1
27  
28 =head1 NAME
29  
30 Build::Hopen::Phases - Definitions and routines for hopen phases
31  
32 =head1 SYNOPSIS
33  
34 Definition of hopen phases. Phase names are case-insensitive. The canonical
35 form has only the first letter capitalized.
36  
37 Phase names may only contain ASCII letters, digits, or underscore. The first
38 character of a phase may not be a digit. This is so they can be used as
39 identifiers if necessary.
40  
41 This package also defines a special export tag, C<:hopenfile>, for use when
42 running hopen files. The wrapper code in L<Build::Hopen::App> uses this
43 tag. Hopen files themselves do not need to use this tag.
44  
45 =head1 VARIABLES
46  
47 =head2 @PHASES
48  
49 The phases we know about, in order.
50  
51 =head1 FUNCTIONS
52  
53 =cut
54  
55 # }}}1
56  
57 # Phases are case-insensitive.
58 our @PHASES; BEGIN { @PHASES = ('Check', 'Gen'); }
59 # *** This is where the default phase ($PHASES[0]) is set ***
60 # TODO? be more sophisticated about this :)
61  
62 =head2 is_phase
63  
64 Return truthy if the given argument is the name of a phase we know about.
65  
66 =cut
67  
68 sub is_phase {
69 my $test_phase = lc(shift) or croak 'Need a phase name';
70 my $curr_idx = first_index { lc($_) eq $test_phase } @PHASES;
71 return $curr_idx+1; # -1 => falsy; all others => truthy
72 } #is_phase()
73  
74 =head2 is_last_phase
75  
76 Return truthy if the argument is the name of the last phase.
77  
78 =cut
79  
80 sub is_last_phase { lc(shift) eq lc($PHASES[$#PHASES]) }
81  
82 =head2 phase_idx
83  
84 Get the index of the phase given as a parameter.
85 Returns undef if none. Phases are case-insensitive.
86  
87 =cut
88  
89 sub phase_idx {
90 my $test_phase = lc(shift) or croak "Need a phase";
91 my $curr_idx = first_index { lc($_) eq $test_phase } @PHASES;
92 return $curr_idx<0 ? undef : $curr_idx;
93 } #phase_idx()
94  
95 =head2 next_phase
96  
97 Get the phase after the given on. Returns undef if the argument
98 is the last phase. Dies if the argument is not a phase.
99  
100 =cut
101  
102 sub next_phase {
103 my $test_phase = lc(shift) or croak "Need a phase";
104 my $curr_idx = phase_idx $test_phase;
105 die "$test_phase is not a phase I know about" unless defined($curr_idx);
106 return undef if $curr_idx == $#PHASES; # Last one
107  
108 return $PHASES[$curr_idx+1];
109 } #next_phase()
110  
111 =head1 ROUTINES FOR USE IN HOPEN FILES
112  
113 =head2 on
114  
115 Take a given action only in a specified phase. Usage examples:
116  
117 on check => { foo => 42 }; # Just return the given hashref
118 on gen => 1337; # Returns { Gen => 1337 }
119 on check => sub { return { foo => 1337 } };
120 # Call the given sub and return its return value.
121  
122 This is designed for use within a hopen file.
123 See L<Build::Hopen::App/_run_phase> for the execution environment C<on()> is
124 designed to run in.
125  
126 When run as part of a hopen file, C<on()> will skip the rest of the file if it
127 runs. For example:
128  
129 say "Hello, world!"; # This always runs
130 on check => { answer => $answer }; # This runs during the Check phase
131 on gen => { done => true }; # This runs during the Gen phase
132 say "Phase was neither Check nor Gen"; # Doesn't run in Check or Gen
133  
134 =cut
135  
136 sub on {
137 my $caller = caller;
138  
139 my $which_phase = shift or croak "I need to know which phase this applies to";
140 croak "I need a single value or subroutine" unless @_ == 1;
141 my $val = shift;
142  
143 my $which_idx = phase_idx($which_phase);
144 return if $which_idx != phase_idx;
145  
146 # We are in the correct phase. Take appropriate action and stash the
147 # result for the caller. However, don't change our own return value.
148 my $result = (ref($val) ne 'CODE') ? $val : &$val;
149 $result = { $PHASES[$which_idx] => $result } unless ref $result eq 'HASH';
150 {
151 no strict 'refs';
152 ${ $caller . "::__R_on_result" } = $result;
153 }
154  
155 # Done --- skip the rest of the hopen file if we're in one.
156 eval {
157 no warnings 'exiting';
158 last __R_DO;
159 };
160 } #on()
161  
162 1;
163 __END__