Dec 27, 2006
Basic #8, Hashes
Declaration 1:
%longday = ("sun" => "sunday",
"mon" => "monday",
"tue" => "tuesday",
"wed" => "wednesday",
"thu" => "thursday",
"fri" => "friday",
"sat" => "saturday",
);
arrays use @, while hashes use %
the left hand side are the keys, the right hand side are the values.
Another way of populating hashes:
%month = ("jan","january","feb","february","mar","march");
the first character of each pair of strings is the key, while the subsequent is the value.
We can also create a hash just by creating a pair value:
$dog{'jrt'} = "Jack Russel Terrier";
Accessing a single hash value:
print $dog{'jrt'};
Nov 23, 2006
Basic #7, Arrays
@animals = ('cats','dogs','rabbits');
Retrieval:
$animals[2] -> returns rabbits
counting starts from 0
Length:
$sum = @animals;
print $sum;
prints 3;
Last Index:
$#animals -> returns 2
Printing:
print @animals -> prints catsdogsrabbits
print "@animals" -> prints cats dogs rabbits
Adding:
push(@animals, "mouse"); -> adds "mouse" to the list
$marsupial = 'kangaroo';
push(@animals, $marsupial); -> adds "kangaroo" to the list
@predators = ("tigers","leopards");
push(@animals, @predators); -> adds "tigers" and "leopards" to the list
Remove last value:
$cute = pop(@animals); -> remove the last item from the list, and assigns it to $cute
Basic #7, Interpolation
$b = 'bananas';
$a.' and '.$b prints apples and bananas
'$a and $b' prints $a and $b
"$a and $b" prints apples and bananas
Basic #6, Operations and Assignments
+ add
- minus
* multiply
/ divide
** power
% remainder of
$a++ returns value of a, then increment it by 1
++$a increment value of a, then return it
$a-- returns value of a, then decrement it
--$a decrements value of a, then return it
String:
$a . $b concatenation
$a x $b a is repeated b times
Basic #5, Scalar variables
For perl, we do not need to declare what type(int, string) is the variable.
variables in perl are just prefixed with a $
e.g.
$a = 'apple';
$number = 67;
$number = '67';
$number = '00067';
all of the above are valid assignments
variable names can be combinations of numbers, characters and _.
$_ is special and reserved, cannot be used.
Scalar variables are case sensitive, i.e. $a is different from $A.
Basic #4, the strict pragma
A pragma is a special perl module that hints the compiler the way a block of statements should be compiled. If the program disobeys the restrictions placed on it, it won’t compile. Here is an example:
———————————————
#!C:/Perl/Bin/Perl
use strict “subs”;
$name = Ellie;
print “Hi $name”;
———————————————-
2nd Line
The 2nd line tells the compiler to use the module - strict, with subs as the argument.
The existence of this 2nd line will give the following outcome:
Bareword “Ellie” not allowed while “strict subs” in use at try_pragma.pl line 3.
Execution of try_pragma.pl aborted due to compilation errors.
The use function call allows users to use modules located in the standard Perl library, which is located at C:\Perl\lib in windows.
When the strict pragma takes “subs” as an argument, it will capture any bare words, or unquoted strings, when the script is being compiled. The program will abort with an error message.
Basic #3, Literals
Numeric Literals
12345 | Integer |
0×456ff | Hexadecimal |
0777 | Octal Numbers |
23.45 | Float |
.234E-2 | Scientific Notation |
String Literals
\t | Tab | ||||||||||
\n | New Line | ||||||||||
\r | Carriage Return | ||||||||||
\f | Form Feed | ||||||||||
\b | Backspace | ||||||||||
\a | Alarm/Bell | ||||||||||
\e | escape | ||||||||||
33 | Octal Character | ||||||||||
\xff | Hexadecimal Character | ||||||||||
\c[ | Control Character | ||||||||||
\l | Next character is converted to lower case | ||||||||||
\u | Next character is converted to upper case | ||||||||||
\L | Next characters are converted to lower case until a \E is found | ||||||||||
\U | Next characters are converted to upper case until a \E is found | ||||||||||
\Q | Backslash all following non-alphanumeric characters until a \E is found | ||||||||||
\E | Ends upper or lower case conversion started with \L or \U | ||||||||||
\\ | Backslash |
Special Literals
_LINE_ | Represents the current line number | |
_FILE_ | Represents the current filename | |
_END_ | Represents the logical end of script; trailing garbage is ignored |