line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# -*- perl -*- |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Module::TestConfig - asks questions and autowrite a module |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# $Id: TestConfig.pm,v 1.33 2003/08/29 18:40:16 jkeroes Exp $ |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
package Module::TestConfig; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
require 5.008; |
10
|
7
|
|
|
7
|
|
210824
|
use strict; |
|
7
|
|
|
|
|
18
|
|
|
7
|
|
|
|
|
298
|
|
11
|
7
|
|
|
7
|
|
41
|
use Carp; |
|
7
|
|
|
|
|
15
|
|
|
7
|
|
|
|
|
746
|
|
12
|
7
|
|
|
7
|
|
40
|
use Fcntl; |
|
7
|
|
|
|
|
15
|
|
|
7
|
|
|
|
|
8699
|
|
13
|
7
|
|
|
7
|
|
48
|
use File::Basename qw/dirname/; |
|
7
|
|
|
|
|
17
|
|
|
7
|
|
|
|
|
730
|
|
14
|
7
|
|
|
7
|
|
9574
|
use Params::Validate; |
|
7
|
|
|
|
|
97129
|
|
|
7
|
|
|
|
|
485
|
|
15
|
7
|
|
|
7
|
|
7389
|
use Config::Auto; |
|
7
|
|
|
|
|
47513
|
|
|
7
|
|
|
|
|
288
|
|
16
|
7
|
|
|
7
|
|
7769
|
use Module::TestConfig::Question; |
|
7
|
|
|
|
|
19
|
|
|
7
|
|
|
|
|
15738
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# Dynamically loaded modules |
19
|
|
|
|
|
|
|
# Data::Dumper; |
20
|
|
|
|
|
|
|
# Text::FormatTable; |
21
|
|
|
|
|
|
|
# Term::ReadKey; |
22
|
|
|
|
|
|
|
# File::Path |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
#------------------------------------------------------------ |
27
|
|
|
|
|
|
|
# Methods |
28
|
|
|
|
|
|
|
#------------------------------------------------------------ |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub new { |
31
|
8
|
|
|
8
|
1
|
209
|
my $proto = shift; |
32
|
8
|
|
33
|
|
|
63
|
my $class = ref $proto || $proto; |
33
|
8
|
|
|
|
|
31
|
my $self = bless {}, $class; |
34
|
8
|
|
|
|
|
65
|
return $self->init( |
35
|
|
|
|
|
|
|
verbose => 1, |
36
|
|
|
|
|
|
|
defaults => 'defaults.config', |
37
|
|
|
|
|
|
|
file => 'MyConfig.pm', |
38
|
|
|
|
|
|
|
package => 'MyConfig', |
39
|
|
|
|
|
|
|
order => [ qw/defaults/ ], |
40
|
|
|
|
|
|
|
questions => [ ], |
41
|
|
|
|
|
|
|
_defaults => { }, |
42
|
|
|
|
|
|
|
qi => 0, |
43
|
|
|
|
|
|
|
@_, |
44
|
|
|
|
|
|
|
); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub init { |
48
|
8
|
|
|
8
|
0
|
93
|
my ( $self, %args ) = @_; |
49
|
|
|
|
|
|
|
|
50
|
8
|
|
|
|
|
51
|
while ( my ( $method, $args ) = each %args ) { |
51
|
59
|
100
|
|
|
|
254
|
if ( $self->can( $method ) ) { |
52
|
58
|
|
|
|
|
175
|
$self->$method( $args ); |
53
|
|
|
|
|
|
|
} else { |
54
|
1
|
|
|
|
|
256
|
croak "Can't handle arg: '$method'. Aborting"; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
7
|
|
|
|
|
27
|
$self->load_defaults; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Params::Validate::validation_options( |
61
|
22
|
|
|
22
|
|
396
|
on_fail => sub { $_[0] =~ s/to.*did //; die "Your answer didn't validate.\n$_[0]" }, |
|
22
|
|
|
|
|
104
|
|
62
|
7
|
|
|
|
|
7973
|
); |
63
|
|
|
|
|
|
|
|
64
|
7
|
|
|
|
|
239
|
return $self; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub load_defaults { |
68
|
7
|
|
|
7
|
1
|
13
|
my $self = shift; |
69
|
7
|
100
|
|
|
|
214
|
$self->{_defaults} = Config::Auto::parse( $self->{defaults} ) |
70
|
|
|
|
|
|
|
if -r $self->{defaults}; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub verbose { |
74
|
9
|
|
|
9
|
1
|
20
|
my $self = shift; |
75
|
9
|
100
|
|
|
|
38
|
$self->{verbose} = shift if @_; |
76
|
9
|
|
|
|
|
43
|
$self->{verbose}; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub debug { |
80
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
81
|
0
|
0
|
|
|
|
0
|
$self->{debug} = shift if @_; |
82
|
0
|
|
|
|
|
0
|
$self->{debug}; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub file { |
86
|
10
|
|
|
10
|
1
|
18
|
my $self = shift; |
87
|
10
|
100
|
|
|
|
64
|
$self->{file} = shift if @_; |
88
|
10
|
|
|
|
|
74
|
$self->{file}; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub package { |
92
|
7
|
|
|
7
|
1
|
13
|
my $self = shift; |
93
|
7
|
50
|
|
|
|
85
|
$self->{package} = shift if @_; |
94
|
7
|
|
|
|
|
35
|
$self->{package}; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# The filename of the defaults file. |
98
|
|
|
|
|
|
|
sub defaults { |
99
|
9
|
|
|
9
|
1
|
20
|
my $self = shift; |
100
|
9
|
100
|
|
|
|
60
|
$self->{defaults} = shift if @_; |
101
|
9
|
|
|
|
|
43
|
$self->{defaults}; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# The defaults hash |
105
|
|
|
|
|
|
|
sub _defaults { |
106
|
8
|
|
|
8
|
|
18
|
my $self = shift; |
107
|
8
|
50
|
|
|
|
77
|
$self->{_defaults} = shift if @_; |
108
|
8
|
|
|
|
|
37
|
$self->{_defaults}; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub order { |
112
|
53
|
|
|
53
|
1
|
72
|
my $self = shift; |
113
|
|
|
|
|
|
|
|
114
|
53
|
100
|
|
|
|
137
|
if ( @_ ) { |
115
|
9
|
100
|
|
|
|
36
|
my @order = ref $_[0] eq "ARRAY" ? @{ $_[0] } : @_; |
|
8
|
|
|
|
|
24
|
|
116
|
9
|
|
|
|
|
36
|
for my $order ( @order ) { |
117
|
12
|
50
|
|
|
|
260
|
croak "Bad arg given to order(): '$order'" |
118
|
|
|
|
|
|
|
unless grep /^$order$/, qw/defaults env/; |
119
|
|
|
|
|
|
|
} |
120
|
9
|
|
|
|
|
35
|
$self->{order} = [ @order ]; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
53
|
100
|
|
|
|
157
|
return wantarray ? @{ $self->{order} } : $self->{order}; |
|
46
|
|
|
|
|
165
|
|
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub questions { |
127
|
15
|
|
|
15
|
1
|
31
|
my $self = shift; |
128
|
|
|
|
|
|
|
|
129
|
15
|
50
|
66
|
|
|
158
|
if ( @_ > 1 ) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
130
|
0
|
|
|
|
|
0
|
$self->{questions} |
131
|
0
|
|
|
|
|
0
|
= [ map { Module::TestConfig::Question->new( $_ ) } @_ ]; |
132
|
|
|
|
|
|
|
} elsif ( @_ == 1 && ref $_[0] eq "ARRAY" ) { |
133
|
23
|
|
|
|
|
121
|
$self->{questions} |
134
|
7
|
|
|
|
|
15
|
= [ map { Module::TestConfig::Question->new( $_ ) } @{ $_[0] } ]; |
|
7
|
|
|
|
|
20
|
|
135
|
|
|
|
|
|
|
} elsif ( @_ ) { |
136
|
0
|
|
|
|
|
0
|
croak "questions() got bad args. Needs a list or arrayref."; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
15
|
100
|
|
|
|
106
|
return wantarray ? @{ $self->{questions} } : $self->{questions}; |
|
2
|
|
|
|
|
12
|
|
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub answers { |
143
|
2
|
|
|
2
|
1
|
4
|
my $self = shift; |
144
|
2
|
100
|
|
|
|
8
|
$self->{answers} = shift if @_; |
145
|
2
|
100
|
|
|
|
9
|
return wantarray ? %{ $self->{answers} } : $self->{answers}; |
|
1
|
|
|
|
|
12
|
|
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub answer { |
149
|
54
|
|
|
54
|
1
|
3774
|
my $self = shift; |
150
|
54
|
|
|
|
|
252
|
return $self->{answers}{+shift}; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub ask { |
154
|
6
|
|
|
6
|
1
|
201
|
my $self = shift; |
155
|
|
|
|
|
|
|
|
156
|
23
|
|
|
|
|
85
|
do { |
157
|
|
|
|
|
|
|
|
158
|
23
|
|
|
|
|
38
|
my $i = $self->{qi}; |
159
|
23
|
|
|
|
|
40
|
my $q = $self->{questions}[$i]; |
160
|
23
|
|
|
|
|
73
|
my $name = $q->name; |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
# Skip the question? |
163
|
23
|
50
|
|
|
|
98
|
if ( $q->skip ) { |
164
|
0
|
0
|
|
|
|
0
|
if ( ref $q->skip eq "CODE" ) { |
|
|
0
|
|
|
|
|
|
165
|
0
|
0
|
|
|
|
0
|
next if $q->skip->( $self ); |
166
|
|
|
|
|
|
|
} elsif ( not ref $q->skip ) { |
167
|
0
|
0
|
|
|
|
0
|
next if $q->skip; |
168
|
|
|
|
|
|
|
} else { |
169
|
0
|
|
|
|
|
0
|
croak "Don't know how to handle question #$i\'s skip block"; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
23
|
|
|
|
|
33
|
my $attempts = 0; |
174
|
|
|
|
|
|
|
|
175
|
43
|
|
|
|
|
108
|
ASK: { |
176
|
23
|
|
|
|
|
28
|
my @args = ( $name => $self->prompt( $q ) ); |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
# Valid answer? |
179
|
43
|
100
|
|
|
|
172
|
if ( $q->validate ) { |
180
|
25
|
50
|
|
|
|
59
|
croak "validate must be a hashref. Aborting" |
181
|
|
|
|
|
|
|
unless ref $q->validate eq "HASH"; |
182
|
|
|
|
|
|
|
|
183
|
25
|
|
|
|
|
36
|
eval { validate( @args, { $name => $q->validate } ) }; |
|
25
|
|
|
|
|
74
|
|
184
|
|
|
|
|
|
|
|
185
|
25
|
100
|
|
|
|
173
|
if ( $@ ) { |
186
|
22
|
|
|
|
|
94
|
warn $@; |
187
|
|
|
|
|
|
|
|
188
|
22
|
100
|
|
|
|
1039
|
if ( ++$attempts > 10 ) { |
189
|
2
|
|
|
|
|
8
|
warn "Let's just skip that question, shall we?\n\n"; |
190
|
2
|
|
|
|
|
94
|
last ASK; |
191
|
|
|
|
|
|
|
} else { |
192
|
20
|
|
|
|
|
90
|
warn "Please try again. [Attempt $attempts]\n\n"; |
193
|
20
|
|
|
|
|
858
|
redo ASK; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
21
|
|
|
|
|
103
|
$self->{answers}{$name} = $args[-1]; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
6
|
|
|
|
|
15
|
} while ( $self->{qi}++ < scalar @{$self->{questions}} - 1 ); |
202
|
|
|
|
|
|
|
|
203
|
6
|
|
|
|
|
33
|
return $self; |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
sub get_default { |
207
|
43
|
|
|
43
|
1
|
71
|
my ($self, $i) = @_; |
208
|
|
|
|
|
|
|
|
209
|
43
|
|
66
|
|
|
156
|
$i ||= $self->{qi}; |
210
|
43
|
|
|
|
|
70
|
my $q = $self->{questions}[$i]; |
211
|
43
|
|
|
|
|
110
|
my $default = $q->default; |
212
|
43
|
50
|
|
|
|
105
|
my $name = $q->name |
213
|
|
|
|
|
|
|
or croak "No name defined for question \#$i."; |
214
|
|
|
|
|
|
|
|
215
|
43
|
|
|
|
|
104
|
for ( $self->order ) { |
216
|
55
|
100
|
|
|
|
165
|
if ( /^env/o ) { |
217
|
19
|
100
|
|
|
|
82
|
return $ENV{"\U$name"} if defined $ENV{"\U$name"}; |
218
|
14
|
100
|
|
|
|
55
|
return $ENV{"\L$name"} if defined $ENV{"\L$name"}; |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
48
|
100
|
100
|
|
|
359
|
return $self->{_defaults}{$name} |
222
|
|
|
|
|
|
|
if /^defaults/ && $self->{_defaults}{$name}; |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
# This will be undef unless set via answers() |
226
|
|
|
|
|
|
|
# or new( answers => {...} ). |
227
|
27
|
|
66
|
|
|
156
|
return $self->{answers}{$name} || $default; |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
sub save { |
231
|
1
|
|
|
1
|
1
|
8
|
my ($self) = @_; |
232
|
|
|
|
|
|
|
|
233
|
1
|
50
|
|
|
|
4
|
my $text = $self->package_text |
234
|
|
|
|
|
|
|
or croak "No text to save. Aborting."; |
235
|
|
|
|
|
|
|
|
236
|
1
|
|
|
|
|
67
|
my $dir = dirname( $self->{file} ); |
237
|
1
|
50
|
|
|
|
38
|
unless ( -d $dir ) { |
238
|
0
|
|
|
|
|
0
|
require File::Path; |
239
|
0
|
0
|
|
|
|
0
|
File::Path::mkpath( [ $dir ], $self->{verbose}) |
240
|
|
|
|
|
|
|
or croak "Can't make path $dir: $!"; |
241
|
|
|
|
|
|
|
} |
242
|
|
|
|
|
|
|
|
243
|
1
|
50
|
|
|
|
139
|
sysopen F, $self->{file}, O_CREAT | O_WRONLY | O_TRUNC, 0600 |
244
|
|
|
|
|
|
|
or croak "Can't open '$self->{file}' for write: $!"; |
245
|
1
|
|
|
|
|
14
|
print F $text; |
246
|
1
|
50
|
|
|
|
68
|
close F or carp ("Can't close '$self->{file}'. $!"), return; |
247
|
|
|
|
|
|
|
|
248
|
1
|
50
|
|
|
|
14
|
print "Module::TestConfig saved $self->{file} with these settings:\n" |
249
|
|
|
|
|
|
|
. $self->report if $self->{verbose}; |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
# Error in v.04: |
253
|
|
|
|
|
|
|
# |
254
|
|
|
|
|
|
|
# Failed test 'save_defaults()' |
255
|
|
|
|
|
|
|
# at t/40_defaults.t line 40. |
256
|
|
|
|
|
|
|
# found warning: Filehandle STDIN reopened as F only for output at /home/sand/.cpan/build/Module-TestConfig-0.04-pvLCvJ/blib/lib/Module/TestConfig.pm line 263. |
257
|
|
|
|
|
|
|
# found carped warning: Skipping bad key with a separator in it: 'bro:ken' at t/40_defaults.t line 39 |
258
|
|
|
|
|
|
|
# expected to find carped warning: /^Skipping bad key/ |
259
|
|
|
|
|
|
|
# Looks like you failed 1 test of 28. |
260
|
|
|
|
|
|
|
# |
261
|
|
|
|
|
|
|
# We're going to ignore that STDIN warning here - ignoring it in t/40_defaults.t does nothing. |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
sub save_defaults { |
264
|
2
|
|
|
2
|
1
|
97
|
my $self = shift; |
265
|
2
|
|
|
|
|
11
|
my %args = ( file => $self->{defaults}, |
266
|
|
|
|
|
|
|
sep => ':', |
267
|
|
|
|
|
|
|
@_, |
268
|
|
|
|
|
|
|
); |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
# doesn't matter if this fails: |
271
|
2
|
50
|
|
|
|
330
|
rename $args{file}, "$args{file}.bak" if -e $args{file}; |
272
|
|
|
|
|
|
|
|
273
|
7
|
|
|
7
|
|
60
|
no warnings "io"; |
|
7
|
|
|
|
|
16
|
|
|
7
|
|
|
|
|
595
|
|
274
|
|
|
|
|
|
|
|
275
|
2
|
50
|
|
|
|
180
|
open F, "> $args{file}" |
276
|
|
|
|
|
|
|
or carp ("Unable to write to $args{file}: $!"), return; |
277
|
|
|
|
|
|
|
|
278
|
7
|
|
|
7
|
|
38
|
use warnings "io"; |
|
7
|
|
|
|
|
14
|
|
|
7
|
|
|
|
|
12054
|
|
279
|
|
|
|
|
|
|
|
280
|
2
|
|
|
|
|
40
|
print F "# This defaults file was autogenerated by Module::TestConfig for $self->{package}\n\n"; |
281
|
|
|
|
|
|
|
|
282
|
2
|
|
|
|
|
4
|
while ( my ($k, $v) = each %{ $self->{answers} } ) { |
|
14
|
|
|
|
|
1317
|
|
283
|
12
|
100
|
|
|
|
116
|
carp ("Skipping bad key with a separator in it: '$k'"), next |
284
|
|
|
|
|
|
|
if $k =~ /$args{sep}/; |
285
|
10
|
|
|
|
|
26
|
print F "$k$args{sep}$v\n"; |
286
|
|
|
|
|
|
|
} |
287
|
|
|
|
|
|
|
|
288
|
2
|
50
|
|
|
|
108
|
close F or return; |
289
|
|
|
|
|
|
|
|
290
|
2
|
|
|
|
|
12
|
return 1; |
291
|
|
|
|
|
|
|
} |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
# Try to report using the best metho |
294
|
|
|
|
|
|
|
sub report { |
295
|
6
|
|
|
6
|
1
|
426
|
my $self = shift; |
296
|
|
|
|
|
|
|
|
297
|
6
|
|
|
|
|
10
|
eval { require Text::FormatTable }; |
|
6
|
|
|
|
|
2750
|
|
298
|
|
|
|
|
|
|
|
299
|
6
|
50
|
|
|
|
49
|
return $@ |
300
|
|
|
|
|
|
|
? $self->report_plain |
301
|
|
|
|
|
|
|
: $self->report_pretty |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
} |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
# Report using Test::FormatTable |
306
|
|
|
|
|
|
|
sub report_pretty { |
307
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
308
|
|
|
|
|
|
|
|
309
|
0
|
0
|
|
|
|
0
|
croak "Can't use report_pretty() unless Text::AutoFormat is loaded." |
310
|
|
|
|
|
|
|
unless UNIVERSAL::can('Text::FormatTable', 'new'); |
311
|
|
|
|
|
|
|
|
312
|
0
|
|
0
|
|
|
0
|
my $screen_width = eval { require Term::ReadKey; (Term::ReadKey::GetTerminalSize())[0] } || 79; |
313
|
0
|
|
|
|
|
0
|
my $table = Text::FormatTable->new( '| r | l |' ); |
314
|
|
|
|
|
|
|
|
315
|
0
|
|
|
|
|
0
|
$table->rule('='); |
316
|
0
|
|
|
|
|
0
|
$table->head('Name', 'Value'); |
317
|
0
|
|
|
|
|
0
|
$table->rule('='); |
318
|
|
|
|
|
|
|
|
319
|
0
|
|
|
|
|
0
|
for my $q ( @{ $self->{questions} } ) { |
|
0
|
|
|
|
|
0
|
|
320
|
0
|
0
|
|
|
|
0
|
if ( $q->noecho ) { |
321
|
0
|
|
|
|
|
0
|
$table->row( $q->name, '*****' ); |
322
|
|
|
|
|
|
|
} else { |
323
|
0
|
|
|
|
|
0
|
$table->row( $q->name, $self->answer( $q->name ) ); |
324
|
|
|
|
|
|
|
} |
325
|
|
|
|
|
|
|
} |
326
|
|
|
|
|
|
|
|
327
|
0
|
|
|
|
|
0
|
$table->rule('-'); |
328
|
|
|
|
|
|
|
|
329
|
0
|
|
|
|
|
0
|
my $report = $table->render($screen_width); |
330
|
0
|
|
|
|
|
0
|
$report =~ s/^/\t/mg; # indent |
331
|
|
|
|
|
|
|
|
332
|
0
|
|
|
|
|
0
|
return $report; |
333
|
|
|
|
|
|
|
} |
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
# Report with plain text. |
336
|
|
|
|
|
|
|
sub report_plain { |
337
|
6
|
|
|
6
|
1
|
12
|
my $self = shift; |
338
|
|
|
|
|
|
|
|
339
|
6
|
|
|
|
|
11
|
my $report = ''; |
340
|
|
|
|
|
|
|
|
341
|
6
|
|
|
|
|
10
|
for my $q ( @{ $self->{questions} } ) { |
|
6
|
|
|
|
|
23
|
|
342
|
30
|
50
|
|
|
|
90
|
if ( $q->noecho ) { |
343
|
0
|
|
|
|
|
0
|
$report .= $q->name . ": *****\n"; |
344
|
|
|
|
|
|
|
} else { |
345
|
30
|
|
|
|
|
82
|
$report .= $q->name . ": " |
346
|
|
|
|
|
|
|
. $self->answer( $q->name ) . "\n"; |
347
|
|
|
|
|
|
|
} |
348
|
|
|
|
|
|
|
} |
349
|
|
|
|
|
|
|
|
350
|
6
|
|
|
|
|
59
|
$report =~ s/^/\t/mg; # indent |
351
|
|
|
|
|
|
|
|
352
|
6
|
|
|
|
|
588
|
return $report; |
353
|
|
|
|
|
|
|
} |
354
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
sub package_text { |
356
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
357
|
|
|
|
|
|
|
|
358
|
1
|
|
|
|
|
5
|
local $/ = undef; |
359
|
1
|
|
|
|
|
22
|
local $_ = ; |
360
|
|
|
|
|
|
|
|
361
|
1
|
|
|
|
|
3
|
my $pkg = $self->{package}; |
362
|
|
|
|
|
|
|
|
363
|
1
|
|
|
|
|
1263
|
require Data::Dumper; |
364
|
1
|
|
|
|
|
7990
|
$Data::Dumper::Terse = 2; |
365
|
1
|
|
|
|
|
10
|
my $answers = Data::Dumper->Dump( [$self->{answers}] ); |
366
|
|
|
|
|
|
|
|
367
|
1
|
|
|
|
|
123
|
s/%%PACKAGE%%/$pkg/mg; |
368
|
1
|
|
|
|
|
6
|
s/%%ANSWERS%%/$answers/m; |
369
|
|
|
|
|
|
|
|
370
|
1
|
|
|
|
|
12
|
return $_; |
371
|
|
|
|
|
|
|
} |
372
|
|
|
|
|
|
|
|
373
|
|
|
|
|
|
|
|
374
|
|
|
|
|
|
|
# Based on ExtUtils::MakeMaker::prompt(). |
375
|
|
|
|
|
|
|
sub prompt { |
376
|
43
|
|
|
43
|
1
|
55
|
my $self = shift; |
377
|
43
|
|
33
|
|
|
100
|
my $q = shift || $self->{questions}[$self->{qi}]; |
378
|
|
|
|
|
|
|
|
379
|
43
|
|
|
|
|
130
|
local $| = 1; |
380
|
|
|
|
|
|
|
|
381
|
43
|
50
|
33
|
|
|
126
|
croak "prompt() called incorrectly" |
382
|
|
|
|
|
|
|
unless defined $q->msg && defined $q->name; |
383
|
|
|
|
|
|
|
|
384
|
43
|
|
|
|
|
108
|
my $def = $self->get_default; |
385
|
43
|
50
|
|
|
|
131
|
my $dispdef = defined $def ? " [$def] " : " "; |
386
|
43
|
50
|
|
|
|
77
|
$def = defined $def ? $def : ""; |
387
|
|
|
|
|
|
|
|
388
|
43
|
|
|
|
|
123
|
print $q->msg . $dispdef; |
389
|
|
|
|
|
|
|
|
390
|
43
|
|
|
|
|
89
|
my $ans = ''; |
391
|
43
|
|
33
|
|
|
121
|
my $ISA_TTY = -t STDIN && (-t STDOUT || !(-f STDOUT || -c STDOUT)); # Pipe? |
392
|
|
|
|
|
|
|
|
393
|
43
|
50
|
|
|
|
76
|
if ( $ISA_TTY ) { |
394
|
0
|
0
|
0
|
|
|
0
|
if ( $Term::ReadKey::VERSION && $q->noecho ) { |
395
|
0
|
|
|
|
|
0
|
ReadMode( 'noecho' ); |
396
|
0
|
|
|
|
|
0
|
chomp( $ans = ReadLine(0) ); |
397
|
0
|
|
|
|
|
0
|
ReadMode( 'normal' ); |
398
|
0
|
|
|
|
|
0
|
print "\n"; |
399
|
|
|
|
|
|
|
} else { |
400
|
0
|
|
|
|
|
0
|
chomp( $ans = ); |
401
|
|
|
|
|
|
|
} |
402
|
|
|
|
|
|
|
} else { |
403
|
43
|
|
|
|
|
4409
|
print "$def\n"; |
404
|
|
|
|
|
|
|
} |
405
|
|
|
|
|
|
|
|
406
|
43
|
50
|
|
|
|
341
|
return $ans ne '' ? $ans : $def; |
407
|
|
|
|
|
|
|
} |
408
|
|
|
|
|
|
|
|
409
|
|
|
|
|
|
|
# Question index |
410
|
|
|
|
|
|
|
sub qi { |
411
|
7
|
|
|
7
|
1
|
14
|
my $self = shift; |
412
|
7
|
50
|
|
|
|
66
|
$self->{qi} = shift if @_; |
413
|
7
|
|
|
|
|
33
|
$self->{qi}; |
414
|
|
|
|
|
|
|
} |
415
|
|
|
|
|
|
|
|
416
|
|
|
|
|
|
|
#------------------------------------------------------------ |
417
|
|
|
|
|
|
|
# Docs |
418
|
|
|
|
|
|
|
#------------------------------------------------------------ |
419
|
|
|
|
|
|
|
|
420
|
|
|
|
|
|
|
=head1 NAME |
421
|
|
|
|
|
|
|
|
422
|
|
|
|
|
|
|
Module::TestConfig - Interactively prompt user to generate a config module |
423
|
|
|
|
|
|
|
|
424
|
|
|
|
|
|
|
=head1 SYNOPSIS |
425
|
|
|
|
|
|
|
|
426
|
|
|
|
|
|
|
use Module::TestConfig; |
427
|
|
|
|
|
|
|
|
428
|
|
|
|
|
|
|
Module::TestConfig->new( |
429
|
|
|
|
|
|
|
verbose => 1, |
430
|
|
|
|
|
|
|
defaults => 'defaults.config', |
431
|
|
|
|
|
|
|
file => 'MyConfig.pm', |
432
|
|
|
|
|
|
|
package => 'MyConfig', |
433
|
|
|
|
|
|
|
order => [ qw/defaults env/ ], |
434
|
|
|
|
|
|
|
questions => [ |
435
|
|
|
|
|
|
|
[ 'Would you like tea?' => 'tea', 'y' ], |
436
|
|
|
|
|
|
|
[ 'Crumpets?' => 'crumpets', 'y' ], |
437
|
|
|
|
|
|
|
] |
438
|
|
|
|
|
|
|
)->ask->save; |
439
|
|
|
|
|
|
|
|
440
|
|
|
|
|
|
|
# and in another module or test file: |
441
|
|
|
|
|
|
|
|
442
|
|
|
|
|
|
|
use MyConfig; |
443
|
|
|
|
|
|
|
|
444
|
|
|
|
|
|
|
my $config = MyConfig->new; |
445
|
|
|
|
|
|
|
print $config->tea eq 'y' |
446
|
|
|
|
|
|
|
? "We're having tea today; splendid!" |
447
|
|
|
|
|
|
|
: "No tea, I'm afraid. P'raps tomorrow."; |
448
|
|
|
|
|
|
|
|
449
|
|
|
|
|
|
|
print $config->crumpets eq 'y' |
450
|
|
|
|
|
|
|
? "Crumpets; lovely!" |
451
|
|
|
|
|
|
|
: "Alas, we have no crumpets today"; |
452
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
|
454
|
|
|
|
|
|
|
=head1 DESCRIPTION |
455
|
|
|
|
|
|
|
|
456
|
|
|
|
|
|
|
This module prompts a user for info and writes a module for later use. |
457
|
|
|
|
|
|
|
You can use it during the module build process (e.g. perl Makefile.PL) |
458
|
|
|
|
|
|
|
to share info among your test files. You can also use it to install |
459
|
|
|
|
|
|
|
that module into your site_perl. |
460
|
|
|
|
|
|
|
|
461
|
|
|
|
|
|
|
Module::TestConfig writes an object-oriented file. You specify |
462
|
|
|
|
|
|
|
the file's location as well as the package name. When you use() |
463
|
|
|
|
|
|
|
the file, each of the questions' names will become an object method. |
464
|
|
|
|
|
|
|
|
465
|
|
|
|
|
|
|
For example, if you asked the questions: |
466
|
|
|
|
|
|
|
|
467
|
|
|
|
|
|
|
Module::TestConfig->new( |
468
|
|
|
|
|
|
|
file => 't/MyConfig.pm', |
469
|
|
|
|
|
|
|
package => 'MyConfig', |
470
|
|
|
|
|
|
|
questions => [ |
471
|
|
|
|
|
|
|
[ 'Can you feel that bump?', 'feel', 'n' ], |
472
|
|
|
|
|
|
|
[ 'Would you like another?', 'another', 'y' ], |
473
|
|
|
|
|
|
|
{ msg => 'How many fingers am I holding up?', |
474
|
|
|
|
|
|
|
name => 'fingers', |
475
|
|
|
|
|
|
|
default => 11, |
476
|
|
|
|
|
|
|
}, |
477
|
|
|
|
|
|
|
], |
478
|
|
|
|
|
|
|
)->ask->save; |
479
|
|
|
|
|
|
|
|
480
|
|
|
|
|
|
|
You'd see something like this: |
481
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
Can you feel that bump? [n] y |
483
|
|
|
|
|
|
|
Would you like another? [y] n |
484
|
|
|
|
|
|
|
How many fingers am I holding up? [11] |
485
|
|
|
|
|
|
|
Module::TestConfig saved t/MyConfig.pm with these settings: |
486
|
|
|
|
|
|
|
=================== |
487
|
|
|
|
|
|
|
| Name | Value | |
488
|
|
|
|
|
|
|
=================== |
489
|
|
|
|
|
|
|
| feel | y | |
490
|
|
|
|
|
|
|
| another | n | |
491
|
|
|
|
|
|
|
| fingers | 11 | |
492
|
|
|
|
|
|
|
+---------+-------+ |
493
|
|
|
|
|
|
|
|
494
|
|
|
|
|
|
|
...and the file t/MyConfig.pm was written. To use it, add this to |
495
|
|
|
|
|
|
|
another file: |
496
|
|
|
|
|
|
|
|
497
|
|
|
|
|
|
|
use MyConfig; |
498
|
|
|
|
|
|
|
|
499
|
|
|
|
|
|
|
my $config = MyConfig->new; |
500
|
|
|
|
|
|
|
print $config->fingers; # prints 11 |
501
|
|
|
|
|
|
|
print $config->feel; # prints 'y' |
502
|
|
|
|
|
|
|
print $config->another; # prints 'n' |
503
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
=head1 PUBLIC METHODS |
505
|
|
|
|
|
|
|
|
506
|
|
|
|
|
|
|
=over 2 |
507
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
=item new() |
509
|
|
|
|
|
|
|
|
510
|
|
|
|
|
|
|
Args and defaults: |
511
|
|
|
|
|
|
|
|
512
|
|
|
|
|
|
|
verbose => 1, |
513
|
|
|
|
|
|
|
defaults => 'defaults.config', |
514
|
|
|
|
|
|
|
file => 'MyConfig.pm', |
515
|
|
|
|
|
|
|
package => 'MyConfig', |
516
|
|
|
|
|
|
|
order => [ 'defaults' ], |
517
|
|
|
|
|
|
|
questions => [ ... ], |
518
|
|
|
|
|
|
|
|
519
|
|
|
|
|
|
|
Returns: a new Module::TestConfig object. |
520
|
|
|
|
|
|
|
|
521
|
|
|
|
|
|
|
=item questions() |
522
|
|
|
|
|
|
|
|
523
|
|
|
|
|
|
|
Set up the questions that we'll ask of the user. This is a list (or |
524
|
|
|
|
|
|
|
array) of questions. Each question can be in one of two forms, the |
525
|
|
|
|
|
|
|
array form or the hash form. See L for |
526
|
|
|
|
|
|
|
more about the question's arguments. |
527
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
Args: |
529
|
|
|
|
|
|
|
an array of question hashes (or question arrays) |
530
|
|
|
|
|
|
|
a list of question hashes (or question arrays) |
531
|
|
|
|
|
|
|
|
532
|
|
|
|
|
|
|
e.g. (to keep it simple, I'll only give an example of a hash-style |
533
|
|
|
|
|
|
|
question): |
534
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
[ |
536
|
|
|
|
|
|
|
{ question => "Question to ask:", |
537
|
|
|
|
|
|
|
name => "foo", |
538
|
|
|
|
|
|
|
default => 42, |
539
|
|
|
|
|
|
|
noecho => 0, |
540
|
|
|
|
|
|
|
skip => sub { shift->answer('bar') }, # skip if bar is true |
541
|
|
|
|
|
|
|
validate => { regex => /^\d+$/ }, # answer must be all numbers |
542
|
|
|
|
|
|
|
}, |
543
|
|
|
|
|
|
|
... |
544
|
|
|
|
|
|
|
] |
545
|
|
|
|
|
|
|
|
546
|
|
|
|
|
|
|
Returns: |
547
|
|
|
|
|
|
|
a list of questions in list context |
548
|
|
|
|
|
|
|
an arrayref of questions in scalar context. |
549
|
|
|
|
|
|
|
|
550
|
|
|
|
|
|
|
Here's an overview of the hash-style arguments to set up a question. |
551
|
|
|
|
|
|
|
See L for more details. |
552
|
|
|
|
|
|
|
|
553
|
|
|
|
|
|
|
Args: |
554
|
|
|
|
|
|
|
|
555
|
|
|
|
|
|
|
=over 4 |
556
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
=item question or msg: |
558
|
|
|
|
|
|
|
|
559
|
|
|
|
|
|
|
A string like "Have you seen my eggplant?" or "Kittens:". They look |
560
|
|
|
|
|
|
|
best when they end with a ':' or a '?'. |
561
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
=item name: |
563
|
|
|
|
|
|
|
|
564
|
|
|
|
|
|
|
A simple mnemonic used for looking up values later. Since it will turn |
565
|
|
|
|
|
|
|
into a method name in the future, it ought to match /\w+/. |
566
|
|
|
|
|
|
|
|
567
|
|
|
|
|
|
|
=item default or def: |
568
|
|
|
|
|
|
|
|
569
|
|
|
|
|
|
|
optional. a default answer. |
570
|
|
|
|
|
|
|
|
571
|
|
|
|
|
|
|
=item noecho: |
572
|
|
|
|
|
|
|
|
573
|
|
|
|
|
|
|
optional. 1 or 0. Do we print the user's answer while they are typing? |
574
|
|
|
|
|
|
|
This is useful when asking for for passwords. |
575
|
|
|
|
|
|
|
|
576
|
|
|
|
|
|
|
=item skip: |
577
|
|
|
|
|
|
|
|
578
|
|
|
|
|
|
|
optional. Accepts either a coderef or a scalar. The Module::TestConfig |
579
|
|
|
|
|
|
|
object will be passed to the coderef as its first and only |
580
|
|
|
|
|
|
|
argument. Use it to look up answer()s. If the coderef returns true or |
581
|
|
|
|
|
|
|
the scalar is true, the current question will be skipped. |
582
|
|
|
|
|
|
|
|
583
|
|
|
|
|
|
|
=item validate: |
584
|
|
|
|
|
|
|
|
585
|
|
|
|
|
|
|
optional. A hashref suitable for Params::Validate::validate_pos(). |
586
|
|
|
|
|
|
|
See L. The question will loop over and over until |
587
|
|
|
|
|
|
|
the user types in a correct answer - or at least one that validates. |
588
|
|
|
|
|
|
|
After the user tries and fails 10 times, Module::TestConfig will give |
589
|
|
|
|
|
|
|
up and skip the question. |
590
|
|
|
|
|
|
|
|
591
|
|
|
|
|
|
|
e.g. |
592
|
|
|
|
|
|
|
|
593
|
|
|
|
|
|
|
Module::TestConfig->new( |
594
|
|
|
|
|
|
|
questions => [ { question => 'Choose any integer: ', |
595
|
|
|
|
|
|
|
name => 'num', |
596
|
|
|
|
|
|
|
default => 0, |
597
|
|
|
|
|
|
|
validate => { regex => qr/^\d+$/ }, |
598
|
|
|
|
|
|
|
}, |
599
|
|
|
|
|
|
|
{ question => 'Pick an int between 1 and 10: ', |
600
|
|
|
|
|
|
|
name => 'guess', |
601
|
|
|
|
|
|
|
default => 5, |
602
|
|
|
|
|
|
|
validate => { |
603
|
|
|
|
|
|
|
callbacks => { |
604
|
|
|
|
|
|
|
'1 <= guess <= 10', |
605
|
|
|
|
|
|
|
sub { my $n = shift; |
606
|
|
|
|
|
|
|
return unless $n =~ /^\d+$/; |
607
|
|
|
|
|
|
|
return if $n < 1 || $n > 10; |
608
|
|
|
|
|
|
|
return 1; |
609
|
|
|
|
|
|
|
}, |
610
|
|
|
|
|
|
|
} |
611
|
|
|
|
|
|
|
} |
612
|
|
|
|
|
|
|
}, |
613
|
|
|
|
|
|
|
] |
614
|
|
|
|
|
|
|
)->ask->save; |
615
|
|
|
|
|
|
|
|
616
|
|
|
|
|
|
|
would behave like this when run: |
617
|
|
|
|
|
|
|
|
618
|
|
|
|
|
|
|
Pick a number, any integer: [0] peach |
619
|
|
|
|
|
|
|
Your answer didn't validate. |
620
|
|
|
|
|
|
|
The 'num' parameter did not pass regex check |
621
|
|
|
|
|
|
|
Please try again. [Attempt 1] |
622
|
|
|
|
|
|
|
|
623
|
|
|
|
|
|
|
Pick a number, any integer: [0] plum |
624
|
|
|
|
|
|
|
Your answer didn't validate. |
625
|
|
|
|
|
|
|
The 'num' parameter did not pass regex check |
626
|
|
|
|
|
|
|
Please try again. [Attempt 2] |
627
|
|
|
|
|
|
|
|
628
|
|
|
|
|
|
|
Pick a number, any integer: [0] 5 |
629
|
|
|
|
|
|
|
Pick an integer between 1 and 10: [5] 12 |
630
|
|
|
|
|
|
|
Your answer didn't validate. |
631
|
|
|
|
|
|
|
The 'guess' parameter did not pass the '1 <= guess <= 10' callback |
632
|
|
|
|
|
|
|
Please try again. [Attempt 1] |
633
|
|
|
|
|
|
|
|
634
|
|
|
|
|
|
|
Pick an integer between 1 and 10: [5] -1 |
635
|
|
|
|
|
|
|
Your answer didn't validate. |
636
|
|
|
|
|
|
|
The 'guess' parameter did not pass the '1 <= guess <= 10' callback |
637
|
|
|
|
|
|
|
Please try again. [Attempt 2] |
638
|
|
|
|
|
|
|
|
639
|
|
|
|
|
|
|
Pick an integer between 1 and 10: [5] 3 |
640
|
|
|
|
|
|
|
Module::TestConfig saved MyConfig.pm with these settings: |
641
|
|
|
|
|
|
|
================= |
642
|
|
|
|
|
|
|
| Name | Value | |
643
|
|
|
|
|
|
|
================= |
644
|
|
|
|
|
|
|
| num | 5 | |
645
|
|
|
|
|
|
|
| guess | 3 | |
646
|
|
|
|
|
|
|
+-------+-------+ |
647
|
|
|
|
|
|
|
|
648
|
|
|
|
|
|
|
=back |
649
|
|
|
|
|
|
|
|
650
|
|
|
|
|
|
|
=item ask() |
651
|
|
|
|
|
|
|
|
652
|
|
|
|
|
|
|
Asks the user the questions. |
653
|
|
|
|
|
|
|
|
654
|
|
|
|
|
|
|
Returns: a Module::TestConfig object. |
655
|
|
|
|
|
|
|
|
656
|
|
|
|
|
|
|
=item save() |
657
|
|
|
|
|
|
|
|
658
|
|
|
|
|
|
|
Writes our answers to the file. The file created will always be |
659
|
|
|
|
|
|
|
0600 for security reasons. This may change in the future. See |
660
|
|
|
|
|
|
|
L<"SECURITY"> for more info. |
661
|
|
|
|
|
|
|
|
662
|
|
|
|
|
|
|
=item defaults() |
663
|
|
|
|
|
|
|
|
664
|
|
|
|
|
|
|
A file parsed by Config::Auto which may be used as default answers to |
665
|
|
|
|
|
|
|
the questions. See L<"order()"> |
666
|
|
|
|
|
|
|
|
667
|
|
|
|
|
|
|
Default: "defaults.config" |
668
|
|
|
|
|
|
|
|
669
|
|
|
|
|
|
|
Args: A filename or path. |
670
|
|
|
|
|
|
|
|
671
|
|
|
|
|
|
|
Returns: A filename or path. |
672
|
|
|
|
|
|
|
|
673
|
|
|
|
|
|
|
=item save_defaults() |
674
|
|
|
|
|
|
|
|
675
|
|
|
|
|
|
|
Writes a new defaults file. The key-value separator should be either |
676
|
|
|
|
|
|
|
':' or '=' to keep it compatible with L. See |
677
|
|
|
|
|
|
|
L<"order()"> for more about defaults. |
678
|
|
|
|
|
|
|
|
679
|
|
|
|
|
|
|
Args and defaults: |
680
|
|
|
|
|
|
|
|
681
|
|
|
|
|
|
|
file => $object->defaults, # 'defaults.config' |
682
|
|
|
|
|
|
|
sep => ':', |
683
|
|
|
|
|
|
|
|
684
|
|
|
|
|
|
|
Returns: 1 on success, undef on failure. Any error message can be |
685
|
|
|
|
|
|
|
found in $!. |
686
|
|
|
|
|
|
|
|
687
|
|
|
|
|
|
|
=item file() |
688
|
|
|
|
|
|
|
|
689
|
|
|
|
|
|
|
The filename that gets written during save(). |
690
|
|
|
|
|
|
|
|
691
|
|
|
|
|
|
|
Default: "MyConfig.pm" |
692
|
|
|
|
|
|
|
|
693
|
|
|
|
|
|
|
Args: a filepath that should probably end in ".pm". e.g. "t/MyConfig.pm" |
694
|
|
|
|
|
|
|
|
695
|
|
|
|
|
|
|
Returns: the filename or path. |
696
|
|
|
|
|
|
|
|
697
|
|
|
|
|
|
|
=item package() |
698
|
|
|
|
|
|
|
|
699
|
|
|
|
|
|
|
The file's package name written during save(). |
700
|
|
|
|
|
|
|
|
701
|
|
|
|
|
|
|
Default: "MyConfig" |
702
|
|
|
|
|
|
|
|
703
|
|
|
|
|
|
|
Args: a package namespace. e.g. "Foo::Bar::Baz" |
704
|
|
|
|
|
|
|
|
705
|
|
|
|
|
|
|
Returns: the set package. |
706
|
|
|
|
|
|
|
|
707
|
|
|
|
|
|
|
=item order() |
708
|
|
|
|
|
|
|
|
709
|
|
|
|
|
|
|
Default: [ 'defaults' ] |
710
|
|
|
|
|
|
|
|
711
|
|
|
|
|
|
|
Args: [ qw/defaults env/ ] |
712
|
|
|
|
|
|
|
[ qw/env defaults/ ] |
713
|
|
|
|
|
|
|
[ qw/defaults/ ] |
714
|
|
|
|
|
|
|
[ qw/env/ ] |
715
|
|
|
|
|
|
|
[ ] # Don't preload defaults from file or env |
716
|
|
|
|
|
|
|
|
717
|
|
|
|
|
|
|
Where do we look up defaults for the questions? They can come from |
718
|
|
|
|
|
|
|
either a file, the environment or perhaps come combination of both. |
719
|
|
|
|
|
|
|
|
720
|
|
|
|
|
|
|
=over 4 |
721
|
|
|
|
|
|
|
|
722
|
|
|
|
|
|
|
=item defaults: |
723
|
|
|
|
|
|
|
|
724
|
|
|
|
|
|
|
a file read by Config::Auto that must parse to a hash of question |
725
|
|
|
|
|
|
|
names and values. |
726
|
|
|
|
|
|
|
|
727
|
|
|
|
|
|
|
=item env: |
728
|
|
|
|
|
|
|
|
729
|
|
|
|
|
|
|
environment variables in either upper or lowercase. |
730
|
|
|
|
|
|
|
|
731
|
|
|
|
|
|
|
=back |
732
|
|
|
|
|
|
|
|
733
|
|
|
|
|
|
|
The following will also be checked, in order: |
734
|
|
|
|
|
|
|
|
735
|
|
|
|
|
|
|
=over 4 |
736
|
|
|
|
|
|
|
|
737
|
|
|
|
|
|
|
=item answers: |
738
|
|
|
|
|
|
|
|
739
|
|
|
|
|
|
|
Any answers already hard-set via answers(). |
740
|
|
|
|
|
|
|
|
741
|
|
|
|
|
|
|
=item a question's default: |
742
|
|
|
|
|
|
|
|
743
|
|
|
|
|
|
|
A default supplied with the question. |
744
|
|
|
|
|
|
|
|
745
|
|
|
|
|
|
|
=back |
746
|
|
|
|
|
|
|
|
747
|
|
|
|
|
|
|
There's a security risk accepting defaults from the environment or |
748
|
|
|
|
|
|
|
from a file. See L<"SECURITY">. |
749
|
|
|
|
|
|
|
|
750
|
|
|
|
|
|
|
=item answer() |
751
|
|
|
|
|
|
|
|
752
|
|
|
|
|
|
|
Get the current value of a question. Useful when paired with a skip |
753
|
|
|
|
|
|
|
or validate. |
754
|
|
|
|
|
|
|
|
755
|
|
|
|
|
|
|
Args: a question's name. |
756
|
|
|
|
|
|
|
|
757
|
|
|
|
|
|
|
Returns: The current value or undef. |
758
|
|
|
|
|
|
|
|
759
|
|
|
|
|
|
|
=item verbose() |
760
|
|
|
|
|
|
|
|
761
|
|
|
|
|
|
|
Should the module be chatty while running? Should it print a report at |
762
|
|
|
|
|
|
|
the end? |
763
|
|
|
|
|
|
|
|
764
|
|
|
|
|
|
|
Default: 1 |
765
|
|
|
|
|
|
|
|
766
|
|
|
|
|
|
|
Args: 1 or 0 |
767
|
|
|
|
|
|
|
|
768
|
|
|
|
|
|
|
Returns: current value |
769
|
|
|
|
|
|
|
|
770
|
|
|
|
|
|
|
=back |
771
|
|
|
|
|
|
|
|
772
|
|
|
|
|
|
|
=head1 PROTECTED METHODS |
773
|
|
|
|
|
|
|
|
774
|
|
|
|
|
|
|
These are documented primarily for subclassing. |
775
|
|
|
|
|
|
|
|
776
|
|
|
|
|
|
|
=over 2 |
777
|
|
|
|
|
|
|
|
778
|
|
|
|
|
|
|
=item answers() |
779
|
|
|
|
|
|
|
|
780
|
|
|
|
|
|
|
A user's questions get stored here. If you preset any answers before |
781
|
|
|
|
|
|
|
calling ask(), they may be used as defaults. See L<"order()"> for |
782
|
|
|
|
|
|
|
those rules. |
783
|
|
|
|
|
|
|
|
784
|
|
|
|
|
|
|
Args: a hash of question names and answers |
785
|
|
|
|
|
|
|
|
786
|
|
|
|
|
|
|
Returns: A hash in list context, a hashref in scalar context. |
787
|
|
|
|
|
|
|
|
788
|
|
|
|
|
|
|
=item prompt() |
789
|
|
|
|
|
|
|
|
790
|
|
|
|
|
|
|
Ask the user a question. It's your job to store the answer in |
791
|
|
|
|
|
|
|
the answers(). |
792
|
|
|
|
|
|
|
|
793
|
|
|
|
|
|
|
Args: $question, $default_answer, \%options |
794
|
|
|
|
|
|
|
|
795
|
|
|
|
|
|
|
Returns: the user's answer, a suitable default or ''. |
796
|
|
|
|
|
|
|
|
797
|
|
|
|
|
|
|
=item get_default() |
798
|
|
|
|
|
|
|
|
799
|
|
|
|
|
|
|
Get a question's default answer from env, file, answer() or the |
800
|
|
|
|
|
|
|
question. It's printed with a prompt()ed question. |
801
|
|
|
|
|
|
|
|
802
|
|
|
|
|
|
|
=item load_defaults() |
803
|
|
|
|
|
|
|
|
804
|
|
|
|
|
|
|
Uses Config::Auto to parse the file specified by defaults(). Defaults |
805
|
|
|
|
|
|
|
are stored in $obj-E{_defaults}. |
806
|
|
|
|
|
|
|
|
807
|
|
|
|
|
|
|
=item package_text() |
808
|
|
|
|
|
|
|
|
809
|
|
|
|
|
|
|
Returns the new file's text. This should take package() and |
810
|
|
|
|
|
|
|
answers() into account. |
811
|
|
|
|
|
|
|
|
812
|
|
|
|
|
|
|
=item qi() |
813
|
|
|
|
|
|
|
|
814
|
|
|
|
|
|
|
The current question index. |
815
|
|
|
|
|
|
|
|
816
|
|
|
|
|
|
|
Args: A number |
817
|
|
|
|
|
|
|
|
818
|
|
|
|
|
|
|
Returns: The current number |
819
|
|
|
|
|
|
|
|
820
|
|
|
|
|
|
|
=item report() |
821
|
|
|
|
|
|
|
|
822
|
|
|
|
|
|
|
Returns a report of question names and values. Will be printed if the |
823
|
|
|
|
|
|
|
object is in verbose mode. This calls report_pretty() or |
824
|
|
|
|
|
|
|
report_plain() depending on whether Text::AutoFormat is |
825
|
|
|
|
|
|
|
available. Won't print any passwords (questions called with C
|
826
|
|
|
|
|
|
|
=E 1> ) in plaintext. |
827
|
|
|
|
|
|
|
|
828
|
|
|
|
|
|
|
=item report_pretty() |
829
|
|
|
|
|
|
|
|
830
|
|
|
|
|
|
|
Prints the report like this: |
831
|
|
|
|
|
|
|
|
832
|
|
|
|
|
|
|
================== |
833
|
|
|
|
|
|
|
| Name | Value | |
834
|
|
|
|
|
|
|
================== |
835
|
|
|
|
|
|
|
| one | 1 | |
836
|
|
|
|
|
|
|
| two | 2 | |
837
|
|
|
|
|
|
|
| three | 3 | |
838
|
|
|
|
|
|
|
| fruit | kiwi | |
839
|
|
|
|
|
|
|
| meat | pork | |
840
|
|
|
|
|
|
|
| passwd | ***** | |
841
|
|
|
|
|
|
|
+--------+-------+ |
842
|
|
|
|
|
|
|
|
843
|
|
|
|
|
|
|
=item report_plain() |
844
|
|
|
|
|
|
|
|
845
|
|
|
|
|
|
|
Prints the report like this: |
846
|
|
|
|
|
|
|
|
847
|
|
|
|
|
|
|
one: 1 |
848
|
|
|
|
|
|
|
two: 2 |
849
|
|
|
|
|
|
|
three: 3 |
850
|
|
|
|
|
|
|
fruit: kiwi |
851
|
|
|
|
|
|
|
meat: pork |
852
|
|
|
|
|
|
|
passwd: ***** |
853
|
|
|
|
|
|
|
|
854
|
|
|
|
|
|
|
=back |
855
|
|
|
|
|
|
|
|
856
|
|
|
|
|
|
|
=head1 SECURITY |
857
|
|
|
|
|
|
|
|
858
|
|
|
|
|
|
|
The resultant file (MyConfig.pm by default) will be chmod'd to 0600 |
859
|
|
|
|
|
|
|
but it will remain on the system. |
860
|
|
|
|
|
|
|
|
861
|
|
|
|
|
|
|
The default action is to load a file named 'defaults.config' and use |
862
|
|
|
|
|
|
|
that for, well, defaults. If someone managed to put their own |
863
|
|
|
|
|
|
|
defaults.config into your working directory, they might be able to |
864
|
|
|
|
|
|
|
sneak a bad default past an unwitting user. |
865
|
|
|
|
|
|
|
|
866
|
|
|
|
|
|
|
Using the environment as input may be a security risk - or a potential |
867
|
|
|
|
|
|
|
bug - especially if your names mimic existing environment variables. |
868
|
|
|
|
|
|
|
|
869
|
|
|
|
|
|
|
=head1 AUTHOR |
870
|
|
|
|
|
|
|
|
871
|
|
|
|
|
|
|
Joshua Keroes Ejkeroes@eli.netE |
872
|
|
|
|
|
|
|
|
873
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
874
|
|
|
|
|
|
|
|
875
|
|
|
|
|
|
|
Copyright 2003 by Joshua Keroes Ejkeroes@eli.netE |
876
|
|
|
|
|
|
|
|
877
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
878
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
879
|
|
|
|
|
|
|
|
880
|
|
|
|
|
|
|
=head1 SEE ALSO |
881
|
|
|
|
|
|
|
|
882
|
|
|
|
|
|
|
L |
883
|
|
|
|
|
|
|
L |
884
|
|
|
|
|
|
|
L |
885
|
|
|
|
|
|
|
|
886
|
|
|
|
|
|
|
=cut |
887
|
|
|
|
|
|
|
|
888
|
|
|
|
|
|
|
# Template for module generation follows: |
889
|
|
|
|
|
|
|
|
890
|
|
|
|
|
|
|
__DATA__ |