The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

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

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

DESCRIPTION

Implemented Methods

  • 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.

Method is_unsigned_float()

Method call

    is_unsigned_float($string)

If $string is undefined or missing in the subroutine call the methods sets the argument $string to ''.

Method description

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.

Examples of return values

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

Method is_signed_float()

Method call

    is_signed_float($string)

If $string is undefined or missing in the subroutine call the methods sets the argument $string to ''.

Method description

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.

Examples of return values

Following scalars return 1 (true):

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

Following scalars return 0 (false):

    '+.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

Method is_unsigned_int()

Method call

    is_unsigned_int($string)

If $string is undefined or missing in the subroutine call the methods sets the argument $string to ''.

Method description

A unsigned integer consists of numbers from 0-9.

Examples of return values

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.

Method is_signed_int()

Method call

    is_signed_int($string)

If $string is undefined or missing in the subroutine call the methods sets the argument $string to ''.

Method description

A signed integer consists of numbers from 0-9.

Examples of return values

Following scalars return 1 (true):

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

Following scalars return 0 (false):

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

Method is_hex()

Method call

    is_hex($string)

If $string is undefined or missing in the subroutine call the methods sets the argument $string to ''.

Method description

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.

Examples of return values

Following scalars return 1 (true):

    '1f'       -> 1
    '0E'       -> 1
    '2Ad3 '    -> 1

Method is_upper_hex()

Method call

    is_upper_hex($string)

If $string is undefined or missing in the subroutine call the methods sets the argument $string to ''.

Method description

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

Method is_lower_hex()

Method call

    is_lower_hex($string)

If $string is undefined or missing in the subroutine call the methods sets the argument $string to ''.

Method description

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

Method is_binary()

Method call

    is_binary($string)

If $string is undefined or missing in the subroutine call the methods sets the argument $string to ''.

Method description

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

Examples of return values

Following scalars return 1 (true):

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

All other scalars are not valid.

Method is_decimal()

Method call

    is_decimal($string)

If $string is undefined or missing in the subroutine call the methods sets the argument $string to ''.

Method description

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.

Examples of return values

Following scalars return 1 (true):

    '01'       -> 1
    '20'       -> 1
    '2345'     -> 1
    '0967'     -> 1

Method is_octal()

Method call

    is_octal($string)

If $string is undefined or missing in the subroutine call the methods sets the argument $string to ''.

Method description

Octal numbers consist of numbers from 0 to 7.

Examples of return values

Following scalars return 1 (true):

    '01'       -> 1
    '70'       -> 1
    '672'      -> 1
    '0254'     -> 1

Method is_signed_sepdec()

Method call

    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 ''.

Method description

E.g. comma instead decimal point in signed decimal comma numbers.

Examples of return values

Following scalars return 1 (true) if separator is a comma:

    '0,1'      -> 1
    '70,34'    -> 1

Method is_unsigned_sepdec()

Method call

    is_unsigned_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 ''.

Method description

E.g. comma instead decimal point in unsigned decimal comma numbers.

Examples of return values

Following scalars return 1 (true) if separator is a comma:

    '+0,1'      -> 1
    '-70,34'    -> 1

Method is_roman()

Method call

    is_roman($string)

If $string is undefined or missing in the subroutine call the methods sets the argument $string to ''.

Method description

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.

Examples of return values

Following scalars return 1 (true):

    'I'        -> 1
    'LM'       -> 1
    'CDI'      -> 1
    'MMXXII'   -> 1

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.