Using Perl scalar variables
Scalar data is the one of the most basic and simplest data in Perl. Scalar data can be number or string. In Perl, string and number can be used nearly interchangeable. Scalar variable is used to hold scalar data. Scalar variable starts with dollar sign ($) followed by Perl identifier. Perl identifier can contain alphanumeric and underscores. It is not allowed to start with a digit.
Let's take a look in more details how we use scalar variables with number and string.
Number
Perl uses double-precision floating point values for calculation. Perl internally cheats integer as floating-point value. Perl uses literal to define number and with minus sign (-) to define negative number. Here is the code snippet to demonstrate scalar variable which holds number in Perl:
-
#floating-point values
-
$x = 3.14;
-
$y = -2.78;
-
-
#integer values
-
$a = 1000;
-
$b = -2000;
Perl also accepts string literal as a number for example:
-
$s = "2000"; # similar to $s = 2000;
In this case $s can be use as a number when calculation even though it is a string.
String
Perl defines string as a sequence of characters. The shortest string contains no character or null string. The longest string can contain unlimited characters which is only limited to available memory of your computer. Similar to number, Perl represents string by literal. A string can be wrapped in a single or double quotes. For example:
-
$str = "this is a string in Perl".
-
$str2 = 'this is also as string too'.
We defined two scalar variables which hold string in double quotes and sing quotes.
Operations on scalar variables
Perl uses arithmetic operators as another languages like C/C++ and Java. Here is the code snippet to demonstrate all of operators on numerical scalar variables.
-
$x = 5 + 9; # Add 5 and 9, and then store the result in $x
-
$x = 30 - 4; # Subtract 4 from 30 and then store the result in $x
-
$x = 3 * 7; # Multiply 3 and 7 and then store the result in $x
-
$x = 6 / 2; # Divide 6 by 2
-
$x = 2 ** 8; # two to the power of 8
-
$x = 3 % 2; # Remainder of 3 divided by 2
-
$y = ++$x; # Increase $x by 1 and store $x in $y
-
$y = $x++; # Store $x in $y then increase $x by 1
-
$y = --$x; # Decrease $x by 1 and then store $x in $y
-
$y = $x--; # Store $x in $y then decrease $x by 1
-
-
$x = $y; # Assign $y to $x
-
$x += $y; # Add $y to $x
-
$x -= $y; # Subtract $y from $x
-
$x .= $y; # Append $y onto $x
For string Perl use full stop (.) for concatenating strings and (x) for repeating a string.
-
$x = 3;
-
$c = "he ";
-
$s = $c x $x;
-
$b = "bye"; # $c repeated $x times
-
print $s . "\n"; #print s and start a new line
-
# similar to
-
print "$s\n";
-
-
$a = $s . $b; # Concatenate $s and $b
-
print $a;
Interpolation of Scalar Variables
Interpolation of scalar variables is a mechanism to convert a scalar variable inside a string into string. For example:
-
$x = 10;
-
$s = "you get $x";
-
print $s;
You will see the string "you get 10" in the output. $x variable is replaced by its value in string $s.