Lesson 1
Introduction to Perl Variables
This module will discuss the basic vocabulary of Perl, namely its
variable types. This will allow you to write simple scripts and, more importantly, it prepares you to go on to learn the operators and other syntax you need to really use the language effectively. Like most languages, Perl categorizes data of different
types and requires that they be represented with specific syntax. This module will teach you about those different types of data and how to represent them.
Perl Scalar Variables
Scalar variables have a leading $ for example, these are all valid Perl scalar variables:
$n $N
$var28
Case matters in Perl, so the first two examples are different variables. The final variable ''$'' is special, one of many that the Perl interpreter will use by default in various operations if the programmer does not indicate otherwise.Any particular valid identifier can be used to designate a scalar, an array, and a hash. The leading character determines which of the three types the variable has. For example, here the identifier
name is used to denote threevdifferent variables:
$name @name %name
- The first is a scalar;
- the second is an array;
- the last is a hash.
All three can be used concurrently, as they denote different storage areas. A variable of the type scalar can contain any scalar value:
$v1 = 'good morning';
$v1 = 127;
The first assignment places a string value in the variable. The second replaces the string with an integer value.
This is different from many strongly typed languages (such as C++ and Java), where types are finely divided into categories such as integer, real, string, and boolean. In Perl these are values, but not types; Perl uses type distinction mainly to separate singular entities (scalar) from collective entities (arrays and hashes). String values are delimited with either single or double quotes. Single-quoted literals are used exactly as written,
whereas double-quoted literals are subject to escape character and variable interpolation before the final value is obtained. For example:
$numer = 2;
$st1 = 'one fine $numer day';
$st2 = "$numer fine day \n";
print $st2;
print "$st1\n";
The output from this script is
2 fine day
one fine $numer day
Perl Interpolations
Four interpolations have taken place. In the second line, the $numer appears to be a use of a variable, but because the string is in single quotes the characters are included as they appear. In the third line the string literal is in double quotes, so the variable name is replaced with its current value (2) to produce the final string value; vthe escape sequence ''\n" also puts in a newline character. The first print statement shows the result. The second print shows two interpolations as well, as the string being printed is in double quotes. The value in $st1 is interpolated into the output string and then a newline is added to the end. Even though $st1 has ''$numer" in its value, this is not recursively interpolated when those characters are put into the output string.
Learning a new language is difficult, partly because you sometimes must use the language to explain the language.
There are
examples in this course that use language elements that have not yet been thoroughly explained.
- Perl Language Elements
When it is necessary to use language elements that have not yet been thoroughly explained, you will usually find a comment telling you that this concept will be covered later. With that in mind, remember that the order of the lessons has been carefully chosen to give you the foundation for each lesson before it is presented.
Sometimes that's a very difficult thing to do, because it's hard to explain arrays without explaining scalars first, and yet there are distinctions in scalars that require arrays to explain.
Perl is a dynamic programming language created by Larry Wall and first released in 1987.
Perl borrows features from a variety of other languages including C, shell scripting (sh), AWK, sed and Lisp.
Structurally, Perl is based on the brace-delimited block style of AWK and C, and was widely adopted for its strengths in string processing and lack of the arbitrary limitations of many scripting languages at the time.
Why was Perl created?
There are no shortage of computer languages, especially in 2018 where languages come and go fairly rapidly?
But, at the time, Larry did not see anything that really met his needs. If one of the other languages of today had been available back then, perhaps Larry would have used one of those.
He needed something with the quickness of coding available in
- shell or
- awk programming,
and with some of the power of more advanced tools like
- grep,
- cut,
- sort, and
- sed,
- #
without having to resort to a language like C.
Perl tries to fill the gap between low-level programming (such as in C or C++ or assembly) and high-level programming (such as shell programming).
Low-level programming
is usually hard to write and ugly, but fast and unlimited and it is hard to beat the speed of a well-written low-level program on a given machine.
And there is not much you cannot do there.
High-level programming, at the other extreme, tends to be slow and limited; there are many things you cannot do at all with the shell or batch programming if there is no command on your system that provides the needed functionality.
Perl is easy, nearly unlimited, mostly fast, and kind of ugly.
We will presume that you have already studied Learning Perl, using at least the fifth edition, or at least pretend you have, and that you have played enough with Perl to already have those basics down. Make sure you know the following things.
- How to run a Perl program on your system
- The three basic Perl variable types: scalars, arrays, and hashes
- Control structures such as while, if, for, and foreach
- Subroutines
- Basic regular expressions
- List operators such as grep, map, sort, and print
- File manipulation such as open, file reading, and -X (file tests)
You might pick up deeper insight into these topics in this course, but we are going to presume you know the basics.
The final parts of this course deal with distributions and contributing to CPAN.
Variables are nothing but reserved memory locations to store values.
When you create a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory.
Therefore, by assigning different data types to variables, you can store integers, decimals, or strings in these variables.
Perl has following three basic data types:
- Scalars
- Arrays
- Hashes
Accordingly we are going to use three types of variables in Perl.
A scalar variable will be preceded by a dollar sign ($) and it can store either a number, a string, or a reference.
An
array variable will be preceded by the @ symbol and it will store ordered lists of scalars.
The
hash variable will be preceded by the % symbol and will be used to store sets of key/value pairs.
Perl maintains every variable type in a separate
namespace.
You can use the same name for a scalar variable, an array, or a hash.
This means that $foo and @foo are two different variables.
- Basic Data Types and Values
Perl provides three types of data for programmers to manipulate:
- scalar,
- array, and
- hash (associative array).
Scalar types are well known to most programmers, as is the array type. The hash is less well known and one of the most powerful aspects of Perl. Scalar values include integer, real, string, and boolean, with the expected operations available for each. Variables do not have to be declared before use; the first character indicates the type.