| 1 | package XAS::Utils; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use DateTime; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use XAS::Exception; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use DateTime::Format::Pg; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Digest::MD5 'md5_hex'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Params::Validate ':all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use DateTime::Format::Strptime; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use POSIX qw(:sys_wait_h setsid); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | use XAS::Class | ||||||||||||||||||||||||||||
| 15 | 1 | debug => 0, | |||||||||||||||||||||||||||
| 16 | version => $VERSION, | ||||||||||||||||||||||||||||
| 17 | base => 'Badger::Utils XAS::Base', | ||||||||||||||||||||||||||||
| 18 | constants => 'HASH ARRAY', | ||||||||||||||||||||||||||||
| 19 | filesystem => 'Dir File', | ||||||||||||||||||||||||||||
| 20 | exports => { | ||||||||||||||||||||||||||||
| 21 | all => 'db2dt dt2db trim ltrim rtrim daemonize hash_walk | ||||||||||||||||||||||||||||
| 22 | load_module bool init_module load_module compress exitcode | ||||||||||||||||||||||||||||
| 23 | kill_proc spawn _do_fork glob2regex dir_walk | ||||||||||||||||||||||||||||
| 24 | env_store env_restore env_create env_parse env_dump | ||||||||||||||||||||||||||||
| 25 | left right mid instr is_truthy is_falsey run_cmd', | ||||||||||||||||||||||||||||
| 26 | any => 'db2dt dt2db trim ltrim rtrim daemonize hash_walk | ||||||||||||||||||||||||||||
| 27 | load_module bool init_module load_module compress exitcode | ||||||||||||||||||||||||||||
| 28 | kill_proc spawn _do_fork glob2regex dir_walk | ||||||||||||||||||||||||||||
| 29 | env_store env_restore env_create env_parse env_dump | ||||||||||||||||||||||||||||
| 30 | left right mid instr is_truthy is_falsey run_cmd', | ||||||||||||||||||||||||||||
| 31 | tags => { | ||||||||||||||||||||||||||||
| 32 | dates => 'db2dt dt2db', | ||||||||||||||||||||||||||||
| 33 | env => 'env_store env_restore env_create env_parse env_dump', | ||||||||||||||||||||||||||||
| 34 | modules => 'init_module load_module', | ||||||||||||||||||||||||||||
| 35 | strings => 'trim ltrim rtrim compress left right mid instr', | ||||||||||||||||||||||||||||
| 36 | process => 'daemonize spawn kill_proc exitcode _do_fork', | ||||||||||||||||||||||||||||
| 37 | boolean => 'is_truthy is_falsey bool', | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 1 | ▶ | ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | #use Data::Dumper; | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 45 | # Public Methods | ||||||||||||||||||||||||||||
| 46 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # recursively walk a HOH | ||||||||||||||||||||||||||||
| 49 | sub hash_walk { | ||||||||||||||||||||||||||||
| 50 | 0 | ▶ | my $p = XAS::Base->validate_params(\@_, { | ||||||||||||||||||||||||||
|
Subroutine
hash_walk: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | -hash => { type => HASHREF }, | ||||||||||||||||||||||||||||
| 52 | -keys => { type => ARRAYREF }, | ||||||||||||||||||||||||||||
| 53 | -callback => { type => CODEREF }, | ||||||||||||||||||||||||||||
| 54 | }); | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | my $hash = $p->{'hash'}; | |||||||||||||||||||||||||||
| 57 | 0 | my $key_list = $p->{'keys'}; | |||||||||||||||||||||||||||
| 58 | 0 | my $callback = $p->{'callback'}; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | while (my ($k, $v) = each %$hash) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # Keep track of the hierarchy of keys, in case | ||||||||||||||||||||||||||||
| 63 | # our callback needs it. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | push(@$key_list, $k); | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if (ref($v) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # Recurse. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | hash_walk(-hash => $v, -keys => $key_list, -callback => $callback); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | } else { | ||||||||||||||||||||||||||||
| 74 | # Otherwise, invoke our callback, passing it | ||||||||||||||||||||||||||||
| 75 | # the current key and value, along with the | ||||||||||||||||||||||||||||
| 76 | # full parentage of that key. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | $callback->($k, $v, $key_list); | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | pop(@$key_list); | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # recursively walk a directory structure | ||||||||||||||||||||||||||||
| 89 | sub dir_walk { | ||||||||||||||||||||||||||||
| 90 | my $p = XAS::Base->validate_params(\@_, { | ||||||||||||||||||||||||||||
| 91 | -directory => { isa => 'Badger::Filesystem::Directory' }, | ||||||||||||||||||||||||||||
| 92 | -callback => { type => CODEREF }, | ||||||||||||||||||||||||||||
| 93 | -filter => { optional => 1, default => qr/.*/, callbacks => { | ||||||||||||||||||||||||||||
| 94 | 'must be a compiled regex' => sub { | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | return (ref shift() eq 'Regexp') ? 1 : 0; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | }}, | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | }); | ||||||||||||||||||||||||||
|
Subroutine
dir_walk: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | my $folder = $p->{'directory'}; | |||||||||||||||||||||||||||
| 101 | 0 | my $filter = $p->{'filter'}; | |||||||||||||||||||||||||||
| 102 | 0 | my $callback = $p->{'callback'}; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | my @files = grep ( $_->path =~ /$filter/, $folder->files() ); | |||||||||||||||||||||||||||
| 105 | 0 | my @folders = $folder->dirs; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | foreach my $file (@files) { | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | $callback->($file); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | foreach my $folder (@folders) { | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | dir_walk(-directory => $folder, -filter => $filter, -callback => $callback); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # Perl trim function to remove whitespace from the start and end of the string | ||||||||||||||||||||||||||||
| 122 | sub trim { | ||||||||||||||||||||||||||||
| 123 | 0 | ▶ | my ($string) = XAS::Base->validate_params(\@_, [1]); | ||||||||||||||||||||||||||
|
Subroutine
trim: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | $string =~ s/^\s+//; | |||||||||||||||||||||||||||
| 126 | 0 | $string =~ s/\s+$//; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | return $string; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # Left trim function to remove leading whitespace | ||||||||||||||||||||||||||||
| 133 | sub ltrim { | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | my ($string) = XAS::Base->validate_params(\@_, [1]); | ||||||||||||||||||||||||||
|
Subroutine
ltrim: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | $string =~ s/^\s+//; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | return $string; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | # Right trim function to remove trailing whitespace | ||||||||||||||||||||||||||||
| 143 | sub rtrim { | ||||||||||||||||||||||||||||
| 144 | 0 | ▶ | my ($string) = XAS::Base->validate_params(\@_, [1]); | ||||||||||||||||||||||||||
|
Subroutine
rtrim: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | $string =~ s/\s+$//; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | return $string; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # replace multiple whitspace with a single space | ||||||||||||||||||||||||||||
| 153 | sub compress { | ||||||||||||||||||||||||||||
| 154 | 0 | ▶ | my ($string) = XAS::Base->validate_params(\@_, [1]); | ||||||||||||||||||||||||||
|
Subroutine
compress: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | $string =~ s/\s+/ /gms; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | return $string; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # emulate Basics string function left() | ||||||||||||||||||||||||||||
| 163 | sub left { | ||||||||||||||||||||||||||||
| 164 | 0 | ▶ | my ($string, $offset) = XAS::Base->validate_params(\@_, [1,1]); | ||||||||||||||||||||||||||
|
Subroutine
left: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | return substr($string, 0, $offset); | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | # emulate Basics string function right() | ||||||||||||||||||||||||||||
| 171 | sub right { | ||||||||||||||||||||||||||||
| 172 | 0 | ▶ | my ($string, $offset) = XAS::Base->validate_params(\@_, [1,1]); | ||||||||||||||||||||||||||
|
Subroutine
right: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | return substr($string, -($offset)); | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | # emulate Basics string function mid() | ||||||||||||||||||||||||||||
| 179 | sub mid { | ||||||||||||||||||||||||||||
| 180 | 0 | ▶ | my ($string, $start, $length) = XAS::Base->validate_params(\@_, [1,1,1]); | ||||||||||||||||||||||||||
|
Subroutine
mid: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | return substr($string, $start - 1, $length); | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | # emulate Basics string function instr() | ||||||||||||||||||||||||||||
| 187 | sub instr { | ||||||||||||||||||||||||||||
| 188 | 0 | ▶ | my ($start, $string, $compare) = XAS::Base->validate_params(\@_, [1,1,1]); | ||||||||||||||||||||||||||
|
Subroutine
instr: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | ▶ | if ($start =~ /^[0-9\-]+/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | $start++; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | } else { | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 0 | $compare = $string; | |||||||||||||||||||||||||||
| 197 | 0 | $string = $start; | |||||||||||||||||||||||||||
| 198 | 0 | $start = 0; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | return index($string, $compare, $start) + 1; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | # Checks to see if the parameter is the string 't', 'true', 'yes', '0E0' | ||||||||||||||||||||||||||||
| 207 | # or the number 1. | ||||||||||||||||||||||||||||
| 208 | # | ||||||||||||||||||||||||||||
| 209 | sub is_truthy { | ||||||||||||||||||||||||||||
| 210 | 0 | ▶ | my ($parm) = XAS::Base->validate_params(\@_, [1]); | ||||||||||||||||||||||||||
|
Subroutine
is_truthy: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 0 | my @truth = qw(yes true t 1 0e0); | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 0 | return scalar(grep {lc($parm) eq $_} @truth); | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | # Checks to see if the parameter is the string 'f', 'false', 'no' or | ||||||||||||||||||||||||||||
| 219 | # the number 0. | ||||||||||||||||||||||||||||
| 220 | # | ||||||||||||||||||||||||||||
| 221 | sub is_falsey { | ||||||||||||||||||||||||||||
| 222 | 0 | ▶ | my ($parm) = XAS::Base->validate_params(\@_, [1]); | ||||||||||||||||||||||||||
|
Subroutine
is_falsey: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 0 | my @truth = qw(no false f 0); | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 0 | return scalar(grep {lc($parm) eq $_} @truth); | |||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | sub bool { | ||||||||||||||||||||||||||||
| 231 | 0 | ▶ | my ($item) = XAS::Base->validate_params(\@_, [1]); | ||||||||||||||||||||||||||
|
Subroutine
bool: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 0 | my @truth = qw(yes true 1 0e0 no false f 0); | |||||||||||||||||||||||||||
| 234 | 0 | return grep {lc($item) eq $_} @truth; | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | sub spawn { | ||||||||||||||||||||||||||||
| 239 | 0 | ▶ | my $p = XAS::Base->validate_params(\@_, { | ||||||||||||||||||||||||||
|
Subroutine
spawn: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 240 | -command => 1, | ||||||||||||||||||||||||||||
| 241 | -timeout => { optional => 1, default => 0 }, | ||||||||||||||||||||||||||||
| 242 | }); | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | local $SIG{ALRM} = sub { | ||||||||||||||||||||||||||||
| 245 | 0 | ▶ | my $sig_name = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 246 | 0 | die "$sig_name"; | |||||||||||||||||||||||||||
| 247 | 0 | }; | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 0 | my $kid; | |||||||||||||||||||||||||||
| 250 | my @output; | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 0 | ▶ | defined( my $pid = open($kid, "-|" ) ) or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 0 | my $ex = XAS::Exception->new( | |||||||||||||||||||||||||||
| 255 | info => "unable to fork, reason: $!", | ||||||||||||||||||||||||||||
| 256 | type => 'xas.utils.spawn' | ||||||||||||||||||||||||||||
| 257 | ); | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 0 | $ex->throw; | |||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | }; | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 0 | ▶ | if ($pid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | # parent | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | try { | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 0 | ▶ | alarm( $p->{'timeout'} ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 0 | ▶ | while (<$kid>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 0 | chomp; | |||||||||||||||||||||||||||
| 274 | 0 | push(@output, $_); | |||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 0 | alarm(0); | |||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | } catch { | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 0 | ▶ | my $ex = $_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 0 | alarm(0); | |||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 0 | ▶ | if ($ex =~ /alrm/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | 0 | ▶ | unless (kill_proc(-signal => 'TERM', -pid => $pid)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 0 | ▶ | unless (kill_proc(-signal => 'KILL', -pid => $pid)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 0 | my $ex = XAS::Exception->new( | |||||||||||||||||||||||||||
| 293 | type => 'xas.utils.spawn', | ||||||||||||||||||||||||||||
| 294 | info => 'unable to kill ' . $pid | ||||||||||||||||||||||||||||
| 295 | ); | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 0 | $ex->throw; | |||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | } else { | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 0 | die $ex; | |||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 0 | }; | |||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | } else { | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | # child | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | # set the child process to be a group leader, so that | ||||||||||||||||||||||||||||
| 316 | # kill -9 will kill it and all its descendents | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 0 | setpgrp(0, 0); | |||||||||||||||||||||||||||
| 319 | 0 | exec $p->{'command'}; | |||||||||||||||||||||||||||
| 320 | 0 | exit; | |||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | 0 | ▶ | wantarray ? @output : join( "\n", @output ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | sub kill_proc { | ||||||||||||||||||||||||||||
| 329 | 0 | ▶ | my $p = XAS::Base->validate_params(\@_, { | ||||||||||||||||||||||||||
|
Subroutine
kill_proc: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 330 | -signal => 1, | ||||||||||||||||||||||||||||
| 331 | -pid => 1, | ||||||||||||||||||||||||||||
| 332 | }); | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 0 | my $time = 10; | |||||||||||||||||||||||||||
| 335 | 0 | my $status = 0; | |||||||||||||||||||||||||||
| 336 | 0 | my $pid = $p->{'pid'}; | |||||||||||||||||||||||||||
| 337 | 0 | my $signal = $p->{'signal'}; | |||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 0 | kill($signal, $pid); | |||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 0 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$time and not $status
MC/DC0%0/2
$time and not $status
$time
not $status
Truth table0%0/3
A and not(B)
A= $timeB= not $status
| |||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | 0 | sleep 1; | |||||||||||||||||||||||||||
| 344 | 0 | $status = waitpid($pid, WNOHANG); | |||||||||||||||||||||||||||
| 345 | 0 | $time--; | |||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | } while ($time && not $status); | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 0 | return $status; | |||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | sub exitcode { | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | 0 | ▶ | my $rc = $? >> 8; # return code of command | ||||||||||||||||||||||||||
|
Subroutine
exitcode: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 356 | 0 | my $sig = $? & 127; # signal it was killed with | |||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 0 | return $rc, $sig; | |||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | sub _do_fork { | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 0 | ▶ | my $child = fork(); | ||||||||||||||||||||||||||
|
Subroutine
_do_fork: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 0 | ▶ | unless (defined($child)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 0 | my $ex = XAS::Exception->new( | |||||||||||||||||||||||||||
| 369 | type => 'xas.utils.daemonize', | ||||||||||||||||||||||||||||
| 370 | info => "unable to fork, reason: $!" | ||||||||||||||||||||||||||||
| 371 | ); | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | 0 | $ex->throw; | |||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 0 | ▶ | exit(0) if ($child); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | sub daemonize { | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 0 | ▶ | _do_fork(); # initial fork | ||||||||||||||||||||||||||
|
Subroutine
daemonize: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 384 | 0 | setsid(); # become session leader | |||||||||||||||||||||||||||
| 385 | 0 | _do_fork(); # second fork to prevent aquiring a controlling terminal | |||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | # change directory to a netural place and set the umask | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | 0 | chdir('/'); | |||||||||||||||||||||||||||
| 390 | 0 | umask(0); | |||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | # redirect our standard file handles | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | 0 | open(STDIN, '<', '/dev/null'); | |||||||||||||||||||||||||||
| 395 | 0 | open(STDOUT, '>', '/dev/null'); | |||||||||||||||||||||||||||
| 396 | 0 | open(STDERR, '>', '/dev/null'); | |||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | sub db2dt { | ||||||||||||||||||||||||||||
| 401 | 0 | ▶ | my ($p) = XAS::Base->validate_params(\@_, [ | ||||||||||||||||||||||||||
|
Subroutine
db2dt: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 402 | { regex => qr/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/ } | ||||||||||||||||||||||||||||
| 403 | ]); | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | my $parser = DateTime::Format::Strptime->new( | ||||||||||||||||||||||||||||
| 406 | pattern => '%Y-%m-%d %H:%M:%S', | ||||||||||||||||||||||||||||
| 407 | time_zone => 'local', | ||||||||||||||||||||||||||||
| 408 | on_error => sub { | ||||||||||||||||||||||||||||
| 409 | 0 | ▶ | my ($obj, $err) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 410 | 0 | my $ex = XAS::Exception->new( | |||||||||||||||||||||||||||
| 411 | type => 'xas.utils.db2dt', | ||||||||||||||||||||||||||||
| 412 | info => $err | ||||||||||||||||||||||||||||
| 413 | ); | ||||||||||||||||||||||||||||
| 414 | 0 | $ex->throw; | |||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||
| 416 | 0 | ); | |||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | 0 | return $parser->parse_datetime($p); | |||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | sub dt2db { | ||||||||||||||||||||||||||||
| 423 | 0 | ▶ | my ($dt) = XAS::Base->validate_params(\@_, [ | ||||||||||||||||||||||||||
|
Subroutine
dt2db: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 424 | { isa => 'DateTime' } | ||||||||||||||||||||||||||||
| 425 | ]); | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | 0 | return $dt->strftime('%Y-%m-%d %H:%M:%S'); | |||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | sub run_cmd { | ||||||||||||||||||||||||||||
| 432 | 0 | ▶ | my ($command) = XAS::Base->validate_params(\@_, [1]); | ||||||||||||||||||||||||||
|
Subroutine
run_cmd: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | 0 | my @output = `$command 2>&1`; | |||||||||||||||||||||||||||
| 435 | 0 | my ($rc, $sig) = exitcode(); | |||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | 0 | return \@output, $rc, $sig; | |||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | sub init_module { | ||||||||||||||||||||||||||||
| 442 | 0 | ▶ | my ($module, $params) = XAS::Base->validate_params(@_, [ | ||||||||||||||||||||||||||
|
Subroutine
init_module: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 443 | 1, | ||||||||||||||||||||||||||||
| 444 | { optional => 1, type => HASHREF, default => {} } | ||||||||||||||||||||||||||||
| 445 | ]); | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | 0 | my $obj; | |||||||||||||||||||||||||||
| 448 | my @parts; | ||||||||||||||||||||||||||||
| 449 | 0 | my $filename; | |||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | 0 | @parts = split("::", $module); | |||||||||||||||||||||||||||
| 452 | 0 | $filename = File(@parts); | |||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | try { | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | 0 | ▶ | require $filename . '.pm'; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 457 | 0 | $module->import(); | |||||||||||||||||||||||||||
| 458 | 0 | $obj = $module->new($params); | |||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | } catch { | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | 0 | ▶ | my $x = $_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 463 | 0 | my $ex = XAS::Exception->new( | |||||||||||||||||||||||||||
| 464 | type => 'xas.utils.init_module', | ||||||||||||||||||||||||||||
| 465 | info => $x | ||||||||||||||||||||||||||||
| 466 | ); | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | 0 | $ex->throw; | |||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | 0 | }; | |||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | 0 | return $obj; | |||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | sub load_module { | ||||||||||||||||||||||||||||
| 477 | 0 | ▶ | my ($module) = XAS::Base->validate_params(\@_, [1]); | ||||||||||||||||||||||||||
|
Subroutine
load_module: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | 0 | my @parts; | |||||||||||||||||||||||||||
| 480 | my $filename; | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | 0 | @parts = split("::", $module); | |||||||||||||||||||||||||||
| 483 | 0 | $filename = File(@parts); | |||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | try { | ||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | 0 | ▶ | require $filename . '.pm'; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 488 | 0 | $module->import(); | |||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | } catch { | ||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | 0 | ▶ | my $x = $_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 493 | 0 | my $ex = XAS::Exception->new( | |||||||||||||||||||||||||||
| 494 | type => 'xas.utils.load_module', | ||||||||||||||||||||||||||||
| 495 | info => $x | ||||||||||||||||||||||||||||
| 496 | ); | ||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | 0 | $ex->throw; | |||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | 0 | }; | |||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | } | ||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | sub glob2regex { | ||||||||||||||||||||||||||||
| 505 | 0 | ▶ | my ($globstr) = XAS::Base->validate_params(\@_, [1]); | ||||||||||||||||||||||||||
|
Subroutine
glob2regex: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||
| 507 | 0 | my %patmap = ( | |||||||||||||||||||||||||||
| 508 | '*' => '.*', | ||||||||||||||||||||||||||||
| 509 | '?' => '.', | ||||||||||||||||||||||||||||
| 510 | '[' => '[', | ||||||||||||||||||||||||||||
| 511 | ']' => ']', | ||||||||||||||||||||||||||||
| 512 | ); | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | 0 | ▶ | $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | 0 | return '^' . $globstr . '$'; | |||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | } | ||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | sub env_store { | ||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | 0 | ▶ | my %env; | ||||||||||||||||||||||||||
|
Subroutine
env_store: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||
| 524 | 0 | ▶ | while ((my $key, my $value) = each(%ENV)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||
| 526 | 0 | delete $ENV{$key}; | |||||||||||||||||||||||||||
| 527 | 0 | $env{$key} = $value; | |||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | } | ||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | 0 | return \%env; | |||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | } | ||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | sub env_restore { | ||||||||||||||||||||||||||||
| 536 | 0 | ▶ | my ($env) = XAS::Base->validate_params(\@_, [1]); | ||||||||||||||||||||||||||
|
Subroutine
env_restore: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | 0 | ▶ | while ((my $key, my $value) = each(%ENV)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | 0 | delete $ENV{$key}; | |||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | } | ||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | 0 | ▶ | while ((my $key, my $value) = each(%{$env})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | 0 | $ENV{$key} = $value; | |||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | } | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | } | ||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | sub env_create { | ||||||||||||||||||||||||||||
| 553 | 0 | ▶ | my ($env) = XAS::Base->validate_params(\@_, [1]); | ||||||||||||||||||||||||||
|
Subroutine
env_create: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | 0 | ▶ | while ((my $key, my $value) = each(%{$env})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||
| 557 | 0 | $ENV{$key} = $value; | |||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||
| 559 | } | ||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||
| 561 | } | ||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||
| 563 | sub env_parse { | ||||||||||||||||||||||||||||
| 564 | 0 | ▶ | my ($env) = XAS::Base->validate_params(\@_, [1]); | ||||||||||||||||||||||||||
|
Subroutine
env_parse: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||
| 566 | 0 | my ($key, $value, %env); | |||||||||||||||||||||||||||
| 567 | 0 | my @envs = split(';;', $env); | |||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||
| 569 | 0 | foreach my $y (@envs) { | |||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||
| 571 | 0 | ($key, $value) = split('=', $y); | |||||||||||||||||||||||||||
| 572 | 0 | $env{$key} = $value; | |||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||
| 574 | } | ||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | 0 | return \%env; | |||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||
| 578 | } | ||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||
| 580 | sub env_dump { | ||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | 0 | ▶ | my $env; | ||||||||||||||||||||||||||
|
Subroutine
env_dump: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | 0 | ▶ | while ((my $key, my $value) = each(%ENV)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||
| 586 | 0 | $env .= "$key=$value;;"; | |||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||
| 588 | } | ||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||
| 590 | # remove the ;; at the end | ||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||
| 592 | 0 | chop $env; | |||||||||||||||||||||||||||
| 593 | 0 | chop $env; | |||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||
| 595 | 0 | return $env; | |||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||
| 597 | } | ||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||
| 599 | 1 | 1; | |||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||
| 601 | __END__ | ||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||
| 603 | =head1 NAME | ||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||
| 605 | XAS::Utils - A Perl extension for the XAS environment | ||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | use XAS::Class | ||||||||||||||||||||||||||||
| 610 | version => '0.01', | ||||||||||||||||||||||||||||
| 611 | base => 'XAS::Base', | ||||||||||||||||||||||||||||
| 612 | utils => 'db2dt dt2db' | ||||||||||||||||||||||||||||
| 613 | ; | ||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | printf("%s\n", dt2db($dt)); | ||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||
| 617 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||
| 619 | This module provides utility routines that can by loaded into your current | ||||||||||||||||||||||||||||
| 620 | namespace. | ||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | =head1 METHODS | ||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||
| 624 | =head2 db2dt($datestring) | ||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||
| 626 | This routine will take a date format of YYYY-MM-DD HH:MM:SS and convert it | ||||||||||||||||||||||||||||
| 627 | into a DateTime object. | ||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||
| 629 | =head2 dt2db($datetime) | ||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||
| 631 | This routine will take a DateTime object and convert it into the following | ||||||||||||||||||||||||||||
| 632 | string: YYYY-MM-DD HH:MM:SS | ||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||
| 634 | =head2 trim($string) | ||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | Trim the whitespace from the beginning and end of $string. | ||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||
| 638 | =head2 ltrim($string) | ||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | Trim the whitespace from the end of $string. | ||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||
| 642 | =head2 rtrim($string) | ||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||
| 644 | Trim the whitespace from the beginning of $string. | ||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||
| 646 | =head2 compress($string) | ||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||
| 648 | Reduces multiple whitespace to a single space in $string. | ||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||
| 650 | =head2 left($string, $offset) | ||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||
| 652 | Return the left chunk of $string up to $offset. Useful for porting | ||||||||||||||||||||||||||||
| 653 | VBS code. Makes allowances that VBS strings are ones based while | ||||||||||||||||||||||||||||
| 654 | Perls are zero based. | ||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||
| 656 | =head2 right($string, $offset) | ||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||
| 658 | Return the right chunk of $string starting at $offset. Useful for porting | ||||||||||||||||||||||||||||
| 659 | VBS code. Makes allowances that VBS strings are ones based while Perls | ||||||||||||||||||||||||||||
| 660 | are zero based. | ||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||
| 662 | =head2 mid($string, $offset, $length) | ||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||
| 664 | Return the chunk of $string starting at $offset for $length characters. | ||||||||||||||||||||||||||||
| 665 | Useful for porting VBS code. Makes allowances that VBS strings are ones | ||||||||||||||||||||||||||||
| 666 | based while Perls are zero based. | ||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||
| 668 | =head2 instr($start, $string, $compare) | ||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||
| 670 | Return the position in $string of $compare. You may offset within the | ||||||||||||||||||||||||||||
| 671 | string with $start. Useful for porting VBS code. Makes allowances that | ||||||||||||||||||||||||||||
| 672 | VBS strings are one based while Perls are zero based. | ||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||
| 674 | =head2 spawn | ||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||
| 676 | Run a cli command with timeout. Returns output from that command. | ||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||
| 678 | =over 4 | ||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||
| 680 | =item B<-command> | ||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||
| 682 | The command string to run. | ||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||
| 684 | =item B<-timeout> | ||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||
| 686 | An optional timeout in seconds. Default is none. | ||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||
| 688 | =back | ||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||
| 690 | =head2 exitcode | ||||||||||||||||||||||||||||
| 691 | |||||||||||||||||||||||||||||
| 692 | Decodes Perls version of the exit code from a cli process. Returns two items. | ||||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||
| 694 | Example: | ||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||
| 696 | my @output = spawn(-command => "ls -l"); | ||||||||||||||||||||||||||||
| 697 | my ($rc, $sig) = exitcode(); | ||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||
| 699 | =head2 run_cmd($command) | ||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||
| 701 | Run a command and capture the output and exit code, stderr is merged | ||||||||||||||||||||||||||||
| 702 | with stdout. | ||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||
| 704 | Example: | ||||||||||||||||||||||||||||
| 705 | |||||||||||||||||||||||||||||
| 706 | my (@output, $rc) = run_cmd("ls -l"); | ||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||
| 708 | =head2 daemonize | ||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||
| 710 | Become a daemon. This will set the process as a session lead, change to '/', | ||||||||||||||||||||||||||||
| 711 | clear the protection mask and redirect stdin, stdout and stderr to /dev/null. | ||||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||
| 713 | =head2 glob2regx($glob) | ||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||
| 715 | This method will take a shell glob pattern and convert it into a Perl regex. | ||||||||||||||||||||||||||||
| 716 | This also works with DOS/Windows wildcards. | ||||||||||||||||||||||||||||
| 717 | |||||||||||||||||||||||||||||
| 718 | =over 4 | ||||||||||||||||||||||||||||
| 719 | |||||||||||||||||||||||||||||
| 720 | =item B<$glob> | ||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||
| 722 | The wildcard to convert. | ||||||||||||||||||||||||||||
| 723 | |||||||||||||||||||||||||||||
| 724 | =back | ||||||||||||||||||||||||||||
| 725 | |||||||||||||||||||||||||||||
| 726 | =head2 hash_walk | ||||||||||||||||||||||||||||
| 727 | |||||||||||||||||||||||||||||
| 728 | This routine will walk a HOH and does a callback on the key/values that are | ||||||||||||||||||||||||||||
| 729 | found. It takes these parameters: | ||||||||||||||||||||||||||||
| 730 | |||||||||||||||||||||||||||||
| 731 | =over 4 | ||||||||||||||||||||||||||||
| 732 | |||||||||||||||||||||||||||||
| 733 | =item B<-hash> | ||||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||
| 735 | The hashref of the HOH. | ||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||
| 737 | =item B<-keys> | ||||||||||||||||||||||||||||
| 738 | |||||||||||||||||||||||||||||
| 739 | An arrayref of the key levels. | ||||||||||||||||||||||||||||
| 740 | |||||||||||||||||||||||||||||
| 741 | =item B<-callback> | ||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||
| 743 | The routine to call with these parameters: | ||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||
| 745 | =over 4 | ||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||
| 747 | =item B<$key> | ||||||||||||||||||||||||||||
| 748 | |||||||||||||||||||||||||||||
| 749 | The current hash key. | ||||||||||||||||||||||||||||
| 750 | |||||||||||||||||||||||||||||
| 751 | =item B<$value> | ||||||||||||||||||||||||||||
| 752 | |||||||||||||||||||||||||||||
| 753 | The value of that key. | ||||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||
| 755 | =item B<$key_list> | ||||||||||||||||||||||||||||
| 756 | |||||||||||||||||||||||||||||
| 757 | A list of the key depth. | ||||||||||||||||||||||||||||
| 758 | |||||||||||||||||||||||||||||
| 759 | =back | ||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||
| 761 | =back | ||||||||||||||||||||||||||||
| 762 | |||||||||||||||||||||||||||||
| 763 | =head2 dir_walk | ||||||||||||||||||||||||||||
| 764 | |||||||||||||||||||||||||||||
| 765 | This will walk a directory structure and execute a callback for the found | ||||||||||||||||||||||||||||
| 766 | files. It takes these parameters: | ||||||||||||||||||||||||||||
| 767 | |||||||||||||||||||||||||||||
| 768 | =over 4 | ||||||||||||||||||||||||||||
| 769 | |||||||||||||||||||||||||||||
| 770 | =item B<-directory> | ||||||||||||||||||||||||||||
| 771 | |||||||||||||||||||||||||||||
| 772 | The root directory to start from. | ||||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||
| 774 | =item B<-filter> | ||||||||||||||||||||||||||||
| 775 | |||||||||||||||||||||||||||||
| 776 | A compiled regex to compare files against. | ||||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||
| 778 | =item B<-callback> | ||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||
| 780 | The callback to execute when matching files are found. | ||||||||||||||||||||||||||||
| 781 | |||||||||||||||||||||||||||||
| 782 | =back | ||||||||||||||||||||||||||||
| 783 | |||||||||||||||||||||||||||||
| 784 | =head2 init_module($module, $options) | ||||||||||||||||||||||||||||
| 785 | |||||||||||||||||||||||||||||
| 786 | This routine will load and initialize a module. It takes one required parameter | ||||||||||||||||||||||||||||
| 787 | and one optinal parameter. | ||||||||||||||||||||||||||||
| 788 | |||||||||||||||||||||||||||||
| 789 | =over 4 | ||||||||||||||||||||||||||||
| 790 | |||||||||||||||||||||||||||||
| 791 | =item B<$module> | ||||||||||||||||||||||||||||
| 792 | |||||||||||||||||||||||||||||
| 793 | The name of the module. | ||||||||||||||||||||||||||||
| 794 | |||||||||||||||||||||||||||||
| 795 | =item B<$options> | ||||||||||||||||||||||||||||
| 796 | |||||||||||||||||||||||||||||
| 797 | A hashref of optional options to use with the module. | ||||||||||||||||||||||||||||
| 798 | |||||||||||||||||||||||||||||
| 799 | =back | ||||||||||||||||||||||||||||
| 800 | |||||||||||||||||||||||||||||
| 801 | =head2 load_module($module) | ||||||||||||||||||||||||||||
| 802 | |||||||||||||||||||||||||||||
| 803 | This routine will load a module. | ||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||
| 805 | =over 4 | ||||||||||||||||||||||||||||
| 806 | |||||||||||||||||||||||||||||
| 807 | =item B<$module> | ||||||||||||||||||||||||||||
| 808 | |||||||||||||||||||||||||||||
| 809 | The name of the module. | ||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||
| 811 | =back | ||||||||||||||||||||||||||||
| 812 | |||||||||||||||||||||||||||||
| 813 | =head2 env_store | ||||||||||||||||||||||||||||
| 814 | |||||||||||||||||||||||||||||
| 815 | Remove all items from the $ENV variable and store them in a hash variable. | ||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||
| 817 | Example: | ||||||||||||||||||||||||||||
| 818 | my $env = env_store(); | ||||||||||||||||||||||||||||
| 819 | |||||||||||||||||||||||||||||
| 820 | =head2 env_restore | ||||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||
| 822 | Remove all items from $ENV variable and restore it back to a saved hash variable. | ||||||||||||||||||||||||||||
| 823 | |||||||||||||||||||||||||||||
| 824 | Example: | ||||||||||||||||||||||||||||
| 825 | env_restore($env); | ||||||||||||||||||||||||||||
| 826 | |||||||||||||||||||||||||||||
| 827 | =head2 env_create | ||||||||||||||||||||||||||||
| 828 | |||||||||||||||||||||||||||||
| 829 | Store all the items from a hash variable into the $ENV varable. | ||||||||||||||||||||||||||||
| 830 | |||||||||||||||||||||||||||||
| 831 | Example: | ||||||||||||||||||||||||||||
| 832 | env_create($env); | ||||||||||||||||||||||||||||
| 833 | |||||||||||||||||||||||||||||
| 834 | =head2 env_parse | ||||||||||||||||||||||||||||
| 835 | |||||||||||||||||||||||||||||
| 836 | Take a formated string and parse it into a hash variable. The string must have | ||||||||||||||||||||||||||||
| 837 | this format: "item=value;;item2=value2"; | ||||||||||||||||||||||||||||
| 838 | |||||||||||||||||||||||||||||
| 839 | Example: | ||||||||||||||||||||||||||||
| 840 | my $string = "item=value;;item2=value2"; | ||||||||||||||||||||||||||||
| 841 | my $env = env_parse($string); | ||||||||||||||||||||||||||||
| 842 | env_create($env); | ||||||||||||||||||||||||||||
| 843 | |||||||||||||||||||||||||||||
| 844 | =head2 env_dump | ||||||||||||||||||||||||||||
| 845 | |||||||||||||||||||||||||||||
| 846 | Take the items from the current $ENV variable and create a formated string. | ||||||||||||||||||||||||||||
| 847 | |||||||||||||||||||||||||||||
| 848 | Example: | ||||||||||||||||||||||||||||
| 849 | my $string = env_dump(); | ||||||||||||||||||||||||||||
| 850 | my $env = env_create($string); | ||||||||||||||||||||||||||||
| 851 | |||||||||||||||||||||||||||||
| 852 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 853 | |||||||||||||||||||||||||||||
| 854 | =over 4 | ||||||||||||||||||||||||||||
| 855 | |||||||||||||||||||||||||||||
| 856 | =item L<XAS|XAS> | ||||||||||||||||||||||||||||
| 857 | |||||||||||||||||||||||||||||
| 858 | =item L<Badger::Utils|https://metacpan.org/pod/Badger::Utils> | ||||||||||||||||||||||||||||
| 859 | |||||||||||||||||||||||||||||
| 860 | =back | ||||||||||||||||||||||||||||
| 861 | |||||||||||||||||||||||||||||
| 862 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 863 | |||||||||||||||||||||||||||||
| 864 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||||||||||||||||||||||||||||
| 865 | |||||||||||||||||||||||||||||
| 866 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 867 | |||||||||||||||||||||||||||||
| 868 | Copyright (C) 2014 Kevin L. Esteb | ||||||||||||||||||||||||||||
| 869 | |||||||||||||||||||||||||||||
| 870 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 871 | the terms of the Artistic License 2.0. For details, see the full text | ||||||||||||||||||||||||||||
| 872 | of the license at http://www.perlfoundation.org/artistic_license_2_0. | ||||||||||||||||||||||||||||
| 873 | |||||||||||||||||||||||||||||
| 874 | =cut | ||||||||||||||||||||||||||||