Summary:
What are variables?
What can I store in a variable?
What can I do with a variable?
Introduction
to Variables
It wouldn't
be much fun if all you could do was print things out that you'd already
typed in.
Dynamic data, that is, data that can change while the program is running,
is stored in structures called variables.
If you've had experience with programming in other languages, you might
be familiar with different types of variables.
In Perl, there are three types of variables: Scalar, Array, and Hash.
Variable Types
Scalars are
the most basic type of variable, and store one piece of
information. Scalars are named beginning with the '$' character, like
`$_` or
`$my_scalar`. An array, or list, is essentially multiple scalars collected
together in order where they can be accessed by their number, or 'subscript'.
Arrays are named beginning with the `@` character, like `@ARGV` or `@my_array`.
Hashes, or 'linked lists', are like arrays, but instead of having the
component
scalars in order, each scalar is linked to an arbitrary value. Hashes
are named
beginning with `%`, like `%ENV` or `%my_hash`. Note that the examples
given
(`$_`, `@ARGV`, and `%ENV`) have special meaning in Perl and will be
discussed
later.
In Perl, like many
languages, variable names (along with function
names, file handles, etc) are case-sensitive. This means that `$this`,
`$This`,
and `$THIS` are all different scalars. Be careful to ensure that you
use the
right case when accessing scalars or any other variable type.
In this lesson, we'll talk about scalars and how to use them. Arrays
and hashes have their own lessons where they'll be discussed in detail.
If the
previous paragraph confused you, don't worry. It's just an overview
and you
don't need to understand it at this point.
Dynamic Typing
Some programming
languages use what is called 'static typing'. This
means that once a variable is declared as (for example) a string type,
it can
be used only to store strings (as opposed to integers, floating-point
numbers,
etc.). Perl uses 'dynamic typing' instead. Dynamic typing means that
a scalar
variable can hold any kind of data and switch between them.
Scalars
Storing a value in a scalar uses the `=` assignment operator. For
example, to store the words `This is a string` in the scalar $foo, you
would
use this syntax:
-------------------------------------
$foo = 'This is a string';
-------------------------------------
Storing a numeric
value is much the same, only the quotes are not
required. Quotes are interpreted the same way as with the print statement.
If
you want to include the value of another variable inside an assignment,
you can
use doublequotes, like this:
-------------------------------------
$name = 'Mike';
$longerstring = "My name is $name.";
-------------------------------------
There's another
way of doing it, using the `.` string concatenation
operator. We'll look at that next lesson, though. Numbers can be treated
the
same way. This is a result of dynamic typing.
Retrieving Scalar Values
Scalars can be
printed just like quoted strings, or as part of a quoted
string. An example looks like this:
-------------------------------------
$name = 'Tiffany';
$longerstring = "My name is $name.\n"; # The \n is so that
the next print
# statement prints on the next line.
print $longerstring;
print "This is another way of saying my name is $name.\n";
-------------------------------------
Special Scalar Variables
Some scalar variables have special meaning in Perl. Of note are `$_`,
`$&`, `$0`, and `$$`.
`$_`, the most
likely to be encountered in CGI programming and possibly the most useful
of the four,
is essentially the current working space. Functions like `print` and
`chomp`, regular expression
switches like `s///`, and other miscellaneous functions will take input
and put output in `$_` if you
don't specify otherwise.
`$&` gets filled with any error message from the last called child
process.
This is useful when investigating what happened if file open calls,
etc. don't work.
`$0` contains the filename of the currently running script.
`$$` contains the process ID of the currently running script (at least
on unix-like systems). If you don't know what this means, don't worry
about it.
Most of these variables
(especially `$_` and `$&`) will take on more
meaning as you learn more about Perl. For more information about these
and
other special variables, check
http://language.perl.com/newdocs/pod/perlvar.html.
The Difference Between `=` and `==`
Important Note:
Later on, you'll learn about another operator that
looks much like `=`. In fact, it's `==`, and it's used for *comparing*
two
numbers. They are *NOT* interchangable. This will be emphasized again
when `==`
is discussed in more detail, but it's important enough that it's worth
mentioning now.
|