line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Bot::Cobalt::Frontend::Utils; |
2
|
|
|
|
|
|
|
$Bot::Cobalt::Frontend::Utils::VERSION = '0.021003'; |
3
|
1
|
|
|
1
|
|
13677
|
use v5.10; |
|
1
|
|
|
|
|
2
|
|
4
|
1
|
|
|
1
|
|
383
|
use strictures 2; |
|
1
|
|
|
|
|
1119
|
|
|
1
|
|
|
|
|
30
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
158
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
70
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
350
|
use parent 'Exporter::Tiny'; |
|
1
|
|
|
|
|
227
|
|
|
1
|
|
|
|
|
5
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our @EXPORT_OK = qw/ |
11
|
|
|
|
|
|
|
ask_yesno |
12
|
|
|
|
|
|
|
ask_question |
13
|
|
|
|
|
|
|
/; |
14
|
|
|
|
|
|
|
our %EXPORT_TAGS; |
15
|
|
|
|
|
|
|
{ my %s; push @{$EXPORT_TAGS{all}}, grep {!$s{$_}++} @EXPORT_OK } |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub ask_question { |
19
|
1
|
|
|
1
|
1
|
1248
|
my %args = @_; |
20
|
|
|
|
|
|
|
|
21
|
1
|
|
33
|
|
|
4
|
my $question = delete $args{prompt} || croak "No prompt => specified"; |
22
|
1
|
|
|
|
|
2
|
my $default = delete $args{default}; |
23
|
|
|
|
|
|
|
|
24
|
1
|
|
|
|
|
2
|
my $validate_sub; |
25
|
1
|
50
|
|
|
|
3
|
if (defined $args{validate}) { |
26
|
|
|
|
|
|
|
$validate_sub = ref $args{validate} eq 'CODE' ? |
27
|
|
|
|
|
|
|
delete $args{validate} |
28
|
1
|
50
|
|
|
|
5
|
: croak "validate => should be a coderef"; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
1
|
|
|
|
|
3
|
select(STDOUT); $|++; |
|
1
|
|
|
|
|
1
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $print_and_grab = sub { |
34
|
1
|
50
|
|
1
|
|
31
|
print "$question ", defined $default ? "[$default] " : "> "; |
35
|
1
|
|
|
|
|
16
|
my $ret = ; chomp($ret); |
|
1
|
|
|
|
|
1
|
|
36
|
1
|
50
|
33
|
|
|
4
|
$ret = $default if defined $default and $ret eq ''; |
37
|
1
|
|
|
|
|
2
|
$ret |
38
|
1
|
|
|
|
|
4
|
}; |
39
|
|
|
|
|
|
|
|
40
|
1
|
|
|
|
|
2
|
my $input = $print_and_grab->(); |
41
|
1
|
|
|
|
|
4
|
until (length $input) { |
42
|
0
|
|
|
|
|
0
|
print "No input specified.\n"; |
43
|
0
|
|
|
|
|
0
|
$input = $print_and_grab->() |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
VALID: { |
47
|
1
|
50
|
|
|
|
1
|
if ($validate_sub) { |
|
1
|
|
|
|
|
3
|
|
48
|
1
|
|
|
|
|
3
|
my $invalid = $validate_sub->($input); |
49
|
1
|
50
|
|
|
|
8
|
last VALID unless defined $invalid; |
50
|
|
|
|
|
|
|
die "Invalid input; $invalid\n" |
51
|
0
|
0
|
0
|
|
|
0
|
if $args{die_if_invalid} or $args{die_unless_valid}; |
52
|
0
|
|
|
|
|
0
|
until (not defined $invalid) { |
53
|
0
|
|
|
|
|
0
|
print "Invalid input; $invalid\n"; |
54
|
0
|
|
|
|
|
0
|
$print_and_grab->(); |
55
|
|
|
|
|
|
|
redo VALID |
56
|
0
|
|
|
|
|
0
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
1
|
|
|
|
|
7
|
return $input |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub ask_yesno { |
64
|
4
|
|
|
4
|
1
|
1649
|
my %args = @_; |
65
|
4
|
|
33
|
|
|
10
|
my $question = $args{prompt} || croak "No prompt => specified"; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
my $default = lc( |
68
|
4
|
|
33
|
|
|
15
|
substr($args{default}||'', 0, 1) || croak "No default => specified" |
69
|
|
|
|
|
|
|
); |
70
|
|
|
|
|
|
|
|
71
|
4
|
50
|
|
|
|
12
|
croak "default should be Y or N" |
72
|
|
|
|
|
|
|
unless $default =~ /^[yn]$/; |
73
|
|
|
|
|
|
|
|
74
|
4
|
100
|
|
|
|
9
|
my $yn = $default eq 'y' ? 'Y/n' : 'y/N' ; |
75
|
|
|
|
|
|
|
|
76
|
4
|
|
|
|
|
9
|
select(STDOUT); $|++; |
|
4
|
|
|
|
|
6
|
|
77
|
|
|
|
|
|
|
|
78
|
4
|
|
|
|
|
3
|
my $input; |
79
|
|
|
|
|
|
|
my $print_and_grab = sub { |
80
|
4
|
|
|
4
|
|
128
|
print "$question [$yn] "; |
81
|
4
|
|
|
|
|
12
|
$input = ; chomp($input); |
|
4
|
|
|
|
|
5
|
|
82
|
4
|
50
|
|
|
|
8
|
$input = $default if $input eq ''; |
83
|
4
|
50
|
|
|
|
13
|
lc substr $input eq '' ? $default : $input, 0, 1 |
84
|
4
|
|
|
|
|
13
|
}; |
85
|
4
|
|
|
|
|
6
|
$print_and_grab->(); |
86
|
4
|
|
66
|
|
|
24
|
until ($input && $input eq 'y' || $input eq 'n') { |
|
|
|
66
|
|
|
|
|
87
|
0
|
|
|
|
|
0
|
print "Invalid input; should be either Y or N\n"; |
88
|
0
|
|
|
|
|
0
|
$print_and_grab->(); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
4
|
100
|
|
|
|
29
|
$input eq 'y' ? 1 : 0 |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
1; |
95
|
|
|
|
|
|
|
__END__ |