Jan 4, 2007

Basic #13, Subroutines

subroutines in Perl is declared like this:

sub testSub{
print "Hello World\n";
}

A call to the subroutine is made with an & before the subroutine name, as in:

&testSub;
&testSub($_);
&testSub(5, $_);

Parameters passed to the subroutine can be referenced with @_, which have nothing to do with $_

e.g.

sub printArguments{
print "@_";
}

each individual parameter can also be referenced with $_[0], $_[1], etc
** again $_[0], $_[1] has nothing to do with $_

Jan 3, 2007

Intermediate #2, Regular Expressions Part 1

regular expressions are used with the =~

e.g

$string = "the cat is looking for food";
print "yes" if($string =~ m/cat/);

m// used for matching, return a boolean
s/// used for substituting

e.g.

$string = "this is a cat";
$string =~ s/cat/dog/;

replaces the cat to dog

* the separator / can be changed to any other characters e.g s#cat#dog#

-------------------------------------------------------------------------

Metacharacter Description

\ escape character

^ match beginning of string (or line if /m modifier)

$ match end of string (or line if /m modifier)

. match any character except \n

| specify alternate matches in []

() groups expression together, each group become $1, $2, $3, etc

[] looks for a set of characters

---------------------------------------------------------------------------------

Sequence Purpose

\w alphanumeric characters including _
\W non-alphanumeric
\s white space character
\S non white space character
\d digit
\D non-digit
\b word boundary
\B non word boundary
\A Matches only the beginning of a string
\Z Matches only at end of string
\G matches where previous m//g operation left off
\t tab
\n newline
\r carriage return
\f form feed
\a alarm (bell)
\e escape
\b backspace
\033 octal character
\x1B hex character
\c[ control character
\l makes next character lowercase
\u makes next character uppercase
\L specify lowercase until \E
\U specify uppercase until \E
\E Ends case modification
\Q Quotes (disables) regexp metacharacters till \E

----------------------------------------------------------------------------

Maximal Minimal Purpose

* *? Matches 0 or more items
+ +? matches 1 or more items
? ?? matches 0 or 1 item
{n} {n}? matches exactly n items
{n,} {n,}? matches at least n items
{n,m} {n,m}? matches at least n, but not more than m items

Maximal : matches maximum no of times
Minimal : matches min no of times

----------------------------------------------------------------------------------

Modifier Description

g matches all occurrence within a string, not just the first
i case insensitive
m for multi-line strings, ^ and $ match end of string instead of individual lines
o eval expression only once
s allow us of . to match newline character
x allows using of whitespace in expression for clarity
e eval replacement string as an expression(substitution only)

*modifiers are put at the end of m// or s/// as e.g. m//g


-------------------------------------------------------------------------------------

Intermediate #1, File Handling

#!c:/www/perl/bin/perl

$file = "D:/My Perls/data.txt";
open(INFILE, $file);
@lines = <INFILE>;
close(INFILE);

foreach(@lines){
print $_;
}


INFILE is the file handler, no quotes is to be added. It can be replaced with any arbitual names.

<INFILE>reads in the entire file in one go, if a scalar variable is used instead of the array, then only the next line would be read in.

-----------------------------------------------------------------
open(INFO, $file); # Open for input
open(INFO, ">$file"); # Open for output
open(INFO, ">>$file"); # Open for appending
open(INFO, "<$file"); # Also open for input

*can add the - or die("error :".$!) for error handling
-----------------------------------------------------------------

if the file is already opened for output,
we can print to the file with the print statement with an additional parameter :

print INFO "END OF FILE\n";

-----------------------------------------------------------------

Standard Input and Output:
open(INFO, '-'); # Open standard input
open(INFO, '>-'); # Open standard output

Basic #12, loop controls

next - skips remainder of the code block, forcing the loop to proceed to the next value in the loop.

last - end the loop entirely, skipping the continue block also

redo - reexecute the code block wthout reevaluating the conditional statement for the loop. It skips the remainder of the code block and also the continue block.

Basic #11, for loops

LABEL for (EXPR; EXPR; EXPR) BLOCK

e.g.

for($i=0;$i<100;$i++){
.....
}

for($i=0, $j=0; $i<100;$i++,$j++){
....
}

LABEL foreach VAR (LIST) BLOCK
LABEL foreach VAR (LIST) BLOCK continue BLOCK

e.g.

for (@months){
print "$_\n";
}

foreach $key (keys %monthstonum){
print "Month $monthstonum{$key} is $key\n";
}

Basic #10, while statements

while (EXPR)
while (EXPR) BLOCK
while (EXPR) BLOCK continue BLOCK

the block after the optional continue statement will be executed after each iteration

do BLOCK while (EXPR)
do BLOCK until (EXPR)


Basic #9, If statements

Various ways of executing the if statement

if(expr)
if(expr) {block}
if(expr) {block} else {block}
if(expr) {block} elsif (expr) {block} ...
if(expr) {block} elsif (expr) {block} ... else {block}

(expression) ? (statement if true) : (statement if false)

unless (expr)