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
- CheckIpFormat
- Compare
- CopyStructure
- CreateJsonWebToken
- CreateNewPassWD
- CryptPassword
- ElementOf
- GenerateGUID
- GenerateShortGUID
- GetApplicationServerPort
- GetArchName
- GetCallerFunction
- GetCallerPackage
- GetCallerStack
- GetCallerString
- GetCurrentDateTime
- GetDoubleHashFromLoop
- GetIPAddress
- GetIPAddresses
- GetLocalLocale
- GetLocalTimeZone
- GetLoopFromDoubleArray
- GetMD5Digest
- GetPackageFileName
- GetWindowsVersion
- Hex2Str
- IndexOf
- Ip2Hex
- IsA
- Memoize
- Str2Hex
- Str2PerlStrDoubleQuote
- Str2PerlStrSingleQuote
- SubtractDuration
- UnloadPackage
- UsePackage
- castToBoolean
- 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 |
|
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 |
|
Return |
|
Compare
Different compares between a and b depending on the type of both elements. Comparing two objects ignores input $Type, both objects must implement method id(). Function dies if comparison is not supported for given input. Note older versions didn't check parameter $Type properly, instead raising an error, undef was returned as result for the broken comparison.
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 |
|
CreateJsonWebToken
Returns a hex-encoded JSON web token, with HMACSHA256 as algorithm.
Syntax |
CreateJsonWebToken($Lifetime, $Subject, $Scope, $Issuer, $Auditor, $Secret) |
Input |
|
Return |
|
CreateNewPassWD
Obsolete. Use E_EPAGES::Core::API::Password::GenerateNewPassword. Generates a plain random word containing only alphanumeric characters.
Syntax |
$Password = CreateNewPassWD($UserName, $Length); |
Input |
|
Return |
|
CryptPassword
Obsolete. Use DE_EPAGES::Core::API::Password::CryptPassword. Generates a bcrypt hash of the password.
Syntax |
$Hash = CryptPassword($Password); |
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). Performance note: the expression ($Search ~~ @List) is much faster than this function.
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 ); $CallerString = GetCallerString( $aaCallers, $SkipCallers, $MaxCallers ); |
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 |
|
GetPackageFileName
return the name of corresponding file for a package
Syntax |
$PackageFileName = GetPackageFileName($PackageName); |
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 |
|
Memoize
Creates a faster version of a sub that caches all results. Caveats: * the return value must be a single scalar * the function parameters cannot be references * the cache key is join('-',@_), i.e. there may be clashes if parameters contain a '-' character
Syntax |
$FastSub = Memoize($Sub); |
Example |
*Fast_Fib = Memoize( \&Fib ); print Fast_Fib(30); |
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. NOTE: Unlike AddDuration this function also modifies the input paramter $DateTime.
Syntax |
$DateTime = SubtractDuration($DateTime, $DurationUnit, $Duration); $example $DateTime = SubtractDuration($DateTime, '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 |
|
castToBoolean
cast any $Value into an predictable boolean output mapping table: * undef = false * '' = false # As a boolean this is false, as a String it would be true * '0' = false # As a boolean this is false, as a String it would be true * 'undef' = true # This is a normal String * 1 = true * 0 = false * [] = true # The array pointer is defined, although the list is empty * @a; = false * @a = (); = false * @a = (undef); = true # This is a list with one element, which is undef * @a = ('one'); = true * {} = true # The hash pointer is defined, although the Hash is empty * %a; = false * %a = (); = false * %a = ('' => undef); = true # This is a hash with one key-value pair, which is undef * %a = ('a' => 'b'); = true
Syntax |
castToBoolean( $Value ); |
Input |
|
Return |
|
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 |
|