PERL

  

These are my personal notes that I use as a quick help in my work.
You are welcome to read them.

Contents of current page Top-level home page
 
Index  Java Internet Oracle Notes
Linux Basics Web Basics SQL Notes
Informatica Servlets Apache BkpRstore SQL*Plus
Teradata   LDAP Storage PL/SQL
Windows     Tables OEM
UML   Net8 Portal
SQL Server Python perl Performance OLAP
Vmware Visual Basic PHP/MySQL User Mgmt  
Git        
More technical pages here

Contents

 


Introduction

  


Basics

A basic script. Save in a file with extension .pl and give executable permissions (755)

#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<html><head>\n";
print "<body bgcolor=\"#FFFFFF\" >\n";
print "<h1>Hello, world!</h1>\n";
print "</body></html>\n";  

/usr/bin/perl should be the location of the perl on the server (try which perl).
There must be a blank line after the line with "content-type...", thus the double "/n/n".
Notice the backslash before the quotes inside the tags. Likewise, escape the following: \" \$ \@ \\

Debugger: perl -d script.pl
n for stepping over
s for stepping in (goes inside functions)
b $line_number or b $function_name: set breakpoint
b line boolean_expression: conditional breakpoint
d line: delete a breakpoint
c continue until a breakpoint
print $a_var: shows value x $a_var: display variable with hierarchy

 

#!/usr/bin/perl -wT
use strict;
use CGI;

my $the_query = CGI->new(); # instantiate a new object

print $the_query->header( "text/html" ), # use "header" of the CGI object (of which $the_query is an instance)
      $the_query->start_html(-title => "Sample CGI Script",
                             -bgcolor => "#ffffcc" ),
      $the_query->h1( "Sample HTML page" ),
      $the_query->p( "Produced by a CGI script" ),
      $the_query->end_html;

 

httpd.conf:

<Directory "${path}/www"><br />
    #Added ExecCGI<br />
    Options Indexes FollowSymLinks ExecCGI<br />
    AllowOverride None<br />
    Order allow,deny<br />
    Allow from all<br />
</Directory><br />
<br />
<IfModule dir_module><br />
    #added index.cgi<br />
    DirectoryIndex index.html .... index.cgi<br />
</IfModule><br />
<br />
<IfModule mime_module><br />
    TypesConfig conf/mime.types<br />
    AddType ....<br />
    #added these:<br />
    AddHandler cgi-script .cgi .pl<br />
    AddHandler server-parsed .html<br />
</IfModule>

Shbang and header

#!/usr/bin/perl -wT
#!C:/perl/bin/perl.exe -wT

The second line is for Windows
The -w switch is for turning on warnings
The -T switch is to enable the taint checking

use strict;

Instructs the compiler to stop on unsafe constructs
vars --> must pre-declare variables
subs --> impacts sub routine calling
refs --> no symbolic references

use CGI;

Uses Lincoln Stein's CGI.pm module.

print <<END_HERE;
<html><head><title>Sample CGI Script</title></head>
<body bgcolor="#ffffcc"> ... </body></html>
END_HERE

Alternative to print "...."

my $time = localtime;

Put time in variable

 

 


Variables and Operators

 

 

"Standard:"+ , - , * , /
As in C: use ++, --, +=, -=, ...
Power: **
String concatenation: . (a period)
Array concatenation: , (a comma)
Modulo: %
Sub-expression: ( ... )

Escape character is "\"
Use in: \\    \"    \$    \@   
\n New line
\r Carriage return
\t Tab
\xDD Character with hexadecimal DD

User input: <>
Example: print "Enter name:\n"; $name = <>; chomp($name); print "Hello, ", $name, "!\n";

Comment: #

Force declaration of variables: use strict;
Create local variable (or array or whatever): my $variable;
Create more than one: my ($a, @b);

Get warnings: use warnings;

Numerical comparison operators: ==  !=  <  >  <=  >=

Boolean operators: &&  ||  !  (second operand not evaluated if not necessary: false && / true ||)
0, "", and undef evaluate to false, all other values evaluate to true

An array: @array_name
An array element: $array_name[i], with i=0 to scalar(@array_name)-1 (to be confirmed)
Number of elements in the array: scalar(@myarray) or also $#myarray.
The last element: $myarray[-1]. Nth element from end: $myarray[-n]

Insert a variable's value in a string, with curly brackets to remove ambiguity: print "Show value: ${the_var_name}"

Conditional operators

Numeric testString testDescription
==eqEqual to
!=neNot equal to
>gtGreater than
>=geGreater than or equal
<ltLess than
<=leLess than or equal to
<=>cmpNot equal to, with signed return

 


Flow Control

 

if (cond)
{
    ...
}
elsif (cond)
{
    ...
}
else
{
    ...
}
If else.

if (0) {
}

is like putting comments
for $i (1..100)
{
    print $i, "\n";
}
For loop
for($a = 1, $b = 2 ; $a < 10 ; $a++, $b+=$a )
{
    ...
}
Another syntax of the for loop
Separate the initial statements and iteration commands with a comma
The interation commands are executed at the end of each iteration
A "next" executes the interation statements.
foreach $i (@an_array)
{
    ...
}
Foreach loop
while (cond)
{
    ...
    next;
    last;
}

While loop
next skips the rest of the loop
last exits the loop

A_LABEL: for $the_outer (1 .. 100)
{
    for $the_inner (1 .. 200)
    {if (...)
      {next A_LABEL;}
    }
}
Exit to the outer loop by labelling the outer loop
Personal opinion: labels are bad practice and lead to illegible code.
sub sub_name
{
    my @a_var;
    @a_var = @_;
}
Declare a sub routine.
The arguments are in @_
exit(n) Exit the script with exit code n. n=0 ==> success; n>0 ==> failure
sub sub_name
{
    my $a_var = shift;
    ${$a_var} = some calculation;
}

sub_name( \$a_variable, ...);
Pass a reference to a variable so that it can be modified inside the function.
\$a_variable gives the reference
${$a_var} dereferences it

 

 


Functions

 

length(string)
Length
substr(the_string, offset, length)
print substr("A string", 1, 2); --> " s"
int()
Returns an integer
chomp
Strip off the new-line character
lc()
lowercase
@components = split(/$regexp/, $string);
Split a string into elements whose boundaries are defined by the regular expression
@a_filtered_text = grep(/$regexp/, @some_lines);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Arrays

push @an_array, @another
push @an_array, $an_element
Adds an element(s) at the end of the array.
Equivalent to @an_array = (@an_array, ...)
$a_var = pop(@an_array)
Extract and return the last element
$a_var = shift(@an_array)
Extract and return the first element, with all elements shifting down
join("\n", @an_array)
Concatenates all elements into one string with new lines between each element
@reversed_array = reverse(@an_array);
First is last and last is first (to be confirmed)
(@an_array) x $number
$scalar x $number
Copies the array or the number many times
@ARGV
Array of the command line parameters. $ARGV[0] is the first element
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

hashes

Arrays indexed by strings

$one_hash{"One entry"} = "a scalar";
Assign one entry in the hash
$one_hash{$a_key}
Retrieves the value of a key
keys(%one_hash)
An array of the keys in the hash
exists($one_hash{$any_key}))
True if the key $any_key is in the hash
delete($one_hash{"a key"});
Remove the key from the hash
%hash1 = ("ct" => "Christopher", "my" => "Moussa", ...);
Alternate hash creation
 
 
 
 
 
 

input/output: globs

open  A_FILE_HANDLE, $mode, $the_relative_name;
open *afilehandle, $mode, $the_path;
Open a file handle, indicated by asterisk (verify this) or an intial capital. Generally put file handle in upper case. Default file handles are STDIN, STDOUT, STDERR, and ARGV. $mode values are:
Reading: < or nothing
Writing: >
Appending: >>
Read-write: +<
print FILE $something, ...
FILE is ommitted if it is STDOUT
close(THE_FILE)
Close
$line = <INPUT_FILE>
Read a line from the INPUT_FILE
join("", <THE_FILE>)
Return the rest of the file in one line
 
 
 
 
 
 
 
 

 

Regular Expressions

See section on regular expressions.

The expression $string =~ /$regexp/ returns a boolean
perform a substitution: $string =~ s/reg_expr/substitution/;

Perl excape sequences:
\w --> word character, equivalent to [A-Za-z_]
\W --> non-word character \s --> whitespace character (space and tab) \S --> non-whitespace character \d --> digit, equivalent to [0-9] \D --> non-digit. \b --> word boundary. Zero width sequence \B --> not a word boundary.

 


Perlisms

 

my $total = shift;
Get the first element in $_ (the function arguments) and shift the other arguments down.
Outside of functions, shift reads @ARGV
<>
Read a line from the standard input, equivalent to <ARGV>
while (<>) {print;}
<> is equivalent to <ARGV>
By default, an input symbol automatically assigns to $_, so the condition is equivalent to ($_ = <ARGV>)
The print outputs $_ by default
The equivalent is while ($_ = <ARGV>) {print $_;}
map
I haven't figured this one out yet
sort
This looks simple, but I will look at it later
<=> and cmp
Same thing (some of these things hurt my head)
[ @an_array ]
Dynamic reference to an array
{ %hash }
Dynamic reference to a hash
 
 
 
 
 
 
 
 
 
 
 
 

 


Forms

 

Encoding: for text

<form action="/cgi-bin/display.cgi" method="post" enctype="multipart/form-data">
...
</form>

Default encoding is enctype="application/x-www-form-urlencoded"

Sample script to read posted data:

#!... -wT
use strict;
my $the_buffer;
read (STDIN, $the_buffer, $ENV{'CONTENT_LENGTH'});
print "Content-type: text/plain\n\n";
print $the_buffer;

Using CGI.pm to handle request parsing

my $the_query = CGI->new();
my @the_name_value_pairs = $the_query->param;
print $the_query->header( "text/plain" );
foreach my $name ( @the_name_value_pairs ) {
    my @the_values = $the_query->param( $name ); # generally one value, but just in case treat as collection
    print $name . "=" . (join ", ", @the_values) . "\n";
}

 


Miscellaneous

 

Security

 

Sources

Ovid's CGI Course
A good simple course with many tips on security. Assumes basic knowledge of programming (not necessarily Perl).
This does not seem to exist anymore
http://www.shlomifish.org/lecture/Perl/Newbies/lecture1/
http://www.shlomifish.org/lecture/Perl/Newbies/lecture2/
A clear basic course
 
 
 
 
 
 

Local file: http://127.0.0.1/cgi-bin/printenv.pl