Number is a scalar type in perl programming. Number are used to perform mathematical operation. In perl programming there are main two types of numbers is generally used numeric value such as integers and floating point numbers. Generally numbers are used by decimal system which is can be form of positive and negative values.
#!/usr/bin/perl use warnings; use strict; #Integer numeric value my $age = 21; #Floating point numbers my $salary = 89333.245; #Negative integer value my $lose = -1000 ; print $age,"\n"; print $salary,"\n"; print $lose,"\n";
21 89333.245 -1000
In this example all three variables such as $age , $salary and $lose are storing the numeric literal values.
There are 4 ways to representing a number. Such as binary, octal, decimal and hexadecimal. In perl programming all those formats can be used.
#!/usr/bin/perl use warnings; use strict; #assign binary literals my $binary = 0b1010; #assign octal literals my $octal = 012; #assign decimal literals my $decimal = 10; #assign hexadecimal literals my $hexadecimal = 0xA; print $binary,"\n"; #10 print $octal,"\n"; #10 print $decimal,"\n"; #10 print $hexadecimal,"\n"; #10
10 10 10 10
Note that result is always in form of a decimal number system. Internally before initialize a value to variable according to prefix value are converted into a decimal.
Formatting Numeric literals
Generally number is separating by comma for separating a long numbers for example (9,48,5,20) is representing a number. But In programming language comma are not a part of number. So we can use (_) underscore to separating long number literals.
#!/usr/bin/perl use warnings; use strict; my $population = 89_69_47_89; my $salary = 9_48_5_20.87_5_10; print $population,"\n"; print $salary,"\n";
Note that number formatting are used to provide readability of programmer internally perl number are not use underscore.