| 1 | package Bot::Cobalt::Utils; | ||||||||||||||||||||||||||||
| 2 | 16 | $Bot::Cobalt::Utils::VERSION = '0.021003'; | |||||||||||||||||||||||||||
| 3 | 16 | ▶ | use strictures 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 16 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 16 | ▶ | use Scalar::Util 'reftype'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 16 | ▶ | use App::bmkpasswd (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 16 | ▶ | use parent 'Exporter::Tiny'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 16 | our @EXPORT_OK = qw/ | |||||||||||||||||||||||||||
| 12 | secs_to_str | ||||||||||||||||||||||||||||
| 13 | secs_to_str_y | ||||||||||||||||||||||||||||
| 14 | secs_to_timestr | ||||||||||||||||||||||||||||
| 15 | timestr_to_secs | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | mkpasswd | ||||||||||||||||||||||||||||
| 18 | passwdcmp | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | color | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | glob_grep | ||||||||||||||||||||||||||||
| 23 | glob_to_re | ||||||||||||||||||||||||||||
| 24 | glob_to_re_str | ||||||||||||||||||||||||||||
| 25 | rplprintf | ||||||||||||||||||||||||||||
| 26 | /; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 16 | our %EXPORT_TAGS = ( | |||||||||||||||||||||||||||
| 29 | ALL => [ @EXPORT_OK ], | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | ## codes mostly borrowed from IRC::Utils | ||||||||||||||||||||||||||||
| 34 | 16 | our %COLORS = ( | |||||||||||||||||||||||||||
| 35 | NORMAL => "\x0f", | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | BOLD => "\x02", | ||||||||||||||||||||||||||||
| 38 | UNDERLINE => "\x1f", | ||||||||||||||||||||||||||||
| 39 | REVERSE => "\x16", | ||||||||||||||||||||||||||||
| 40 | ITALIC => "\x1d", | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | WHITE => "\x0300", | ||||||||||||||||||||||||||||
| 43 | BLACK => "\x0301", | ||||||||||||||||||||||||||||
| 44 | BLUE => "\x0302", | ||||||||||||||||||||||||||||
| 45 | GREEN => "\x0303", | ||||||||||||||||||||||||||||
| 46 | RED => "\x0304", | ||||||||||||||||||||||||||||
| 47 | BROWN => "\x0305", | ||||||||||||||||||||||||||||
| 48 | PURPLE => "\x0306", | ||||||||||||||||||||||||||||
| 49 | ORANGE => "\x0307", | ||||||||||||||||||||||||||||
| 50 | YELLOW => "\x0308", | ||||||||||||||||||||||||||||
| 51 | TEAL => "\x0310", | ||||||||||||||||||||||||||||
| 52 | PINK => "\x0313", | ||||||||||||||||||||||||||||
| 53 | GREY => "\x0314", | ||||||||||||||||||||||||||||
| 54 | GRAY => "\x0314", | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | LIGHT_BLUE => "\x0312", | ||||||||||||||||||||||||||||
| 57 | LIGHT_CYAN => "\x0311", | ||||||||||||||||||||||||||||
| 58 | LIGHT_GREEN => "\x0309", | ||||||||||||||||||||||||||||
| 59 | LIGHT_GRAY => "\x0315", | ||||||||||||||||||||||||||||
| 60 | LIGHT_GREY => "\x0315", | ||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 16 | my %default_fmt_vars; | |||||||||||||||||||||||||||
| 64 | 16 | for my $color (keys %COLORS) { | |||||||||||||||||||||||||||
| 65 | 368 | my $fmtvar = 'C_'.$color; | |||||||||||||||||||||||||||
| 66 | 368 | $default_fmt_vars{$fmtvar} = $COLORS{$color}; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | ## String formatting, usually for langsets: | ||||||||||||||||||||||||||||
| 70 | 5 | ▶ | sub rplprintf { | ||||||||||||||||||||||||||
|
Subroutine
rplprintf: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 5 | my $string = shift; | |||||||||||||||||||||||||||
| 72 | 5 | ▶ | return '' unless $string; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | ## rplprintf( $string, $vars ) | ||||||||||||||||||||||||||||
| 75 | ## returns empty string if no string is specified. | ||||||||||||||||||||||||||||
| 76 | ## | ||||||||||||||||||||||||||||
| 77 | ## variables can be terminated with % or a space: | ||||||||||||||||||||||||||||
| 78 | ## rplprintf( "Error for %user%: %err") | ||||||||||||||||||||||||||||
| 79 | ## | ||||||||||||||||||||||||||||
| 80 | ## used for formatting lang RPLs | ||||||||||||||||||||||||||||
| 81 | ## $vars should be a hash keyed by variable, f.ex: | ||||||||||||||||||||||||||||
| 82 | ## 'user' => $username, | ||||||||||||||||||||||||||||
| 83 | ## 'err' => $error, | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 5 | my %vars; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 5 | ▶ | if (@_ > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 1 | my %args = @_; | |||||||||||||||||||||||||||
| 89 | 1 | %vars = ( %default_fmt_vars, %args ); | |||||||||||||||||||||||||||
| 90 | } else { | ||||||||||||||||||||||||||||
| 91 | 4 | ▶ | if (reftype $_[0] eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 4 | %vars = ( %default_fmt_vars, %{$_[0]} ); | |||||||||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||||||||||
| 94 | 0 | confess "rplprintf() expects a hash" | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 18 | ▶ | my $repl = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 99 | ## _repl($1, $2, $vars) | ||||||||||||||||||||||||||||
| 100 | 18 | my ($orig, $match) = @_; | |||||||||||||||||||||||||||
| 101 | 18 | ▶ | defined $vars{$match} ? $vars{$match} : $orig | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 5 | }; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 5 | my $regex = qr/(%([^\s%]+)%?)/; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 5 | $string =~ s/$regex/$repl->($1, $2)/ge; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 5 | $string | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | ## Glob -> regex functions: | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 4 | ▶ | sub glob_grep ($;@) { | ||||||||||||||||||||||||||
|
Subroutine
glob_grep: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | 4 | my $glob = shift; | |||||||||||||||||||||||||||
| 116 | 4 | ▶ | confess "glob_grep called with no arguments!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | unless defined $glob; | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 4 | ▶ | my @array = ref $_[0] eq 'ARRAY' ? @{$_[0]} : @_ ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 4 | my $re = glob_to_re($glob); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 4 | grep { m/$re/ } @array | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 6 | ▶ | sub glob_to_re ($) { | ||||||||||||||||||||||||||
|
Subroutine
glob_to_re: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 127 | 6 | my ($glob) = @_; | |||||||||||||||||||||||||||
| 128 | 6 | ▶ | confess "glob_to_re called with no arguments!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | unless defined $glob; | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 6 | my $re = glob_to_re_str($glob); | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 6 | qr/$re/ | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 8 | ▶ | sub glob_to_re_str ($) { | ||||||||||||||||||||||||||
|
Subroutine
glob_to_re_str: called CC=20 SCAR=33.9
Pod
documented
| |||||||||||||||||||||||||||||
| 137 | ## Currently allows: | ||||||||||||||||||||||||||||
| 138 | ## * == .* | ||||||||||||||||||||||||||||
| 139 | ## ? == . | ||||||||||||||||||||||||||||
| 140 | ## + == literal space | ||||||||||||||||||||||||||||
| 141 | ## leading ^ (beginning of str) is accepted | ||||||||||||||||||||||||||||
| 142 | ## so is trailing $ | ||||||||||||||||||||||||||||
| 143 | ## char classes are accepted | ||||||||||||||||||||||||||||
| 144 | 8 | my ($glob) = @_; | |||||||||||||||||||||||||||
| 145 | 8 | ▶ | confess "glob_to_re_str called with no arguments!" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | unless defined $glob; | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 8 | my($re, $in_esc); | |||||||||||||||||||||||||||
| 149 | 8 | my ($first, $pos) = (1, 0); | |||||||||||||||||||||||||||
| 150 | 8 | my @chars = split '', $glob; | |||||||||||||||||||||||||||
| 151 | 8 | for my $ch (@chars) { | |||||||||||||||||||||||||||
| 152 | 82 | ++$pos; | |||||||||||||||||||||||||||
| 153 | 82 | ▶ | my $last = 1 if $pos == @chars; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | ## Leading ^ (start) is OK: | ||||||||||||||||||||||||||||
| 156 | 82 | ▶ | if ($first) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 157 | 11 | ▶ | if ($ch eq '^') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 3 | $re .= '^' ; | |||||||||||||||||||||||||||
| 159 | next | ||||||||||||||||||||||||||||
| 160 | 3 | } | |||||||||||||||||||||||||||
| 161 | 8 | $first = 0; | |||||||||||||||||||||||||||
| 162 | ## So is trailing $ (end): | ||||||||||||||||||||||||||||
| 163 | ▶ | } elsif ($last && $ch eq '$') { | |||||||||||||||||||||||||||
|
Condition100%3/3
$last and $ch eq "\$"
MC/DC100%2/2
$last and $ch eq "\$"
$last
$ch eq "\$"
Truth table100%3/3
A and B
A= $lastB= $ch eq "\$"
| |||||||||||||||||||||||||||||
| 164 | 3 | $re .= '$' ; | |||||||||||||||||||||||||||
| 165 | last | ||||||||||||||||||||||||||||
| 166 | 3 | } | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | ## Escape metas: | ||||||||||||||||||||||||||||
| 169 | 76 | ▶ | if (grep { $_ eq $ch } qw/ . ( ) | ^ $ @ % { } /) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 0 | $re .= "\\$ch" ; | |||||||||||||||||||||||||||
| 171 | 0 | $in_esc = 0; | |||||||||||||||||||||||||||
| 172 | next | ||||||||||||||||||||||||||||
| 173 | 0 | } | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | ## Handle * ? + wildcards: | ||||||||||||||||||||||||||||
| 176 | 76 | ▶ | if ($ch eq '*') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 6 | ▶ | $re .= $in_esc ? '\*' : '.*' ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 6 | $in_esc = 0; | |||||||||||||||||||||||||||
| 179 | next | ||||||||||||||||||||||||||||
| 180 | 6 | } | |||||||||||||||||||||||||||
| 181 | 70 | ▶ | if ($ch eq '?') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 2 | ▶ | $re .= $in_esc ? '\?' : '.' ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 2 | $in_esc = 0; | |||||||||||||||||||||||||||
| 184 | next | ||||||||||||||||||||||||||||
| 185 | 2 | } | |||||||||||||||||||||||||||
| 186 | 68 | ▶ | if ($ch eq '+') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 3 | ▶ | $re .= $in_esc ? '\+' : '\s' ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 3 | $in_esc = 0; | |||||||||||||||||||||||||||
| 189 | next | ||||||||||||||||||||||||||||
| 190 | 3 | } | |||||||||||||||||||||||||||
| 191 | 65 | ▶ | if ( $ch eq '[' || $ch eq ']' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ch eq "[" or $ch eq "]"
MC/DC0%0/2
$ch eq "[" or $ch eq "]"
$ch eq "["
$ch eq "]"
Truth table33%1/3
A or B
A= $ch eq "["B= $ch eq "]"
| |||||||||||||||||||||||||||||
| 192 | 0 | ▶ | $re .= $in_esc ? "\\$ch" : $ch ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 0 | $in_esc = 0; | |||||||||||||||||||||||||||
| 194 | next | ||||||||||||||||||||||||||||
| 195 | 0 | } | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | ## Switch on/off escaping: | ||||||||||||||||||||||||||||
| 198 | 65 | ▶ | if ($ch eq "\\") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 0 | ▶ | if ($in_esc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 0 | $re .= "\\\\"; | |||||||||||||||||||||||||||
| 201 | 0 | $in_esc = 0; | |||||||||||||||||||||||||||
| 202 | 0 | } else { $in_esc = 1; } | |||||||||||||||||||||||||||
| 203 | next | ||||||||||||||||||||||||||||
| 204 | 0 | } | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 65 | $re .= $ch; | |||||||||||||||||||||||||||
| 207 | 65 | $in_esc = 0; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | $re | ||||||||||||||||||||||||||||
| 211 | 8 | } | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | ## IRC color codes: | ||||||||||||||||||||||||||||
| 215 | 4 | ▶ | sub color ($;$) { | ||||||||||||||||||||||||||
|
Subroutine
color: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 216 | ## color($format, $str) | ||||||||||||||||||||||||||||
| 217 | ## implements mirc formatting codes, against my better judgement | ||||||||||||||||||||||||||||
| 218 | ## if format is unspecified, returns NORMAL | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | ## interpolate bold, reset to NORMAL after: | ||||||||||||||||||||||||||||
| 221 | ## $str = color('bold') . "Text" . color; | ||||||||||||||||||||||||||||
| 222 | ## -or- | ||||||||||||||||||||||||||||
| 223 | ## format specified strings, resetting NORMAL after: | ||||||||||||||||||||||||||||
| 224 | ## $str = color('bold', "Some text"); # bold text ending in normal | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 4 | my ($format, $str) = @_; | |||||||||||||||||||||||||||
| 227 | 4 | ▶ | $format = uc($format||'normal'); | ||||||||||||||||||||||||||
|
Condition50%1/2
$format || 'normal'
MC/DC0%0/1
$format || 'normal'
$format
Truth table50%1/2
A
A= $format
| |||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 4 | my $selected = $COLORS{$format}; | |||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 4 | ▶ | carp "Invalid COLOR $format passed to color()" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | unless $selected; | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 4 | ▶ | return $selected . $str . $COLORS{NORMAL} if $str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 4 | ▶ | $selected || $COLORS{NORMAL}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | ## Time/date ops: | ||||||||||||||||||||||||||||
| 240 | 4 | ▶ | sub timestr_to_secs ($) { | ||||||||||||||||||||||||||
|
Subroutine
timestr_to_secs: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||
| 241 | ## turn something like 2h3m30s into seconds | ||||||||||||||||||||||||||||
| 242 | 4 | my ($timestr) = @_; | |||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 4 | ▶ | unless ($timestr) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | 0 | carp "timestr_to_secs() received a false value"; | |||||||||||||||||||||||||||
| 246 | 0 | return 0 | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | ## maybe just seconds: | ||||||||||||||||||||||||||||
| 250 | 4 | ▶ | return $timestr if $timestr =~ /^[0-9]+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 4 | my @chunks = $timestr =~ m/([0-9]+)([dhms])/gc; | |||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 4 | my $secs = 0; | |||||||||||||||||||||||||||
| 255 | 4 | ▶ | while ( my ($ti, $unit) = splice @chunks, 0, 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 256 | UNIT: { | ||||||||||||||||||||||||||||
| 257 | 6 | ▶ | if ($unit eq 'd') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | 1 | $secs += $ti * 86400; | |||||||||||||||||||||||||||
| 259 | last UNIT | ||||||||||||||||||||||||||||
| 260 | 1 | } | |||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 5 | ▶ | if ($unit eq 'h') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | 1 | $secs += $ti * 3600; | |||||||||||||||||||||||||||
| 264 | last UNIT | ||||||||||||||||||||||||||||
| 265 | 1 | } | |||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 4 | ▶ | if ($unit eq 'm') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | 2 | $secs += $ti * 60; | |||||||||||||||||||||||||||
| 269 | last UNIT | ||||||||||||||||||||||||||||
| 270 | 2 | } | |||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 2 | $secs += $ti; | |||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | $secs | ||||||||||||||||||||||||||||
| 277 | 4 | } | |||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | 8 | ▶ | sub _time_breakdown ($) { | ||||||||||||||||||||||||||
|
Subroutine
_time_breakdown: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 280 | 8 | my ($diff) = @_; | |||||||||||||||||||||||||||
| 281 | 8 | ▶ | return unless defined $diff; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 8 | my $days = int $diff / 86400; | |||||||||||||||||||||||||||
| 284 | 8 | my $sec = $diff % 86400; | |||||||||||||||||||||||||||
| 285 | 8 | my $hours = int $sec / 3600; | |||||||||||||||||||||||||||
| 286 | 8 | $sec %= 3600; | |||||||||||||||||||||||||||
| 287 | 8 | my $mins = int $sec / 60; | |||||||||||||||||||||||||||
| 288 | 8 | $sec %= 60; | |||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 8 | ($days, $hours, $mins, $sec) | |||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | 3 | ▶ | sub secs_to_timestr ($) { | ||||||||||||||||||||||||||
|
Subroutine
secs_to_timestr: called CC=6 SCAR=18.5
Pod
documented
| |||||||||||||||||||||||||||||
| 294 | 3 | my ($diff) = @_; | |||||||||||||||||||||||||||
| 295 | 3 | ▶ | return unless defined $diff; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 296 | 3 | my ($days, $hours, $mins, $sec) = _time_breakdown($diff); | |||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 3 | my $str; | |||||||||||||||||||||||||||
| 299 | 3 | ▶ | $str .= $days .'d' if $days; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 3 | ▶ | $str .= $hours .'h' if $hours; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 301 | 3 | ▶ | $str .= $mins .'m' if $mins; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 302 | 3 | ▶ | $str .= $sec .'s' if $sec; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 3 | $str | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 3 | ▶ | sub secs_to_str ($) { | ||||||||||||||||||||||||||
|
Subroutine
secs_to_str: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 308 | ## turn seconds into a string like '0 days, 00:00:00' | ||||||||||||||||||||||||||||
| 309 | 3 | my ($diff) = @_; | |||||||||||||||||||||||||||
| 310 | 3 | ▶ | return unless defined $diff; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 311 | 3 | my ($days, $hours, $mins, $sec) = _time_breakdown($diff); | |||||||||||||||||||||||||||
| 312 | 3 | ▶ | my $plural = $days == 1 ? 'day' : 'days'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 313 | 3 | sprintf "%d $plural, %2.2d:%2.2d:%2.2d", $days, $hours, $mins, $sec | |||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 2 | ▶ | sub secs_to_str_y { | ||||||||||||||||||||||||||
|
Subroutine
secs_to_str_y: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||
| 317 | 2 | my ($diff) = @_; | |||||||||||||||||||||||||||
| 318 | 2 | ▶ | return unless defined $diff; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 319 | 2 | my ($days, $hrs, $mins, $sec) = _time_breakdown($diff); | |||||||||||||||||||||||||||
| 320 | 2 | my $yrs = int $days / 365; | |||||||||||||||||||||||||||
| 321 | 2 | $days %= 365; | |||||||||||||||||||||||||||
| 322 | 2 | ▶ | my $plural_y = $yrs > 1 ? 'years' : 'year'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | 2 | ▶ | my $plural_d = $days == 1 ? 'day' : 'days'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 2 | ▶ | $yrs ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | sprintf "%d $plural_y, %d $plural_d, %2.2d:%2.2d:%2.2d", | ||||||||||||||||||||||||||||
| 326 | $yrs, $days, $hrs, $mins, $sec | ||||||||||||||||||||||||||||
| 327 | : sprintf "%d $plural_d, %2.2d:%2.2d:%2.2d", | ||||||||||||||||||||||||||||
| 328 | $days, $hrs, $mins, $sec | ||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | ## App::bmkpasswd stubs as of 00_35 | ||||||||||||||||||||||||||||
| 333 | 1 | ▶ | sub mkpasswd ($;@) { App::bmkpasswd::mkpasswd(@_) } | ||||||||||||||||||||||||||
|
Subroutine
mkpasswd: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 334 | 2 | ▶ | sub passwdcmp ($$) { App::bmkpasswd::passwdcmp(@_) } | ||||||||||||||||||||||||||
|
Subroutine
passwdcmp: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 16 | 1; | |||||||||||||||||||||||||||
| 337 | __END__ | ||||||||||||||||||||||||||||