1. Scalar variables
Perl, as php, use the symbol "$" to set a variable:
example:
$s = 33.56;
$thing = 'books';
$phone = '450 345-0000';
The variables hold both strings and numbers
Perl is case sensitive, so $s and $S are different.
2. Interpolation
Use single quotes and double quotes as follows:
$x = 34.56;
$y = 'things\n';
print $x 'and' $y ; is incorrect
print $x .' and '. $y ; ouputs: 34.56 and things
print "$x .and. $y" ; ouputs: 34.56 and things
print '$x .and. $y' ; ouputs x .and. $y
print '$x and $y' ; ouputs x and $y
print "$x and \n $y" ; ouputs 34.56 and
things
The code \n is a newline and \t is a tab
3. ArraysAn array has the following expression:
@books = ("book1","book2","book3");
We use the "at" symbol for arrays and $ for
a scalar variable
We access an element of an array as follows:
$books[1]; wich gives book2.
To add elements in the array, we use push
function.
push(@books "book7", "book8"); or
push(@books, ("book7", "book8")); or
push(@books, @otherbooks); with
@otherbooks =("book7","book8");
push (@books); returns the length of the array @books;
The inverse of this fuction is the pop function.
It removes elements from an array.
@books = ("book1","book2","book3");
To remove the last element from the array @books, we use:
pop(books); that removes "book3".;
The index of the last element of the list @books is found by using the
following expression:
$#books
|