Scalar Variables
A scalar variable holds a single scalar value. The value represents either a number, a string or a reference to something. Scalar variable names begin with a dollar sign followed by a letter(s), digit(s), or underscore(s). All scalar variables are case-sensitive. Perl has three contexts in which it will interpret a scalar variable: string context, numeric context, and miscellaneous context.
Scalar Data Types:
The scalar data type is the most basic form of data container Perl has. Perl treats strings and numbers in a similar manner. You don't need to declare a scalar, just create it (use it).
$string = "YourString";
$number = 269;
$decimal = 49.42
Perl figures out by itself whether it is a float, integer, or string. If you want to include the symbols ( " ) or ( ; ) in your string, you will need to escape them by using ( \" ) and ( \; ). You can use q() for single quotes and qq() for double quotes as well. Please refer to Perl Quotes and Escape Sequences for more information.
| Type | Example |
|---|---|
| integer | $answer = 968; |
| real | $pi = 3.14159265 |
| scientific | $avogadro = 6.02e23 |
| string | $car = "BMW" |
| string with interpolation | $sign = "I love my $car" |
| string without interpolation | $cost = 'It costs $80000'; |
| another variable | $one = $two |
| expession | $force = $mass * $acceleration; |
| string output from a command | $cwd = 'pwd' |
| numeric status of a command | $exit = system("vi, $x") |
| an object | $car = new Car "BMW"; |
There is no way to declare a scalar to be of type "number" or "string". Perl converts between the various subtypes as needed, so you can treat a number as a string or a string as a number, and Perl will do the Right Thing. References (pointers), however, are not castable.
| 12345 | integer |
| 12345.67 | floating point |
| 6.02E23 | scientific notation |
| 0xffff | hexadecimal |
| 0489 | octal |
Scalar Operators:
Before performing an operation, perl operators decide the type of its operands. If the both or all operands (whichever is applicable) are scalars,then the result is a scalar. We would explore what happens if all operands are not scalar in the following chapters. This section would just briefly touch the topic of operators. Please refer to Perl Operators for more information. Perl supports common arithmetic operators like +, -, * , /, and %.
$a = 4 + 7; # $a = 11
$a = 4.9 + 3.9; # $a = 8.8
$a = 10 / 3; # $a = 3.3333333....
$a = 5 % 3; # $a = 2, remainder
Perl has different comparison operators for strings and numbers.
| Comparision | Numbers | Examples | Strings | Examples |
|---|---|---|---|---|
| Equal | == |
if($one == 5) { # do something; } |
eq |
if($string1 eq $string2) { # do something; } |
| Not Equal |
if($one != 5) { # do something; } |
ne |
if($string1 ne $string2) { # do something; } |
|
| Less Than |
if($one < 5) { # do something; } |
if($string1 lt $string2) { # do something; } |
||
| Greater Than |
if($one > 5) { # do something; } |
if($string1 gt $string2) { # do something; } |
||
| Less Than or Equal to |
if($one <= 5) { # do something; } |
if($string1 le $string2) { # do something; } |
||
| Greater Than or Equal to |
if($one >= 5) { # do something; } |
if($string1 ge $string2) { # do something; } |
There are two really handy operators for strings only. (.) and (x). The first concatenates strings. The other multiplies them:
"my " . "life."; # my life. This operator concatenates strings
"perl" x 3; # This is same as perlperlperl
From time to time, you would want to convert a string to a number and a number to string. This is how you do it. Suppose you have a $string "123" and a $string2 "234", then string = string + string2; would produce 357 (as a number, not as a string), not 123234.
To be fair, Perl also provides some operators which deal with numbers only. Namely autoincrement and autodecrement.
$a = 4; $b = 9;
$r = ++$a; # $r = 5. Increments before assignment
$r = $b++; # $r = 5, $b = 10. Increment after assignment
$b--; # postdecrement
--$b; # predecrement
Functions
chop
Chop function chops of the last character of a string scalar. It seems like a useless function but it does come in handy at times.
$r = "perls";
$r = chop($r); # $r = s
chop($r); # $r = perl
chomp
chomp deletes the last character only if it is a \n. It comes in handy.