Dec 27, 2006

Basic #8, Hashes

Hashes are just like arrays, but instead of numbers, hashes use strings as the index.

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

Assignment:
@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

$a = 'apples';
$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

Numeric 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

A variable is essentially a placeholder for some data, which can be reused many times.
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

Basic #2, Switches to Run Perl

-e Switch

Executes perl statements at command prompt instead of from a script file.
e.g perl -e “print ‘hello dolly’”;


-c Switch

Checks the syntax of the perl script without actually executing the Perl commands. If the syntax is correct, Perl will tell you so.
e.g. perl -c hello.pl


The -w Switch

Warns user about possibility of using future reserved words.
e.g try_w_switch.pl

#!C:/Perl/Bin/Perl
print STDOUT ellie, “\tThe price is \$100.00\n”;

If i run it just using perl try_w_switch.pl, the output is :

ellie The price is $100.00

However, if i run it using perl -w try_w_switch.pl, i get the following output:

Unquoted string “ellie” may clash with future reserved word at try_w_switch.pl line 2.
ellie The price is $100.00

Btw, the print STDOUT …. is just another way to print text to the standard output, only with this type of command can i use the comma sign.

A “\t” indicates a tab character.

A “\n” indicates a new line character.

A “\” is neccessary before the “$” sign, else the output will be incorrect. This is because the “$”,”@” and “%” signs are special character prefix for variables.

Basic #1 Hello World

This is the first perl script to write.

--------------------------------------------------------------------------------------
#!C:/Perl/Bin/Perl
#prints the words “hello world” with a leading next-line character
print “Hello World\n”;
-------------------------------------------------------------------------------------

Line 1
The first line must always start with the “shebang” character -”#!”, followed by the location of where your perl interpreter resides.

I am using windows, but i suggest to use the “/” instead of the “\” as the folder separator because next time if you write a perl script on the apache server, all the “\” folder separator must be changed to “/” for it to work propely.

Line 2
The second line uses the print command to print the trailing text unto the standard output.

Note that except for the first line, all lines should end with a “;” to indicate the end of the line.

Line 3
The 3rd line is a comment, it will not be executed. A comment is indicated by a “#” at the beginning.


Save the file as hello.pl and run perl hello.pl in the command prompt.

hello world

That’s it!