ScalarTypes::NumericTypes - Perl extension for checking numeric types of Perl scalars
use ScalarTypes::NumericTypes; # Declare the teststring. my $teststring = undef; # Test unsigned integer. $teststring = "12345678"; # Valid unsigned integer. $testresult = is_unsigned_int($teststring); # Returns 1 (true) print $testresult . "\n"; # Test signed integer. $teststring = "-12345678"; # Valid signed integer. $testresult = is_signed_int($teststring); # Returns 1 (true) print $testresult . "\n"; # Test unsigned float. $teststring = "1234.5678"; # Valid unsigned float. $testresult = is_unsigned_float($teststring); # Returns 1 (true) print $testresult . "\n"; # Test signed float. $teststring = "+1234.5678"; # Valid signed float. $testresult = is_signed_float($teststring); # Returns 1 (true) print $testresult . "\n"; # Test unsigned float with separator. $string = "1234,5678"; # Valid unsigned float. $sep = ","; # Decimal comma instead of decimal point. $testresult = is_unsigned_sepdec($string, $sep); # Returns 1 (true) print $testresult . "\n"; # Test unsigned float with separator. $string = "+1234,5678"; # Valid signed float. $sep = ","; # Decimal comma instead of decimal point. $testresult = is_signed_sepdec($string, $sep); # Returns 1 (true) print $testresult . "\n";
is_signed_sepdec()
is_unsigned_sepdec()
is_signed_float()
is_unsigned_float()
is_signed_integer()
is_unsigned_integer()
is_decimal()
is_binary()
is_octal()
is_hex()
is_upper_hex()
is_lower_hex()
is_roman()
All of the implemented methods return 1 (true) or return 0 (false). A subroutine argument is necessary. If no argument is given in the subroutine call the argument is set to an empty string.
is_unsigned_float($string)
If $string is undefined or missing in the subroutine call the methods sets the argument $string to ''.
$string
A unsigned float in the context of the method consists of numbers from 0 to 9 and a decimal dot as separator. Before and after the separtor there must be a valid number. Spaces before and after the number are not allowed. A sign like + or - is not allowed in front of the number. The seperator must be a decimal point.
Following scalars return 1 (true):
'0.0' -> 1 '0.9' -> 1 '1.645' -> 1 '124.567' -> 1
Following scalars return 0 (false):
'.0' -> 0 '0.' -> 0 ' 1.3' -> 0 '3.1 ' -> 0 ' 0.0 ' -> 0 '4,5' -> 0 '+9.2' -> 0 'abc' -> 0
is_signed_float($string)
A signed float in the context of the method consists of numbers from 0 to 9 and a decimal dot as separator. Before and after the separtor there must be a valid number. Spaces before and after the number are not allowed. A sign like + or - is required in front of the number. The seperator must be a decimal point.
'+0.9' -> 1 '-1.645' -> 1 '+24.567' -> 1
'+.0' -> 0 '-0.' -> 0 '.0' -> 0 '0.' -> 0 ' 0.0 ' -> 0 ' -6.37 ' -> 0 ' +2.3' -> 0 '-3.4 ' -> 0 ' 1.3' -> 0 '3.1 ' -> 0 '4,5' -> 0 'abc' -> 0
is_unsigned_int($string)
A unsigned integer consists of numbers from 0-9.
'0' -> 1 '6430' -> 1 '12345678' -> 1
'01234567' -> 0 ' 823467' -> 0 '521496 ' -> 0
A leading 0 and spaces before and after result in not valid result.
is_signed_int($string)
A signed integer consists of numbers from 0-9.
'+1' -> 1 '-6430' -> 1 '+1245678' -> 1
'-0' -> 0 ' +26367' -> 0 '-52496 ' -> 0
is_hex($string)
The subroutine checks whether the specified argument is a valid hexadecimal number. A hexadecimal number in the context of this method is a number consisting of integers from 0-9, lower case characters from a-f or upper case characters from A-F. Spaces before and after the number itself are not allowed. The subroutine returns 1 (true) or 0 (false) based on the check.
'1f' -> 1 '0E' -> 1 '2Ad3 ' -> 1
is_upper_hex($string)
Same as is_hex(). Only uper case hex characters are valid.
is_lower_hex($string)
Same as is_hex(). Only lower case hex characters are valid.
is_binary($string)
The method returns true on a valid binary. A valid binary consist of 0 and 1.
'0' -> 1 '1' -> 1 '0110011' -> 1 '1011000' -> 1
All other scalars are not valid.
is_decimal($string)
The decimal numeral system is the standard system for denoting e.g. integer numbers. In the context of this method numbers from 0 to 9 are allowed. A leading 0 is also allowed. In the base 10 system, which the decimal system is, each digit is multiplied by a power of 10 according to its place and than summed up.
'01' -> 1 '20' -> 1 '2345' -> 1 '0967' -> 1
is_octal($string)
Octal numbers consist of numbers from 0 to 7.
'01' -> 1 '70' -> 1 '672' -> 1 '0254' -> 1
is_signed_sepdec($str, $sep)
If $str is undefined or missing in the subroutine call the methods sets the argument $str to ''. If $sep is undefined or missing in the subroutine call the methods sets the argument $sep to ''.
$str
$sep
E.g. comma instead decimal point in signed decimal comma numbers.
Following scalars return 1 (true) if separator is a comma:
'0,1' -> 1 '70,34' -> 1
is_unsigned_sepdec($str, $sep)
E.g. comma instead decimal point in unsigned decimal comma numbers.
'+0,1' -> 1 '-70,34' -> 1
is_roman($string)
The method returns true on a valid roman number. A valid roman number consist of uper case letters I, V, X, L, C, D and M.
I -> 1 V -> 5 X -> 10 L -> 50 C -> 100 D -> 500 M -> 1000
The method does not check whether the Roman numeral is valid according to the Roman calculation rules. It is only checked whether the permitted number symbols are contained in the number.
'I' -> 1 'LM' -> 1 'CDI' -> 1 'MMXXII' -> 1
Regular expressions or short written regex are used to check the scalars. According to the Perl documentation, \d recognises not only the numbers 0 to 9, but other number signs. Accordingly, the methods of this module use e.g. [0-9] for the recognition of numbers.
Perl documentation Tutorials about Regular Expressions
Dr. Peter Netz, <ztenretep@cpan.org>
Copyright (C) 2022 by Dr. Peter Netz
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.30.0 or, at your option, any later version of Perl 5 you may have available.
To install ScalarTypes::NumericTypes, copy and paste the appropriate command in to your terminal.
cpanm
cpanm ScalarTypes::NumericTypes
CPAN shell
perl -MCPAN -e shell install ScalarTypes::NumericTypes
For more information on module installation, please visit the detailed CPAN module installation guide.