NAME

ScalarTypes::NumericTypes - Perl extension for checking numeric types of Perl scalars

SYNOPSIS

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";

# and so on for the other methods

DESCRIPTION

Implemented Methods

  • is_signed_integer()

  • is_unsigned_integer()

  • is_signed_float()

  • is_unsigned_float()

  • is_decimal()

  • is_binary()

  • is_octal()

  • is_hex()

  • is_upper_hex()

  • is_lower_hex()

  • is_roman()

  • is_signed_deccmanum()

  • is_unsigned_deccmanum()

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_int()

Following scalars return 1 (true):

'0'        -> 1
'6430'     -> 1
'12345678' -> 1

Following scalars return 0 (false):

'01234567' -> 0
' 823467'  -> 0
'521496 '  -> 0

A leading 0 and spaces before and after result in not valid result.

is_signed_int()

Following scalars return 1 (true):

'+1'       -> 1
'-6430'    -> 1
'+1245678' -> 1

Following scalars return 0 (false):

'-0'       -> 0
' +26367'  -> 0
'-52496 '  -> 0

is_unsigned_float()

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

is_signed_float()

Following scalars return 1 (true):

'+0.9'     -> 1
'-1.645'   -> 1
'+24.567'  -> 1

Following scalars return 0 (false):

'+.0'      -> 0
'-0.'      -> 0
' +2.3'    -> 0
'-3.4 '    -> 0

is_hex()

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.

is_upper_hex()

Same as is_hex(). Only uper case hex characters are valid.

is_lower_hex()

Same as is_hex(). Only lower case hex characters are valid.

is_binary()

The method returns true on a valid binary. A valid binary consist of 0 and 1.

Following scalars return 1 (true):

'0'        -> 1
'1'        -> 1
'0110011'  -> 1
'1011000'  -> 1

All other scalars are not valid.

is_roman()

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.

is_decimal()

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.

is_octal()

Octal numbers consist of numbers from 0 to 7.

is_signed_deccmanum()

Comma instead decimal point in signed decimal comma numbers.

is_unsigned_deccmanum()

Comma instead decimal point in unsigned decimal comma numbers.

Background

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.

SEE ALSO

Perl documentation Tutorials about Regular Expressions

AUTHOR

Dr. Peter Netz, <ztenretep@cpan.org>

COPYRIGHT AND LICENSE

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.