File Coverage

blib/lib/Perl/Configure/Questions.pm
Criterion Covered Total %
statement 49 57 85.9
branch 2 2 100.0
condition n/a
subroutine 11 13 84.6
pod 1 10 10.0
total 63 82 76.8


line stmt bran cond sub pod time code
1             package Perl::Configure::Questions;
2 2     2   26143 use strict;
  2         3  
  2         48  
3 2     2   8 use warnings;
  2         2  
  2         54  
4 2     2   722 use YAML qw(Load);
  2         12850  
  2         1029  
5              
6             our @QA = yaml_read();
7              
8             ###########################################
9             sub new {
10             ###########################################
11 6     6 0 2114 my($class) = @_;
12              
13 6         8 my $self = {
14             };
15              
16 6         17 bless $self, $class;
17             }
18              
19             ###########################################
20             sub add {
21             ###########################################
22 4     4 0 21 my($self, $token, $question,
23             $sample_answer, $override) = @_;
24              
25 4         13 push @QA, [$token, $question, $sample_answer, $override];
26             }
27              
28             ###########################################
29             sub remove {
30             ###########################################
31 0     0 0 0 my($self, $token) = @_;
32              
33 0         0 @QA = grep { $_->[0] ne $token } @QA;
  0         0  
34             }
35              
36             ###########################################
37             sub yaml_read {
38             ###########################################
39 2     2 0 3 my($self) = @_;
40              
41 2         291 my $text = join '', ;
42 2         36 my @data = Load($text);
43              
44 2         170970 return @data;
45             }
46              
47             ###########################################
48             sub by_key {
49             ###########################################
50 3     3 0 384 my($self) = @_;
51              
52 3         5 my %by_key = ();
53              
54 3         5 for (@QA) {
55 304         520 $by_key{$_->[0]} = [ $_->[1], $_->[2], $_->[3] ];
56             }
57              
58 3         10 return \%by_key;
59             }
60              
61             ###########################################
62             sub by_pattern {
63             ###########################################
64 2     2 0 3 my($self) = @_;
65              
66 2         15 my @patterns = $self->patterns();
67 2         7 my %by_match = ();
68              
69 2         5 for (@QA) {
70 203         369 $by_match{shift @patterns} = [ $_->[0], $_->[2], $_->[3] ];
71             }
72              
73 2         20 return \%by_match;
74             }
75              
76             ###########################################
77             sub by_match {
78             ###########################################
79 0     0 0 0 my($self) = @_;
80              
81 0         0 my %by_match = ();
82              
83 0         0 for (@QA) {
84 0         0 $by_match{$_->[1]} = [ $_->[0], $_->[2] ];
85             }
86              
87 0         0 return \%by_match;
88             }
89              
90             ###########################################
91             sub questions {
92             ###########################################
93 7     7 1 15 my($self) = @_;
94              
95 7         10 return map { $_->[1] } @QA;
  408         343  
96             }
97              
98             ###########################################
99             sub patterns {
100             ###########################################
101 6     6 0 279 my($self) = @_;
102              
103 6         9 my @patterns = ();
104              
105 6         9 for my $question (questions()) {
106              
107 307         185 my $pattern = "";
108 307         177 my $rest = $question;
109              
110 307 100       157 { if($rest =~ /\G(.*?)ANY\{.*?}(.*)/g) {
  325         389  
111 18         30 $pattern .= quotemeta($1);
112 18         7 $pattern .= ".*?";
113 18         26 $rest = $2;
114 18         15 redo;
115             } else {
116 307         347 $pattern .= quotemeta($rest);
117             }
118             }
119              
120 307         301 push @patterns, $pattern;
121             }
122              
123 6         62 return @patterns;
124             }
125              
126             ###########################################
127             sub tokens {
128             ###########################################
129 2     2 0 555 my($self) = @_;
130              
131 2         4 return map { $_->[0] } @QA;
  202         203  
132             }
133              
134             1;
135              
136             =head1 NAME
137              
138             Perl::Configure::Questions - Questions asked by perl's Configure
139              
140             =head1 SYNOPSIS
141              
142             use Perl::Configure::Questions;
143              
144             my $q = Perl::Configure::Questions->new();
145              
146             # Add a new (customized) token/question
147             $q->add($token, $question, $sample_answer, $override)
148              
149             # These are used by Perl::Configure internally
150             my @questions = $q->questions();
151             my @patterns = $q->patterns();
152             my @tokens = $q->tokens();
153             my $by_key = $q->by_key();
154              
155             =head1 DESCRIPTION
156              
157             C returns a list of questions asked by perl's Configure.
158             C just runs a quotemeta() on the strings returned by
159             @questions. This module is used internally by Perl::Configure.
160              
161             =head2 Question Format
162              
163             The questions recognized by C are stored in YAML format
164             in the __DATA__ section of C:
165              
166             ...
167             ---
168             - vendor-specific-prefix
169             - Installation prefix to use for vendor-supplied add-ons?
170             - '/foobar'
171             ---
172             ...
173              
174             The first line in each tuple (separated by --- according to YAML rules)
175             holds the token, C in the example above. The second
176             line shows the question regular expression and the third line a
177             'sample answer', which is just used for documentation purposes.
178              
179             =head2 Overriding Configure's defaults by default
180              
181             If there is an optional forth line specifying an override answer,
182             Perl::Configure will use this answer on a match that does not have
183             an answer defined by the user. For example, when a part of the installation
184             path is missing, perl's Configure will ask "Use that name anyway?" and
185             provide "n" as a default. This, of course, is unfortunate, since accepting
186             the default will cause Configure to pop the question again and have
187             Perl::Configure enter an endless loop.
188              
189             For this reason, "dir-check" has a fourth parameter defined that overrides
190             Configure's default of "n" with "y":
191              
192             - dir-check
193             - Use that name anyway?
194             - n
195             - y
196              
197             Same holds true for the question of reusing an existing config.sh file,
198             which gets overridden to "n" to start from a clean slate every time.
199              
200             =head2 Fuzzy matching
201              
202             Note that regex meta characters in the question line are B escaped.
203             Instead, if a part of the question should match I text, use the
204             ANY{...} clause:
205              
206             ...
207             ---
208             - compiler-flags-special
209             - Any special flags to pass to ANY{cc -c} to compile shared library modules?
210             - '-fpic'
211             ---
212             ...
213              
214             This will cause the question matcher to accept any text instead of
215             C, which comes in handy if Configure dynamically replaces these
216             parts based on previous selections.
217              
218             =head2 Remove questions
219              
220             To debug problems with automatically provided answers that cause
221             endless loops during the configuration process, it sometimes helps
222             to remove a question from the Perl::Configure pool:
223              
224             my $q = Perl::Configure::Questions->new();
225             $q->remove('dynamic-extensions');
226              
227             my $conf = Perl::Configure->new( questions => $q );
228              
229             In this example, Perl::Configure won't recognize the question on dynamic
230             extentions anymore and therefore block the Configure process at this question,
231             allowing the operator to examine the question and the proposed answer
232             thoroughly.
233              
234             =head1 AUTHOR
235              
236             Mike Schilli, m@perlmeister.com, 2006
237              
238             =head1 COPYRIGHT AND LICENSE
239              
240             Copyright (C) 2006 by Mike Schilli
241              
242             This library is free software; you can redistribute it and/or modify
243             it under the same terms as Perl itself, either Perl version 5.8.5 or,
244             at your option, any later version of Perl 5 you may have available.
245              
246             =cut
247              
248             __DATA__