A listing of the various varaible types in perl
One of the best(or worst, depending on your viewpoint)things about perl is the lack of typing as far as variables go, lets look at an example:
Say you need to store a floating point number such as 1.09
that's it, no typecasting at all, just give it a name starting with $, and it's stored.
this is called a scalar variable, and can store one thing of any type, numbers, text, whatever.
Code:
$number = 1.09;
$whole_number = 15;
$text = "Hello World";
perl dosn't care what you put into a scalar, it just stores it and moves on.
Another storage type in perl is the array
If you are at all familar with anyother programming languages, arrays are old stuff, but lets review(or look at for the first time, depending on your skill level)
arrays in perl all start with the @ symbol
Code:
@stuff = ("book", "table", "lamp");
elements in an array are accesed like this:
this would output "book"
remember that arrays always start from 0 and go up, it is common to forget that leading to off-by-one errors.
arrays in perl are not static, if you need to add something to an array, go ahead, perl won't mind one bit
lets add something to our sample array
Code:
push @stuff, "chair";
print @stuff;
this would output "book table lamp chair" just like you would expect
another thing to remember is that perl dosn't care about if what you are storing is a string or numbers or whatever.
Hashes
the last major storage type in perl is the hash
Hashes are unordered arrays indexed by keywords, in other words a hash is like a mixed bag of things, no real order, this is a hashes strength though, because unlike arrays, hashes stay fast no matter how many variables are added to it.
here is an example of a hash
Code:
%days=(
"Sun" => "Sunday,
"Mon" => "Monday",
"Tue" => "Tuesday",
"Wed" => "Wednesday",
"Thu" => "Thursday",
"Fri" => "Friday",
"Sat" = > "Saturday"
);
to access a element in a hash, you do this
Code:
print $days("Mon");
This will print Monday, just like you would expect
note that when you access an individual element in a hash, you use a $ instead of a %, this is easy to forget, but will cause an error.