NAME
XAS::Utils - A Perl extension for the XAS environment
SYNOPSIS
use XAS::Class
version => '0.01',
base => 'XAS::Base',
utils => 'db2dt dt2db'
;
printf("%s\n", dt2db($dt));
DESCRIPTION
This module provides utility routines that can by loaded into your current namespace.
METHODS
validate_params($params, $spec, $class)
This method is used to validate parameters. Internally this uses Params::Validate::validate_with() for the parameter validation.
By convention, all named parameters have a leading dash. This method will strip off that dash and lower case the parameters name.
If an validation exception is thrown, the parameter name will have the dash stripped.
Based on the $spec, this can return an array or a hashref of validated parameters and values.
- $params
-
An array ref to a set of parameters.
- $spec
-
A validation spec as defined by Params::Validate.
- $class
-
An optional class that is calling this method. If one is not provided then caller() is used to determine the calling method.
validation_exception($param, $class)
This is a package level sub routine. It exists to provide a uniform exception error message. It takes these parameters:
- $param
-
The error message returned by Params::Validate.
- $class
-
The routine that the error occurred in.
db2dt($datestring)
This routine will take a date format of YYYY-MM-DD HH:MM:SS and convert it into a DateTime object.
dt2db($datetime)
This routine will take a DateTime object and convert it into the following string: YYYY-MM-DD HH:MM:SS
trim($string)
Trim the whitespace from the beginning and end of $string.
ltrim($string)
Trim the whitespace from the end of $string.
rtrim($string)
Trim the whitespace from the beginning of $string.
compress($string)
Reduces multiple whitespace to a single space in $string.
left($string, $offset)
Return the left chunk of $string up to $offset. Useful for porting VBS code. Makes allowances that VBS strings are ones based while Perls are zero based.
right($string, $offset)
Return the right chunk of $string starting at $offset. Useful for porting VBS code. Makes allowances that VBS strings are ones based while Perls are zero based.
mid($string, $offset, $length)
Return the chunk of $string starting at $offset for $length characters. Useful for porting VBS code. Makes allowances that VBS strings are ones based while Perls are zero based.
instr($start, $string, $compare)
Return the position in $string of $compare. You may offset within the string with $start. Useful for porting VBS code. Makes allowances that VBS strings are one based while Perls are zero based.
de_camel_case($string)
Break up a "CamelCase" string into a "camel_case" string. The opposit of camel_case() from Badger::Utils.
exitcode
Decodes Perls version of the exit code from a cli process. Returns three items.
Example:
my @output = `ls -l`;
my ($rc, $sig, $cored) = exitcode();
run_cmd($command)
Run a command and capture the output, exit code and exit signal, stderr is merged with stdout.
Example:
my ($output, $rc, $sig) = run_cmd("ls -l");
if ($rc == 0) {
foreach my $line (@$output) {
print $line;
}
}
daemonize
Become a daemon. This will set the process as a session lead, change to '/', clear the protection mask and redirect stdin, stdout and stderr to /dev/null.
glob2regx($glob)
This method will take a shell glob pattern and convert it into a Perl regex. This also works with DOS/Windows wildcards.
hash_walk
This routine will walk a HOH and does a callback on the key/values that are found. It takes these parameters:
- -hash
-
The hashref of the HOH.
- -keys
-
An arrayref of the key levels.
- -callback
-
The routine to call with these parameters:
dir_walk
This will walk a directory structure and execute a callback for the found files. It takes these parameters:
- -directory
-
The root directory to start from.
- -filter
-
A compiled regex to compare files against.
- -callback
-
The callback to execute when matching files are found.
load_module($module)
This routine will load a module.
stat2text($stat)
This will convert the numeric process status to a text string.
- $stat
-
A number between 0 and 6.
0 = 'unknown' 1 = 'other' 2 = 'ready' 3 = 'running' 4 = 'blocked' 5 = 'suspended blocked' 6 = 'suspended ready'
level2syslog($level)
This will convert a XAS log level to an appropriate syslog priority.
- $level
-
A XAS log level, it should be lower cased.
info = 'info', error = 'err', warn = 'warning', fatal = 'alert', trace = 'notice', debug = 'debug'
env_store
Remove all items from the $ENV variable and store them in a hash variable.
Example:
my $env = env_store();
env_restore
Remove all items from $ENV variable and restore it back to a saved hash variable.
Example:
env_restore($env);
env_create
Store all the items from a hash variable into the $ENV varable.
Example:
env_create($env);
env_parse
Take a formatted string and parse it into a hash variable. The string must have this format: "item=value;;item2=value2";
Example:
my $string = "item=value;;item2=value2";
my $env = env_parse($string);
env_create($env);
env_dump
Take the items from the current $ENV variable and create a formatted string.
Example:
my $string = env_dump();
my $env = env_create($string);
SEE ALSO
AUTHOR
Kevin L. Esteb, <kevin@kesteb.us>
COPYRIGHT AND LICENSE
Copyright (c) 2012-2015 Kevin L. Esteb
This is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0. For details, see the full text of the license at http://www.perlfoundation.org/artistic_license_2_0.