PERL Strings
String Literalsare groups of
characters surrounded by quotes so
that they can be used as a single
data.
They are frequently used in programs
to identify filenames, display
messages, and prompt for input.
In Perl you can use single quotes
('), double quotes("), and back
quotes (`).
Single-Quoted Strings
A single-quoted string is pretty simple. Just
surround the text that you'd like to use with
single quotes,
E.g. Hello
But what if you wanted to use a single
quote inside the literal?
Perl uses the backslash (\) character to
indicate that the normal function of the
single quote-ending a literal-should be
ignored for a moment.
Double-Quoted Strings
Onemajor differencebetween
double- and single-quoted strings is
that double-quoted strings have
some specialescape sequencesthat
can be used.
Escape sequences represent
characters that are not easily
entered using the keyboard or that
are difficult to see inside an editor
window.
Back-Quoted Strings
Itmight be argued that back-quoted strings are
not really a data type.
That's because Perl uses back-quoted strings to
execute system commands.
When Perl sees a back-quoted string, it passes
the contents to Windows, UNIX, or whatever
operating system you are using.
Let's see how to use the back-quoted string to
display a directory listing of all text files in the
perl5 directory:
print `dir *.txt`;
Variables
Perl has three types of variables:
Scalar-- denoted by a$symbol
prefix. A scalar variable can be either
a number or a string.
Array-- denoted by a@symbol
prefix. Arrays are indexed by
numbers.
Associative Array-- denoted by
a%symbol prefix. Arrays are
indexed by strings. You can look up
Scalar Variables
Scalar variables can be either a
number or a string
You can use variable types almost
interchangeably. Numbers first then
strings later
Numbers are numbers -- there is no
integer typeper se. Perl regards all
numbers a floating point numbers for
calculationsetc.
\n newline
\r carriage return
\t tab
\b backspace
\\ backslash character
\" double-quote character
\l lower case next letter
\L lower case all letters until \E
\u upper case next letter
\U upper case all letters until \E
\E Terminate \L or \E
Arrays
We can also have two forms of array:
1. Arrays - An array is a series of
numbers and strings handled as a
unit. You can also think of an array
as a list.
2. Associative Arrays - This is the most
complicated data type. Think of it as
a list in which every value has an
associated lookup item.
What is an Array?
Perl usesarrays-or lists-to store a series of
items. You could use an array to hold all of the
lines in a file, to help sort a list of addresses, or
to store a variety of items.
An array, in Perl, is an ordered list of scalar
data.
Each element of an array is an separate scalar
variable with a independent scalar value Arrays
can therefore havemixedelements, for
example
(1,"fred", 3.5) is perfectly valid.
Literal Arrays
Arrays can be defined literally in Perl
code by simply enclosing the array
elements in parentheses and
separating each array element with a
comma.
For example
(1, 2, 3) ("fred", "albert") () # empty
array (zero elements) (1..5) #
shorthand for (1,2,3,4,5)
Indexed Arrays
You declare an ordinary indexed array by giving it a name and prefixing it
with a@
Values are assigned to arrays in the usual fashion:
@array = (1,2,3);
@copy_array = @array;
@one_element = 1;
for example:
@array1 = (4,5,6);
@array2 = (1,2,3, @array1, 7,8); results in the elements
ofarray1being inserted in the appropriate parts of the list.
Therefore after the above operations
@array2 = (1,2,3,4,5,6,7,8) This means Lists cannot contain other lists
elements only scalars allowed.
Elements of arrays are indexed by index:
$array1[1] = $array2[3]; Assign ``third'' element ofarray2to ``first''
element ofarray1.
Each array element is a scalar so we reference each array element wth$.
shift()andunshift()
Likepush()andpop()except put
values on and take values off the left
side of an array.
Theshift()operation 'pops' the
element from the left side of the
array andunshift()'pushes' to the
left side of an array.
Associative Arrays
Associative arrays are a very useful and
commonly used feature of Perl.
Associative arrays basically storetablesof
information where the lookup is the right
handkey(usually a string) to an associated
scalar value. Again scalar values can be mixed
``types''.
Associative arrays are denoted by a verb| When
you declare an associative array the key and
associated values are listed in consecutive
pairs.