473,508 Members | 4,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ


Variables in Perl

By Blair Ireland
Senior Editor, TheScripts.com

Time to learn the different types of variables available within Perl.

Variables are basically handy places to store something. In other words, it is just the name of a place so you know where to find it later.

Scalar Variables

Scalar variables are identified with the '$' character infront of the variable name. So a variable with the name of "thing" would be written as $thing. You assign values to these variables by using the = operator, just like many other programming languages. All scalar variables have a singular (scalar) value to them. Scalar variables do not have to be pre-defined, as other languages usually require.

The $ identifies the variable as scalar.

Scalar Variables can also change their type during runtime, for example, say we have the variable $stuff. We can change it like so;

$stuff = "123";
print $stuff + 1 . "\n";

The scalar variable $stuff was originally a string. During the print statement though, it was converted to a number to add 1 to it, then converted back to a string. Therefore, the output would be 124.

Another thing you can do is utilize the original variable value when you redeclare that variable. Sound confusing?

Just look at the next example.

$lucky = 7;
$lucky = "My lucky number is $lucky";

This ability comes in extremely handy later on when you are making scripts.

Arrays

Arrays are identified by the @ prefix. Arrays, in short, are plural versions of scalar variables. Within an array, you can store many variables. The syntax for an array is shown in the example below;

@one_to_ten =("one","two","three","four","five","six","seven","eight","nine","ten");

Perl arrays are index from the number 0 and up though. Therefore, when you want to access a single value from the array, you have to keep this in mind. Say I wanted to print the value "seven" from @one_to_ten. I would do so using the following syntax;

print "$one_to_ten[6]\n";

It's that easy. If you want to print all the values of @one_to_ten though, you will utilize the function "foreach". foreach is used like so;

foreach $number (@one_to_ten) {
  print "$number\n";}

The $number part of the foreach function can be named whatever you want it to be. It is basically the reference you are using to each value of the array.

Hashes

A hash is not a normal type of array, it is known as an associative array or a paired group of elements. Hash names in perl have the prefix %, or the percent sign. They consist of pairs of elements.... keys and the value. You will find an example on the most readable way to declare a hash below;

%mysite = ( 
  "scripts" => "http://www.mysite.com/scripts/",
  "forums" => "http://www.mysite.com/forums/",
  "reviews" => "http://www.mysite.com/reviews/"
  );

Now if I wanted to print just the "scripts" part of the mysite hash, I would do so with the following;

print "$mysite{'scripts'}\n";

If you want to print all of the values of a hash though, you will have to use the foreach function again, but a little differently;

foreach $key (keys %mysite) {
     print "$key can be found at: $mysite{$key}\n";
}

Keys are the identifying element of a hash. In the %mysite, the keys are scripts, helpforums, and reviews.

« Perl Subroutines Perl Functions »

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.