quick.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

   

space.gif

   

space.gif

  ../images/main/bullet_green_ball.gif Perl basics

This section is about simple things in perl.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Comments

Perl has only one type of comments - one line comments. Everything what starts with # and up to the end of the line is considered as comment.

   

space.gif

print "Hello!"; # print out one word

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Variables

To use variables in Perl all you need to do is to initialize them. The names of scalar variables are to start with $ sign:

   

space.gif

$counter = 0;

$amount = 15.20;

$name = "Daniel";

print "amount = $amount\n";

   

space.gif

As you can see from the example above we don't describe variables we just use them. You can also notice that we do not need to specify a type of a variable. Perl automatically chooses the right type based on the value assigned to a variable. Thus, one variable can store different types of data:

   

space.gif

$a = 100;

print "\$a = $a is a number\n";

$a = 'is a text now';

print "\$a $a\n";

   

space.gif

Scalar variables in Perl may hold the following types of values:

  • Integers (decimal, octal, or hexadecimal)
  • Floating point numbers
  • Strings

Please notice that Perl does not give any error message if we are trying to use an unitialized variable. For example, the following code

   

space.gif

print "\$a = $a\n";

   

space.gif

will print

$a =

If you want to get such messages we would suggest to use option -w for the perl interpreter and package strict in the scripts. The following code

   

space.gif

# file: warning.pl

use strict;

print "\$a = $a\n";

   

space.gif

if executed by

...> perl -w warning.pl

will produce the output:

   

space.gif

Name "main::a" used only once: possible typo at comments.pl line 3.

Use of uninitialized value in concatenation (.) or string at comments.pl line 3.

$a =

   

space.gif

To correct this error we need to declare the variable $a by using the my function:

   

space.gif

# file: nowarning.pl

use strict;

my $a = 12;

print "\$a = $a\n";

   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Strings

String literals can be created with either single quotes or double quotes. To illustrate difference between them let's consider the following code:

   

space.gif

use strict;

my $name = "Bob";

print "Hello, $name\n";

print 'Hello, $name\n';

   

space.gif

This code generates the output:

   

space.gif

Hello, Bob

Hello, $name\n

   

space.gif

As you can see double quotes strings substitute variable names with their values and also treat backslashes as an escape character, which allows to print special characters like new line character (\n) or a dollar sign (\$). Single quoted strings don't do that.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Array variables

Arrays in Perl are ordered collection of scalars. Perl allows these scalars to have different types. Array names begin with the @ character:

   

space.gif

# an array of data

my @collection = ("My name", 123, 'string', 3.97);

   

space.gif

Perl also allows us to print all elements of the array at once. The following example shows how to do that, please also pay attention to the difference between double and single quotes and also array printed outside of any quotes:

   

space.gif

use strict;

my @collection = ("My name", 123, 'string', 3.97);

print "Within double quotes: @collection\n";

print "Outside any quotes: ", @collection, "\n";

print 'Within single quotes: @collection', "\n";

   

space.gif

Within double quotes: My name 123 string 3.97

Outside any quotes: My name123string3.97

Within single quotes: @collection

   

space.gif

As in C++, Perl arrays start at element 0. For @collection, the elements are:

   

space.gif

  • 0: "My name"
  • 1: 123
  • 2: 'string'
  • 3: 3.97
   

space.gif

  ../images/main/bulllet_4dots_orange.gif Name Conventions
   

space.gif

  ../images/main/bullet_green_ball.gif Literals and Operators
   

space.gif

  ../images/main/bulllet_4dots_orange.gif Example
   

space.gif

   

space.gif

   

space.gif

   

space.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

  

Copyright © 1998-2014

Deepak Kumar Tala - All rights reserved

Do you have any Comment? mail me at:deepak@asic-world.com