Monday, December 05, 2005
Perl: Variables
To do anything useful, you need to be able to store values in temporary
locations and perform manipulations on them. We all know these as
"variables." Perl actually has several kinds of variables, known more
generically as "data structures." We'll use word "variable" to talk about
a specific instance of a data structure (or "data type"). Some data types
you may be familiar with:
C: int, float, char, struct
C++: class
Fortran: integer, real, character
All of the above are "strongly typed," which means you must explicitly
declare variables before you use them. Languages such as lisp or smalltalk
do not work this way - data types are determined dynamically, and if a
variable holds a number, the programmer is responsible for making sure
that the program doesn't try to pull substrings out of it. All languages
have internal representations for the different data types, they just vary
in how much the programmer assists in making sure the variable is in the
right form.
Perl falls in the middle. Which data type you use explicit in how you
access it, but you don't need to declare it before you use it.