RoPkg::Utils exports all the methods from the above list. Of course, you can select which ones you want
to import.
Example:
use RoPkg::Utils qw(ZeroPad);
sub main {
print ZeroPad(1, 3),$/;
}
main();
CheckParam
Check if the parameters from the list are defined inside the object. If the parameters are not defined
Param::Missing exception is raised.
Example
package RoPkg::TesterPkg;
sub new {
my ($class, %opt) = @_;
my $self;
$self = bless { %opt }, $class;
$self->_test_options_1();
$self->_test_options_2();
return $self;
}
sub _test_options_1 {
my ($self) = @_;
RoPkg::Utils::CheckParam($self, qw(dbo dbo_method));
return 1;
}
sub _test_options_2 {
my ($self) = @_;
my $ru;
$ru = new RoPkg::Utils;
$ru->CheckParam($self, qw(dbo dbo_method));
return 1;
}
1;
ZeroPad
This method is usefull when you need to pad with zeroes some numbers. For the moment positive and
negative numbers are suported (only integer ones). This method takes 2 parameters $number and
$pad_length. The $number is the number who is going to be padded, $pad_length is the maximum length.
Zeroes are added until $pad_length is reached. If $pad_length is not defined, length($number)+1 is used.
If $pad_length is less than number length, no padding is done. Ex:
ZeroPad(1, 2) => 01
ZeroPad(12, 5) => 00012
ZeroPad(-3, 4) => -003
ZeroPad(10, 2) => 10
GetMonthNo
Used to find a month numeric index (1..12). For the moment full month names and short month names (first
3 letters) are supported. 2 parameters may be passed to this method: $month_name and $padding.
$month_name must be January .. December or Jan .. Dec . If the month_name is not found, Param::Unknown
exception is raised. $padding is used to pad the numeric index of the month. When padding is not defined,
no padding is made. Ex:
GetMonthNo('Jan') => 1
GetMonthNo('Jan', 2) => 01
GetMonthNo('January') => 1
GetMonthNo('January', 2) => 01
ElemInList
Takes two parameters. A element value and a list of elements. The methods, search the element value
within the list. Returns 1 if the element was found, 0 otherwise. Ex:
ElemInList(1, qw(1 2 3 4)) => 1
ElemInList(1, qw(2 3 4)) => 0
GetHumanDate
Converts the timestamp to YYYY/MM/DD HH:MM format. Ex:
GetHumanDate(12345678) => '1970/05/23 23:21'
SecToTime
Converts the seconds to <x days> HH:MM:SS format. Ex:
SecToTime(3) => 00:00:03
SecToTime(61) => 00:01:01
AddSlash
Adds a slash at the end of the string, if the slash is not present. Othewise, leaves the string
untouched. Ex:
AddSlash('mydirectory') => 'mydirectory/'
AddSlash('/tmp/') => '/tmp/';
DelSlash
Removes the slash from the end of the string. If the string has multiple slashes, only the last one is
removed. If the string doesn't have any slashes at the end, no modification is made. Ex:
DelSlash('/tmp/') => '/tmp'
DelSlash('/tmp//') => '/tmp/'
CreateFile
Creates a file (with the filename specified), and optionally set the content of the file to the one
specified by $content variable. If $content is not defined, the file is just created. If the file can't
be created File::Create exception is raised. Returns 1 upon success.
ReadFile
Reads the content of the specified file into a variable. If the file can't be opened a File::Open
exception is raised. On success returns the file content or !defined if the file is empty.
CleanURI($uri)
Cleans the $uri from the excedent / -es .
Example:
my $uri = 'http://www.packages.ro//test///uri';
$uri = CleanURI($uri);
print $uri,$RS;
the result is:
http://www.packages.ro/test/uri
CleanURI recognise only ftp,http and rsync uri's . Any other uri is ignored
CleanPath($path)
Cleans the $path from the excedent / -es .
Example:
my $path = '/var/ftp/mirrors///pub//debian.org//';
$path = CleanPath($path);
print $path, $RS;
the result is:
/var/ftp/mirrors/pub/debian.org/
GetMD5($path)
Returns the base64 md5 sum for the specified file.