Perl Programming

Contents:

Perl Programming. 1

Writing Safe Code. 3

Declare your variables with my. 3

Enforcing the usage of my by the strict module. 3

Using the warnings module or the -w switch. 3

PERL-CGI 3

Guidelines for HTML page generation by a CGI-program.. 4

Sending the contents of a file to the user 5

Counters. 5

Mailing List 6

A complete example. 7

Checkboxes. 9

Vocabulary. 10

Perl-DBI 12

 

VARIOUS

# Notes from Weizmann Institute, Israel http://bioinformatics.weizmann.ac.il/courses/prog/

# February, 2002

# This is not meant to be an exhaustive Perl course, there are many proper ones out there. These are the notes about what I considered interesting…

            \n         newline

            \t          tab

            \a         bell

 

** exponentiation

(e.g. 2**3 is 2 to the power of 3, resulting 8)

% modulus

(e.g. the value of 10%3 is the remainder when 10 is divided by 3, which is 1)

 

length

-------

  $result = length ("university"); #$result gets 10

 

ARRAYS

===========

foreach $i ( {at} some_array) {

   statement_1;

   statement_2;

   statement_3;

}

 

An empty list:

 

{at} list = ();

 

sort

---------

  {at} array2 =  sort   ( {at} array1);

  _______   ______  ___________

 

# return   function  argument

# value

 

push

-----

$a = 5;

$b = 7;

{at} array = ("David", "John", "Gadi");

push ( {at} array, $a, $b);

 

# {at} array is now ("David", "John", "Gadi", 5, 7)

 

shift

-----

{at} array = ("David", "John", "Gadi");

$k = shift ( {at} array);

 

# {at} array is now ("John", "Gadi");

# $k is now "David"

 

$numberofelements = {at} array;

$numberofelements = scalar ( {at} array);

Use the special variable $#array_name to get the index value of the last element of {at} array_name.

Example:

{at} fruits = ("apple", "orange", "banana", "melon");

$a = $#fruits;      # $a is now 3;

 

Functions

sub subroutine_name {
   my ( {at} list) =  {at} _;    
   my ($no_of_elem, $result);
   my ($sum) = 0;      
   statement_1;
   statement_2;
   statement_3;
   ...
}

 

$var2= subroutine_name ($var1);

 

·        use local my variables inside subroutine!

·        Upon subroutine invocation, a special array variable is formed called {at} _, and it lasts throughout the duration of the subroutine.

·        The return function is used inside subroutines to specify the value that will be returned by the subroutine to the outside program (or to the place in the program from which the subroutine was invoked).

·        any type of variable can be passed onto subroutine, and will be extrapolated

 

When the arguments are to be captured in a combination of array and scalar variables, special care has to be taken. In this case, only one array is allowed, and it has to be the last variable in the list to which {at} _ is assigned.

sub good {
   my ($first, $second,  {at} group) =  {at} _; # capturing arguments
   print "first:  $first\n",
         "second: $second\n",
         "group:   {at} group\n";
}

Some functions need not receive any arguments, and are invoked as:

function_name();

Writing Safe Code

Declare your variables with my

Local Variables need to be declared, but not initialised.

Note: If a list of variables is declared with my, the list must be enclosed in parentheses. If only one variable is declared with my, do not use parentheses (this might cause problems in statements like my ($line) = <FILEHANDLE>;, which we will learn later).

e.g.

 
   my ( {at} list) =  {at} _;
   my ($no_of_elem, $result);
   my $sum;

Enforcing the usage of my by the strict module

The strict module imposes several restrictions on your Perl program. It will send you compile-time error messages and will avoid program execution, in cases where your program does not obey the restrictions. You may choose to use strict for creating safer code and to help you debug your programs.

use strict 'vars';

Using the warnings module or the -w switch

The warnings module may help you debug your program and write safer code, by sending warnings during program compilation.
However, it will not stop the program execution.

#!/usr/local/bin/perl –w
or
use warnings;

Looks more useful than strict.

Last but not least about my:

my creates a local variable. local doesn't.

Because local does not actually create local variables, it is not very much use.

PERL-CGI

Static HTML page

Stored as an HTML file on the Web server.

Dynamic HTML page

Created "on the fly" by a computer program at the server side.

CGI (Common Gateway Interface)

The mechanism that connects between computer programs and the Web server.

It also defines a set of environment variables. Commonly, the program will generate some HTML, which will be passed back to the browser but it can also request URL redirection.

CGI allows the returned HTML (or other document type) to depend in any arbitrary way on the request. The CGI program can, for example, access information in a database and format the results as HTML. A CGI program can be any program, which can accept command line arguments. Some HTTP servers require CGI programs to reside in a special directory, often "/cgi-bin" but better servers provide ways to distinguish CGI programs so they can be kept in the same directories as the HTML files to which they are related. Whenever the server receives a CGI execution request it creates a new process to run the external program. If the process fails to terminate for some reason, or if requests are received faster than the server can respond to them, the server may become swamped with processes.

 

Guidelines for HTML page generation by a CGI-program

  1. In order for your program to send its standard output to the Web it should be placed in a special directory on the Web server, usually the cgi-bin directory.

You can sometimes get the same effect if you append the name of your program with .cgi and place it in your public_html directory.

Consult your system administrator or Internet provider for that.

  1. If your program output is a text file (e.g. in HTML format), you must include in your program the following print command before any other print commands:
print "content-type: text/html\n\n";
  1. Remember to use <BR> or <P> tags to mark end of text lines.
    \n "newlines" will not be visible on Web pages.
  2. In quoted strings, remember to place a backslash before quotes. e.g.
5.             print "<A HREF=\"home.html\">Back to Home Page</A>";
  1. Do not forget to make the file containing your program (e.g. hello.cgi) executable by "the world" using the Unix command:
chmod a+x hello.cgi
  1. To execute your program from a Web browser, use the following URL (assuming that your program is in your public_html directory):
http://host_computer_name/~userid/hello.cgi

Where userid is your own userid.

From: http://www.cc.ukans.edu/~acs/docs/other/forms-intro.shtml

 

An HTTP URL may identify a file that contains a program or script rather than an HTML document. That program may be executed when a user activates the link containing the URL. Such programs are sometimes called HTTP scripts or "Common Gateway Interface" (CGI) scripts. On some HTTP servers these CGI programs are stored in a directory called cgi-bin, and so they are also sometimes called "cgi-bin scripts."

 

from http://www.cc.ukans.edu/~acs/docs/other/cgi-with-perl.shtml and http://www.yahoo.com/Computers/World_Wide_Web/Programming/Perl_Scripts/

also see: http://www.speakeasy.org/~cgires/cgi-tour.html

 

Perl may be used to return an HTML document by using a program like

 

#!/usr/local/bin/perl

print "Content-type:text/html\n\n";

print <<WEB_PAGE;

    <html>

       

    </html>

WEB_PAGE

 

Note also that any script that returns an HTML document to the Web browser must print the string Content-type:text/html\n\n to standard out.

If this script is stored in a file called script.pl within the cgi-bin directory within the username home directory on mrc-lmb.cam.ac.uk, the URL for the script would look like this:

http://www.mrc-lmb.cam.ac.uk/scripts.pl

 

IMPORTANT: Your script files should probably give access permissions only to the owner. You can assign these permissions by entering

chmod 700 script_filename

 

Perl allows you to call a script from within a Perl script. In general, you can run a script and send the output of that script to the user with a Perl print command like

print `script_filename`;

where script_filename is the name of the file that holds the script to be executed by the Perl script. This facility can be used to run file-resident shell commands as well as other Perl or shell scripts. For example, you can use this approach to include the current date in a document:

 

#!/usr/local/bin/perl

print "Content-type: text/html\n\n";

print "The current date is ";

print `/bin/date`;

 

Sending the contents of a file to the user

For example, suppose you have a signature file named

/home/username/public_html/signature.file

 

that contains a signature like

    <p>

    See <a href="http://falcon.cc.ukans.edu/~username/">

    my homepage</a> for more information.

 

and you want to use this signature with a page you send to the user from a script. You could do that with a sequence of print commands:

#!/usr/local/bin/perl

print "Content-type:text/html\n\n";

print <<WEB_PAGE;

    <html>

    <title>My Thank You Page</title>

    <h1>Thank you for reading this document.</h1>

WEB_PAGE

print `cat /home/username/public_html/signature.file`;

print "</html>\n";

 

Alternatively, you might first use the UNIX cat command to copy the signature file into a Perl variable and then print that variable within a Perl here document:

#!/usr/local/bin/perl

print "Content-type:text/html\n\n";

$SIG = `cat /home/username/public_html/signature.file`;

print <<WEB_PAGE;

    <html>

    <title>My Thank You Page</title>

    <h1>Thank you for reading this document.</h1>

$SIG

    </html>

WEB_PAGE

Counters

#!/usr/local/bin/perl

print "Content-type:text/html\n\n";

#

#First, increment the counter:

#

open(COUNTER, "+< /home/smith/counter.file");

             # open the counter file with read and write access.

$COUNT= <COUNTER>;           #read the current value.

$COUNT++;                    #increment it by one.

seek(COUNTER, 0 , 0);        #rewind the file.

print COUNTER $COUNT;        #write the new value to the file.

close COUNTER;

#

#Then use the counter in the returned HMTL.

#

print <<END_OF_PAGE;

<html>

<title>My first return page</title>

<h1>Thank you for selecting this document.</h1>

You are visitor number $COUNT

</html>

END_OF_PAGE

 

Perl does include facilities for synchronizing file accesses. Among others, the flock function allows users to lock a file for private use. You could use the line

flock (COUNTER, 2);

to lock the file immediately after opening the file and the line

flock (COUNTER, 8);

to unlock the file immediately before closing it.

Mailing List

Suppose you want to build a form that collects e-mail address from users interested in vegetarianism. The following is an example.

 

<form method="post" action="http://www.cc.ukans.edu/cgiwrap/grobe/send-veggi-info.pl">

<P>

If you would like more information about vegetarianism,

please enter your name and e-mail address below.<P>

Please enter your name:<br>

<input type="text" name="name" size="35"><br>

Please enter your e-mail address:<br>

<input type="text" name="address" size="35"><p>

<input type="submit" value="send address">

<input type="reset" value="start over">

</form>

 

To actually use the data, you must use the HTML form field names in a special way within the Perl script. For example, within the following script, $q->param('name') refers to the contents of the "name" field in the example form above. The result page returned to the user will contain the actual name entered when the fieldname "name" is used in this fashion:

thank.you.pl:

#!/usr/local/bin/perl

use CGI;

$q = new CGI;

$name = $q->param('name');

$address = $q->param('address');

print "Content-type:text/html\n\n";

print <<END_OF_MESSAGE;

<html>

<title>Thank You Page</title>

<h1>Thank you for filling out my form!</h1>

Thank you, $name, for filling out my form!

I will mail information to $address right away.

</html>

END_OF_MESSAGE

 

Mailing information from a script

sendmail.pl:

use CGI;

$q = new CGI;

$address = $q->param('address');

$name = $q->param('name');

open (MAIL, "| /usr/lib/sendmail -oi -n -t" );

print MAIL <<MESSAGE_TO_USER;

To:$address

From:cvogel\ {at} mrc-lmb.cam.ac.uk

Dear $name:

Here are some books that talk about vegetarianism:

     Diet for a New America

     How to Avoid Beef

MESSAGE_TO_USER

close MAIL;

 

To record data in a file, you can use a Perl fragment like the following:

open (DATAFILE, ">> /home/smith/filename.txt");

flock (DATAFILE, 2);

print DATAFILE <<LIST_ITEM;

 blabla

LIST_ITEM

flock (DATAFILE, 8);

close DATAFILE;

 

The information written may contain HTML mixed with strings instructing Perl to insert information collected on a form. For example, a string like $name appearing in the HTML indicates the name variable set previously in your program should be included in the HTML written to the file. This approach might be used in a typical guestbook application as

guestbook.pl:

#!/usr/local/bin/perl

use CGI;

$q = new CGI;

$name = $q->param('name');

$address = $q->param('address');

open (DATAFILE, ">> /home/smith/public_html/guestbook.html");

flock (DATAFILE, 2);

print DATAFILE <<RECORD_ITEM;

Name: $name

Address: $address

RECORD_ITEM

flock (DATAFILE, 8);

close DATAFILE;

A complete example

The script collects the name and address information submitted by the user, records that information in a data file, returns a summary of the record to the user who entered the information, and sends a mail message to the form creator to let her know more data has arrived. This script uses the CGI module to collect the form information submitted by the user and relayed to the script by the Web server.

This example would be referenced with the URL

http://raven.cc.ukans.edu/cgiwrap/grobe/send-veggi-info.pl

if it were to be run from the public_html/cgi-bin directory within the home directory for the account grobe.

 

#!/usr/local/bin/perl

#

# Written by Michael Grobe...5-8-96.

# Updated by Jeff Long...9-15-2000.

#

# Send the html document MIME type.

#

print "Content-type: text/html\n\n";

# parse the input information and retrieve arguments from the form.

#

use CGI;

$q = new CGI;  # get the form arguments.

$name = $q->param('name');

$address = $q->param('address');

#

# respond with an html file to the user.

#

print <<WEB_PAGE;

<html>

<h1>Vegetarians unite!</h1>

A list of books about vegetarianism will be sent to $name

at $address.

</html>

WEB_PAGE

#

# Mail the information to the user using the Unix sendmail command.

#  sendmail -n ignores alias file.

#  sendmail -t examines stdin for To: list of addressees

#  sendmail -oi does not stop with a line containing only a period

#

open (MAIL, "| /usr/lib/sendmail -oi -n -t" );

print MAIL <<MESSAGE_TO_USER;

To:$address

From:grobe\ {at} raven.cc.ukans.edu

Dear $name:

Here are some books that talk about vegetarianism:

     Diet for a New America

     How to Avoid Beef

MESSAGE_TO_USER

close MAIL;

#

# Record the request in a datafile in comma delimited format.

#

open (DATAFILE, ">> /homea/grobe/list-of-recipients.txt");

flock (DATAFILE, 2);

print DATAFILE <<LIST_ITEM;

\"$name\",\"$address\"

LIST_ITEM

flock (DATAFILE, 8);

close DATAFILE;

#

# send a message to the script owner that info has been sent to the user.

#

#  sendmail -n ignores alias file.

#  sendmail -t examines stdin for To: list of addressees

#  sendmail -oi does not stop with a line containing only a period

#

open (MAIL, "| /usr/lib/sendmail -oi -n -t" );

print MAIL <<MAIL_MESSAGE;

To:grobe\ {at} raven.cc.ukans.edu

From:grobe\ {at} raven.cc.ukans.edu

You have just sent info about vegetarianism to $name at