User Tools

Site Tools


handbook:handbook:perl3

Advanced concept

Variables

Comparing to many of the other programming languages, variables in Perl are organized in a different way. Generally, they are separated in three groups

  • Scalar variables
  • Arrays
  • Hashes

Scalar variables

All scalar variables in Perl are identified with a starting “$”. They can hold any data type: integers, strings, floats, doubles and so on. The scalar variables have a singular (scalar) value in them. You can assign data with the equal sign (“=”) to them, just like in many other programming languages. Scalar variables can change their type in run-time. For example:

#!/usr/local/bin/perl

$variable = "5"; #defined as a string

print $variable + 3. "\n";

Here, in the print statement, the string value of $variable is converted to integer and 3 is added to it. Therefore,the result will be 8.

Arrays

Arrays are identified with the “@” prefix. As you might already know, arrays are used to hold multiple values in them. Just like in C and C++, Perl arrays use index numbers, starting from 0. Initializing an array:

@array = ("one", "two", "three");

And accessing specific array member:

print "The first member is: $array[0]\n";

Please note that every array member is a scalar variable.

Hashes

Hashes are an extremely good way to organize data in Perl. They are a very special kind of array. Every hash is recognized by the prefix '%', or the percent sign. Every hash stores the values in pairs, the key and the value which is accessed by the key. Let's look at the example:

%example_hash("animal" => "horse",
              "human" => "man",
              "plant" => "tree");

Here, the values animal, humand and plant are the hash keys, while horse,man and tree are the hash values. We can access every hash value through its key:

print "Printing out animal value...\n";
print $example_hash{'animal'} "\n"; #This will print "horse"
print "Printing out human value...\n";
print $example_hash{'human'} "\n"; #This will print "man"
print "Printing out plant value...\n";
print $example_hash{'plant'} "\n"; #This will print "tree"

Loops

Loops in Perl are very similar to the loops in every other programming language. We differ these kinds of loops in Perl:

  • for
  • foreach
  • while

for

The for loop is very similar to the one in C and C++. A simple usage would be:

for(my $counter = 0; $counter < 10; $counter++)
{
  print $counter;
  print "\n";
}

This will increment $counter up to 9 and print the results out.

foreach

Foreach loop is a very handy loop. This loop has a built-in iterator used to cycle through all array or hash members. It can greatly simplify your work with arrays or hashes. Here is an example code:

my $array_element;

foreach $array_element(@some_array)
{
  print $array_element;
  print "\n";
}

So, look carefully at this line: foreach $array_element(@some_array). Notice that we first declared a new variable - array_element. So, we store every member of the array @some_array in the variable $array_element. With every cycle the foreach loop turns,it stores the next element in the $array_element variable. This loop has default element, named $_.

foreach (@some_array)
{
  print $_;
  print "\n";
}

while

The while loop is also very similar to the C/C++ while. A simple example:

while (1<2)
{
  print "True\n";
}

This is a never-ending loop, because the condition will be always true, 1 will be always less then 2. True will be printed forever. However, Perl implements the usage of next and last.

The keyword next is used to jump the remaining code of the while and go back to the beggining of the while structure. An example is handy:

while ($counter < 10)
{
  $counter++;
  if ($counter == 5)
  {
   print "Using next here, so the last lines won't be printed out.\n";
   next;
  }

  print "This will not be printed, if $counter is 5\n";
}

last is used to break the while loop. For example, if we want to stop looping when $counter will reach 5:

while ($counter < 10)
{
  $counter++;
  if ($counter == 5)
  {
   print "Using last here, so the loop will be broken.\n";
   last;
  }

  print "This will not be printed, if $counter is 5\n";
}

Functions

As in many other languages, functions in Perl exist. They are defined with the sub keyword. For example, if we like to define a simple function print_name:

#!/usr/local/bin/perl

sub print_name
{
   print "This function prints the name Alexander.\n";
   print "Alexander.\n";
}

print_name; #The function is called here

First, we define the function (subroutine) and we call it later. Every function body must be surrounded with brackets - {}.

It is possible to define local variables, variables which can be used only in the function they are declared in. They are specified with the my keyword:

#!/usr/local/bin/perl

sub print_name
{
  my $name = "Alexander"; #This is local variable

  print "This function prints the name Alexander.\n";
  print $name "\n";
}

#The variable $name cannot be accessed here
print_name;

$name can only be used within the print_name function and nowhere else. If it does not exist in the global space, then it cannot be used there. The variables which are accessible in the global space, are called global variables. They can be used anywhere in the code, but their extended usage is not recommended too much.

Functions and parameters

Function parameters are passed as array members in Perl. Actually, all parameters passed to a function are transformed in members of the array @_. A simple example will clarify things out:

#!/usr/local/bin/perl
#Simple script


sub print_name
{
  #Now, there is only one parameter, the name
  #It is stored in the first place of the @_ array
  #and that is $_[0]

  my $name = $_[0]; # we store our parameter in the variable $name

  print "We will print out name now..\n";
  print $name;
  print "\n";
}

print_name("Alexander"); #We pass the parameter "Alexander" here
                         #The print_name function will print the name "Alexander"

As you might have already guessed, the second parameter will be stored in $_[1] and so on..

handbook/handbook/perl3.txt · Last modified: 2010/04/15 21:18 (external edit)