PERL Programming



PERL

The way to laziness




PERL: a brief historic
PERL was designed to be an upscale tool for string manipulation, and was conceived as a substitute for at once sed and awk.

It inherited its data structures from LISP, in its GNU version gcl, its flow-control methods from C, and redirections capabilities from the Bourne shell (more precisely, from bash, let's remain in the brave GNU world).

PERL is a run-time compiled language, combining speed with the versatility of self-writing codes.


Data structures in PERL


Syntax
Variables dereferencings are typed. It is enough to affect a value to a variable to create it. PERL manages by itself memory allocation and garbage collecting.

$scal = "string" | 0 | \ *var | undef ;
@array = (1,2,3) ;
%hash = ('key1','val1','key2','val2') ;

Two syntax may be used in PERL: C-like, with parentheses, and Bash-like, with spaces.

*rvalue = *lvalue op func(*arg1, (res *arg2a, *arg2b) ) ;

#comment


Flow-control
The primary syntax for tests is much like C, but test can also be used in their infixed version.
if(clause1) { expr1 ; ... }expr1 if clause1 ;
elsif(clause2) { expr2 ; ... }expr3 unless clause2 ;
else { expr3 ; ... }
for my $id in (@list) { ... }
for(my $i=0;$i$lt;$n;$i++) { ... }
while(test) {
}
sub FuncName(prototype) {
}


Memory and processes
To execute an external program in PERL, one must use exec or system.
exec("command"): doesn't return unless on error.
system("command"): waits for external program termination, then continues the execution.
Just as in C!
Two command let you control the scope of variables.
my(*vars): declaration of variables local to encolsing block.
local(*vars): gives a local value to global variables, modifications are local to the enclosing block.
glob(*var): gives the original (unmodified) global value of a 'localized' varibale.


Input-output
Arguments to the PERL script are stored in @ARGV, and environment variables in %ENV.
Three filehandles are defined by default: STDIN, STDOUT and STDERR. Filehandles can be created, destroyed and read using:
open(FLHD,"file"): opens 'file' on FLHD (mode is controlled by leading chars: '>' means write, '<' read, '+' append, default is read).
close(FLHD): frees the resources associated with a filehandle.
<FLHD>: reads a line (text-file-wise).
read,write,lseek: used as in C.


Regexps and string manipulation
m//: matching, eg: $var =~ m/^{}#/so
s///: substitution, eg: $var =~ s/( C )|( bash )/ Perl /gso
split(//,$var,$nb): splits a string on every spot where the regexp matches, producing at most nb chunks (any remainig data is in the last chunk).
join($del,@liste): concatenes the elements from the list, separating them with the delimiter.
. : matches any character.
?,*,+ : repeating 0-1, 0-.., 1-.. times.
[...] : set of accepted characters at this spot.
^ ,$ : matches the begining and end of a line.