Package DE_EPAGES::Core::API::PerlTools
This Module collects useful function, which uses only standard or cpan modules. This Module initialize the randomizer via srand. Please dont call srand again !!!
Functions
- AddDuration
- Can
- CheckIpFormat
- Compare
- CopyStructure
- CreateNewPassWD
- CryptPassword
- ElementOf
- GenerateGUID
- GenerateShortGUID
- GetApplicationServerPort
- GetArchName
- GetCallerFunction
- GetCallerPackage
- GetCallerStack
- GetCallerString
- GetCurrentDateTime
- GetDoubleHashFromLoop
- GetIPAddress
- GetIPAddresses
- GetLocalLocale
- GetLocalTimeZone
- GetLoopFromDoubleArray
- GetMD5Digest
- GetWindowsVersion
- Hex2Str
- IndexOf
- Ip2Hex
- IsA
- Str2Hex
- Str2PerlStrDoubleQuote
- Str2PerlStrSingleQuote
- SubtractDuration
- UnloadPackage
- UsePackage
- fcmp
- isValid
- string_unique
- trim
- unique
AddDuration
Adds a duration to a datetime object and returns the new one.
Syntax |
$DateTime = AddDuration($OldDateTime, $DurationUnit, $Duration); $example $DateTime = AddDuration($OldDateTime, 'days', 1); |
Input |
|
Return |
|
Can
Provides a save way to check if a object has a method named $MethodName Should be used instead of UNIVERSAL->can().
Syntax |
Can( $Object, $MethodName ); |
Input |
|
Return |
|
CheckIpFormat
Tests if the parameter $IpAddress is an IP address of address family $AF.
Syntax |
$ErrorMsg = CheckIpFormat($IpAddress); $ErrorMsg = CheckIpFormat($IpAddress, $AF); |
Example |
$ErrorMsg = CheckIpFormat('123.120.34.254'); |
Input |
|
Output |
|
Compare
Different compares between a and b depending on the type of both elements. The parameter $Type can be ignored if both values are objects and have the method id().
Syntax |
$result = Compare($Type, $a, $b); |
Input |
|
Return |
|
CopyStructure
Copies the full structure with objects, arrays, hashes, scalars but no code references using Data::Dumper.
Syntax |
$Copy = CopyStructure($Structure); |
Input |
|
Return |
|
CreateNewPassWD
Generates a plain password containing only the characters 0..9a-zA-Z.
Syntax |
$Password = CreateNewPassWD($UserName, $Length); |
Input |
|
Return |
|
CryptPassword
Generates a SHA-2 crypted password.
Syntax |
$CryptedPassword = CryptPassword($Password, $Salt); |
Input |
|
Return |
|
ElementOf
Looks if an element is in the list (compare operator is 'eq' = string compare). Looks if an object is in the list (compares the ids of the object).
Syntax |
$IsIncluded = ElementOf($Search, @List); $IsIncluded = ElementOf($SearchObject, @ObjectList); |
Input |
|
Return |
|
GenerateGUID
Returns 36-character long strings containing GUID (UUID). These look something like: b09e53f4-60e9-41a6-8b30-526b69c28b5d.
Syntax |
$GUID = GenerateGUID(); |
Return |
|
GenerateShortGUID
returns a 20 char GUID which is shorter than the original 36 chars
Syntax |
$ShortGUID = GenerateShortGUID(); |
Return |
|
GetApplicationServerPort
Returns the port of the application server.
Syntax |
$ApplicationServerPort = GetApplicationServerPort(); |
Return |
|
GetArchName
Returns architecture name and 32 bit version of architecture name
Syntax |
$ArchName = GetArchName(); |
Return |
|
GetCallerFunction
Called by function. Get your own function name, get name of your caller function.
Syntax |
$FunctionName = GetCallerFunction($SkipCallers); |
Input |
|
Return |
|
GetCallerPackage
Called by package.
Syntax |
$Package = GetCallerPackage($SkipCallers); |
Input |
|
Return |
|
GetCallerStack
Stack information. See additional information at "perldoc -f caller".
Syntax |
$aaStack = GetCallerStack($SkipCallers); |
Input |
|
Return |
|
GetCallerString
Returns a string representation of the caller stack.
Syntax |
$CallerString = GetCallerString(); $CallerString = GetCallerString( $aaCallers); $CallerString = GetCallerString( $aaCallers, $SkipCallers ); |
Example |
$CallerString = GetCallerString( GetCallerStack() ); |
Input |
|
Return |
|
GetCurrentDateTime
Returns the current time with server time_zone.
Syntax |
$DateTime = GetCurrentDateTime(); |
Return |
|
GetDoubleHashFromLoop
Converts a loop (array/hash) to a double hash (hash,hash). Neccesary is an identify field for the new key in the upper hash. Double entries are lost, eg: $hhDoubleHash->{$_->{$IdentKey}} = $_ foreach @$ahLoop.
Syntax |
$hhDoubleHash = GetDoubleHashFromLoop($ahLoop, $IdentKey); |
Input |
|
Return |
|
GetIPAddress
Returns the first IP address from $Address if multiple addresses are used. If address family $AF is omitted, 6 (IPv6) is used. If $Address is omitted, hostname() is used. The information is memorized until the program ends.
Syntax |
$IPAddress = GetIPAddress(); $IPAddress = GetIPAddress($Address, $AF); |
Input |
|
Return |
|
GetIPAddresses
Returns a hash of all IP addresses for $Address (IP or hostname). If $Address is omitted, hostname() is used. The information is memorized until the program ends.
Syntax |
$IpHash = GetIPAddresses($Address); |
Input |
|
Return |
|
GetLocalLocale
Returns the local locale (germany 'de') DateTime::Locale of the application server machine. Under windows operating system returns the full name of languange and country (e.q. "German Germany") wh
Syntax |
$locale = GetLocalLocale(); |
Return |
|
GetLocalTimeZone
Returns the name of the server time zone. Uses the environment variables EPAGES_TZ or TZ.
Syntax |
$timezone = GetLocalTimeZone(); |
Return |
|
GetLoopFromDoubleArray
Converts a 2 dimension array into a loop. The second array will be replaced by hash. Keys of hash are defined at second parameter.
Syntax |
$ahLoop = GetLoopFromDoubleArray ($aaArray, $aKeys); |
Input |
|
Return |
|
GetMD5Digest
Returns the md5 hex digest of content
Syntax |
$HexDigest = GetMD5Digest($rContent); |
Example |
my $HexDigest = GetMD5Digest(GetFileContent("/var/log/error.log")); |
Input |
|
Return |
|
GetWindowsVersion
Returns Windows version name and number
Syntax |
($VersionName, $VersionNumber) = GetWindowsVersion(); |
Return |
|
Hex2Str
Converts a hexadecimal code to a string.
Syntax |
$String = Hex2Str(HexString); |
Example |
print Hex2Str('313241426162'); # prints '12ABab' |
Input |
|
Return |
|
IndexOf
Looks if an element is in the list (compare operator is 'eq' = string compare, if element is an object compares the ids of the object) and returns the posistion (0..) and -1 if not found.
Syntax |
$Position = IndexOf($Search, @List); $IsIncluded = ElementOf($SearchObject, @ObjectList); |
Input |
|
Return |
|
Ip2Hex
Returns the IP address as hex number.
Syntax |
$IPHex = Ip2Hex($IP, $Length); |
Example |
Ip2Hex("1.2.3.4") -> 01020304; Ip2Hex("1.2.3.4", 4) -> 0304; |
Input |
|
Return |
|
IsA
Provides a save way to check if a $Object is blessed into package $ClassName or inherits from it. Should be used instead of UNIVERSAL->isa().
Syntax |
IsA( $Object, $ClassName ); |
Input |
|
Return |
|
Str2Hex
Convers each byte in a string to a hexadecimal code.
Syntax |
$Hex = Str2Hex($String); |
Example |
print Str2Hex('12ABab'); # prints '313241426162' |
Input |
|
Return |
|
Str2PerlStrDoubleQuote
Quotes the string that it can be used in a perl eval. The eval string includes a variable assignment with double quotes (see example).
Syntax |
$OutString = Str2PerlStrDoubleQuote($InString); |
Example |
eval "\$Var1 = \"" . Str2PerlStrDoubleQuote('some string also @@$$ allowed') ."\";"; |
Input |
|
Return |
|
Str2PerlStrSingleQuote
Quotes the string that it can be used in a perl eval.The eval string includes a variable assignment with single quotes (see example).
Syntax |
$OutString = Str2PerlStrSingleQuote($InString); |
Example |
eval "\$Var1 = '" . Str2PerlStrSingleQuote('some string also @@$$ allowed') ."';"; |
Input |
|
Return |
|
SubtractDuration
Subtracts a duration of a datetime object and returns the new one.
Syntax |
$DateTime = SubtractDuration($OldDateTime, $DurationUnit, $Duration); $example $DateTime = SubtractDuration($OldDateTime, 'days', 1); |
Input |
|
Return |
|
UnloadPackage
Unloads a package to force it to be reloaded if needed next time. If a package name is invalid, this procedure dies with a proper message.
Syntax |
UnloadPackage($PackageName); |
Input |
|
UsePackage
Provides a save way to load a package instead of utilizing 'eval "use $Package"' If $Package contains not a valid package name, this procedure dies with a proper message. Otherwise, the package is loaded.
Syntax |
UsePackage($PackageName); |
Input |
|
fcmp
Compares two float numbers (avoid numeric problems, see example).
Syntax |
$result = fcmp($Float1, $Float2, $Precision); |
Example |
my $a = 0.00011; my $b = 1.10 - 1.09989; printf "%d != %d\n", fcmp($a,$b), $a <=> $b; |
Input |
|
Return |
|
isValid
Is string defined and not empty ?
Syntax |
$Valid = isValid($String); |
Input |
|
Return |
|
string_unique
Removes duplicates from a list and preserves the sort order from the beginning. The string representation of each value "$Value" is used to find duplicates. Useful for strings but also for DateTime objects.
Syntax |
@UniqueValues = string_unique @Values; |
Example |
print unique( "3", $a ) |
Input |
|
Return |
|
trim
Removes leading and training whitespaces from a list of values.
Syntax |
@TrimmedValues = trim @Values; |
Example |
print trim( " A", " B", "\t C\n" ); |
Input |
|
Return |
|
unique
Removes duplicates from a list and preserves the sort order from the beginning.
Syntax |
@UniqueValues = unique @Values; |
Example |
print unique( 3,2,1,2,3 ) |
Input |
|
Return |
|