# Perl Core Number Basics and Testing

Foreword: In Perl, a number can be a whole number, an integer, a real number or a real number in exponential form. In this tutorial I talk about numbers, and how they can be tested.

By: Chrysanthus Date Published: 14 Oct 2015

### Introduction

In Perl, a number can be a whole number, an integer, a real number or a real number in exponential form. In this tutorial I talk about numbers, and how they can be tested.

Pre-Knowledge
This tutorial is part of the volume, Perl Course. At the bottom of this page you have links to the different series you should have read before coming here.

Numbers

Whole Number
A whole number is an integer without the � or + sign. It is actually a positive number, also called an unsigned number. It can be assigned to a variable as in the following statement:

my \$wholeNum = 25;

Integer
An integer is a whole number, which can be positive or negative. If it is positive, the + sign can be omitted. It can be called a sign number. It can be assign to a variable as in the following cases:

my \$inter1 = -246;
my \$inter2 = +36;
my \$inter3 = 36;

Real Number
A real number is a number with a decimal point. If it is a negative number, it would have the negative sign. If it is a positive number it might or might not have a + sign. It can be assigned to a variable as in:

my \$rl = 2.5;

Note that the decimal part of the number may be zero. In that case it is equivalent to an integer.

Real Number in Exponential Form
A real number can be written in exponential form. For example, 268.3 can be written as,

2.683e2

You begin with a single digit whole number; then you have an optional decimal point. After, you have an optional decimal part; then e; and finally a number, which indicates the number of places the decimal point has to be shifted to get the real number. If the decimal point has to be shifted to the left, then this number has to be preceded by - . If the complete number is negative, then the complete number has to be preceded by - .

Testing Numbers
First of all let us see how to test if a variable is a string. The following binding expression, test if a variable is a string:

\$str=~/\D/

This tests if there is a non-digit character in the string. Remember, the binding operator returns true for success and false for failure. Try the following code:

use strict;

my \$str = "We are the world.";

print "Variable is a string" if \$str=~/\D/;

Whole Number
The following expression will return true if the variable is a whole number:

\$var=~/^\d+\z/

In the regex, \d means digit. ^ means match at the beginning of the string. + means match the preceding character 1 or more times. \z means match to the very end of the string, regardless of whether there is a newline character at the end or not. Try the following code:

use strict;

my \$var = 25;

print "It is a whole number." if \$var=~/^\d+\z/;

Integer
The following expression will return true if the variable is an integer:

\$var =~ /^[+-]?\d+\z/

[+-] means + or -. ? means match the preceding character, 0 or 1 time. Try the following code:

use strict;

my \$var = -246;

print "It is an integer." if \$var =~ /^[+-]?\d+\z/;

Real Number
The following expression will return true if the variable is a real number:

\$var =~ /^-?(?:\d+\.?|\.\d)\d*\z/

I leave the explanation of the regex as an exercise for you. Try the following code:

use strict;

my \$var = 2.5;

print "It is a real number." if \$var =~ /^-?(?:\d+\.?|\.\d)\d*\z/;

Real Number in Exponential Form
The following expression will return true if the variable is a real number in exponential form:

\$var =~ /^[+-]?(?=\.?\d)\d*\.?\d*(?:e[+-]?\d+)?\z/i

Note that the e in the exponential form can also be E; that is why you have /i at the end of the regex. I leave the explanation of the rest of the regex as an exercise for you. Try the following code:

use strict;

my \$var = 2.683e2;

print "It is a real number." if \$var =~ /^[+-]?(?=\.?\d)\d*\.?\d*(?:e[+-]?\d+)?\z/i;

That is it for this part of the series.

Chrys

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Miscellaneous Features in Perl
Perl Two-Dimensional Structures