Data Types, Scalar Variables and Operators

Unlike some other languages, Perl does not have seperate data types for holding boolean, integer, or floating point values. The basic data type is the scalar which can hold a string or a numeric value. Whether the value acts as a string or a number depends upon the context in which it is being used.

A scalar variable begins with the '$' character, followed by a letter, and then by other letters, numbers or the underscore character, '_'.


Assigning a value to a scalar variable

Here are some examples of assigning values to scalar variables:

$date = "November 17, 1998";   # a string

$date2 = "";                   # an empty string

$amount = 120.45;              # a floating-point number

$Legal_Age = 18;               # an integer


In Perl, variables do not need to declared before they can be used.


Comments

The "#" character in Perl denotes a comment. Unlike C, there are no nested comments. Any line that needs to be commented needs a "#" character.


Operators

Perl offers the standard operators you would expect in a programming language plus several additional ones


Numeric Operators

+    # addition
-    # subtraction
*    # multiplication
/    # division
**   # exponentiation


Logical Operators

&&   # Logical AND
!    # Logical NOT
||   # Logical OR


Relational Operators

For Numbers:

==   # equal to
!=   # not equal to
>    # greater than
<    # less than
>=   # greater than or equal to
<=   # less than or equal to

For Strings:

eq   # equal to
ne   # not equal to
gt   # greater than
lt   # less than
ge   # greater than or equal to
le   # less than or equal to


Other Operators


++  # pre- and post- increment
--  # pre- and post- decrement

.   # string concatenation (add two string together)

Note: This is not a complete list. See the Perl Desktop Reference or the standard Perl Documentation for more operators.



Automatic Type Conversion
$a = 3;
$b = 4;
$c = $a + $b; # Two integers are added, c = 7


Now, we use the string concatenation operator (the period) and add the number on to the end of a string.


$d = "The sum is";               # The concatenation operator expects a string
$e = "1st Answer: " . $d . $c;   # The value of c is converted to a string
                                 # before it is added. 
                                 # $d = "1st Answer: The sum is 7"

$f = $e + 3;          # The addition operator expects a number
                      # $e is converted to a number to be added
                      # non-numeric characters and numbers that follow are ignored
                      # $f = 1 + 3 = 4;


Precedence

Operations with the same precedence occur in order, left to right. (Note: a few operators don't behave this way). '+' and '.' have the same precedence.

$a = "Answer is " . 3 + 4; # $a = "Answer is 3" + 4 = 0 + 4 = 4

Operations surrounded by parentheses are evaluated first.

$b = "Answer is ". (3 + 4); # $b = "Answer is " . 7 = "Answer is 7"


See the Perl Desktop Reference or the standard Perl Documentation for complete details on the precedence of operators..