Scalar data is simplest kind of data Perl can work with. It is either a string or a number. A string is a series of 0 or more characters, usually placed in quotation marks. Numbers are positive or negative integers and reals. They are not placed in quotation marks. Perl supports a variety of number formats, such as scientific notation, hexadecimal and octal numbers.
A list is an ordered sequence of scalar data. A list can be composed as a comma-separated list enclosed in parentheses and can contain stirngs, numbers, or both.
Literals are data values that appear directly in a script. For example, the statement
print "Hello, world!\n";
prints a string literal. Likewise, the statement
$value = 5;
assigns the literal '5' to a variable called '$value'.
List literals are comma-separated lists in parentheses as, for example
(1, 2, 3)
('four', 5, 'six')
Variables are temporary storage containers in a computer's memory created during the execution of a program. Perl uses three basic variable types: scalars for scalar data, and arrays and hashes for list data. Each variable type is prefixed with a special character.
scalars:
$
arrays:
@
hashes:
%
Variables are given names by the programmer. A name must start with a letter or underscore character, followed by any combination letters, digits, or underscores of up to 256 characters in length. (Perl is case-sensitive.) It is good programming practice to a variable a name that is indicative of its use and function.
Scalars, arrays, and hashes can have the same name. Due to the identifier prefix, $var, @var, and %var are different entities and will always be distinguished by Perl.
Variables don't need to be declared, as in many other programming languages, and can be created anywhere in a script. Moreover, a variable can hold different data types in the course of a program.
Variables are assigned data (values) with the assignment operator, =. Before a variable is assigned a value, it has the undef (undefined) value. For scalar variables, undef evaluates (is equal) to 0 if it is used as a number, or "" as a string.
Values assigned to scalar variables must be either strings or numbers.
Assignment:
$myVar= "This a string literal"; $myNum = 3.34;
An array is an ordered list of scalar values. Perl indexes all elements in an array by number, starting at 0 with the first element and continuing sequentially to the last. A reverse index is also available, where -1 is an index to the last element, -2 to the penultimate, etc.
Values assigned to array variables must be a comma separated list enclosed in parentheses. Arrays may hold strings or/and numbers, even other variables.
Assignment:
@str_array = ("A", "list", "of", "words"); @num_array = (1, 3.4, -6, 0); @mix_array = ("A", 34, "of", $var);
Hashes (hash tables), also known as associative arrays, are also collections of scalar data. However, they are ordered not by a positional index, but as key/value pairs. The key, which must be a string, is the name of the data stored in the hash.
Hash values can be assigned in two ways: either as a comma-delimited list of even elements (where every odd-numbered element becomes the key to the following even-numbered element), or by using the => operator. The latter method is preferred since it is more explicit than comma-delimited list assignment.
Assignment:
# assignment by comma-delimited list %myHash = ('California', 'Sacramento', 'Wisconsin', 'Madison', 'New York', Albany'); # assgignment with the => operator %myHash = (California => "Sacramento", Wisconsin => "Madison" , "New York" => "Albany");