|
|
|
|
|
|
|
|
|
|
|
Perl basics
|
|
|
This section is about simple things in perl. |
|
|
|
|
|
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. |
|
|
|
|
|
print "Hello!"; # print out one word |
|
|
|
|
|
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: |
|
|
|
|
|
$counter = 0; |
|
|
$amount = 15.20; |
|
|
$name = "Daniel"; |
|
|
print "amount = $amount\n"; |
|
|
|
|
|
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: |
|
|
|
|
|
$a = 100; |
|
|
print "\$a = $a is a number\n"; |
|
|
$a = 'is a text now'; |
|
|
print "\$a $a\n"; |
|
|
|
|
|
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 |
|
|
|
|
|
print "\$a = $a\n"; |
|
|
|
|
|
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 |
|
|
|
|
|
# file: warning.pl |
|
|
use strict; |
|
|
print "\$a = $a\n"; |
|
|
|
|
|
if executed by |
|
|
...> perl -w warning.pl |
|
|
will produce the output: |
|
|
|
|
|
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 = |
|
|
|
|
|
To correct this error we need to declare the variable $a by using the my function: |
|
|
|
|
|
# file: nowarning.pl |
|
|
use strict; |
|
|
my $a = 12; |
|
|
print "\$a = $a\n"; |
|
|
|
|
|
|
|
|
|
|
|
Strings
|
|
|
String literals can be created with either single quotes or double quotes. To illustrate difference between them let's consider the following code: |
|
|
|
|
|
use strict; |
|
|
my $name = "Bob"; |
|
|
print "Hello, $name\n"; |
|
|
print 'Hello, $name\n'; |
|
|
|
|
|
This code generates the output: |
|
|
|
|
|
Hello, Bob |
|
|
Hello, $name\n |
|
|
|
|
|
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. |
|
|
|
|
|
Array variables
|
|
|
Arrays in Perl are ordered collection of scalars. Perl allows these scalars to have different types. Array names begin with the @ character: |
|
|
|
|
|
# an array of data |
|
|
my @collection = ("My name", 123, 'string', 3.97); |
|
|
|
|
|
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: |
|
|
|
|
|
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"; |
|
|
|
|
|
Within double quotes: My name 123 string 3.97 |
|
|
Outside any quotes: My name123string3.97 |
|
|
Within single quotes: @collection |
|
|
|
|
|
As in C++, Perl arrays start at element 0. For @collection, the elements are: |
|
|
|
|
|
- 0: "My name"
- 1: 123
- 2: 'string'
- 3: 3.97
|
|
|
|
|
|
Name Conventions
|
|
|
|
|
|
Literals and Operators
|
|
|
|
|
|
Example
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|