Home arrow Perl Programming arrow Page 2 - Scalars

Numbers - Perl

If you're writing a program that involves computation, you're going to come face to face with some basic programming concepts. This article, the first of a five-part series, will explain scalars, one of the basic ideas of programming in Perl. It is excerpted from chapter two of the book Beginning Perl, written by James Lee (Apress; ISBN: 159059391X).

  1. Scalars
  2. Numbers
  3. Binary, Hexadecimal, and Octal Numbers
  4. Strings
By: Apress Publishing
Rating: starstarstarstarstar / 2
March 25, 2010

print this article



There are two types of number that we are interested in as Perl programmers: integers and floating point numbers. The latter weíll come to in a minute, but letís work a bit with integers right now. Integers are whole numbers with no numbers after the decimal point like 42, Ė1, or 10. The following program prints a couple of integer literals in Perl.

#!/usr/bin/perl -w
# number1.pl

print 25, -4;

$ perl number1.pl

Well, thatís what you see, but itís not exactly what we want. Fortunately, this is pretty easy to fix. Firstly, we didnít tell Perl to separate the numbers with a space, and secondly, we didnít tell it to put a new line on the end. Letís change the program so it does that:

#!/usr/bin/perl -w
# number2.pl

print 25, " ", -4, "\n";

This will do what we were thinking of:

$ perl number2.pl
25 -4

For the purpose of human readability, we often write large integers such as 10000000 by splitting up the number with commas: 10,000,000. This is sometimes known as chunking. While we might write 10 million with a comma if we wrote a check for that amount, donít use the comma to chunk in a Perl program. Instead, use the underscore: 10_000_000. Change your program to look like the following:

#!/usr/bin/perl -w
# number3.pl

print 25_000_000, " ", -4, "\n";

Notice that those underscores donít appear in the output:

$ perl number3.pl
25000000 -4

As well as integers, thereís another type of numberófloating point numbers. These contain everything else, like 0.5, Ė0.01333, and 1.1.

Note that floating point numbers are accurate to a certain number of digits. For instance, the number 15.39 may in fact be stored in memory as 15.3899999999999. This is accurate enough for most scientists, so it will have to be for us programmers as well.

Here is an example of printing the approximate value of pi:

#!/usr/bin/perl -w
# number4.pl

print "pi is approximately: ", 3.14159, "\n";

Executing this program produces the following result:

$ perl number4.pl
pi is approximately: 3.14159

>>> More Perl Programming Articles          >>> More By Apress Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: