Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview...

23
Contents Introduction A simple Program in PERL PERL Syntax PERL File Extension Variable types First CGI Program Complete CGI program using PERL Applications Scope of Research

Transcript of Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview...

Page 1: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

Contents

• Introduction

• A simple Program in PERL

• PERL Syntax

• PERL File Extension

• Variable types

• First CGI Program

• Complete CGI program using PERL

• Applications

• Scope of Research

Page 2: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

Introduction

Since we have covered In this lecture we will be studying

basics of PERL programming and how to write a

complete CGI program using PERL

Page 3: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

A simple “Hello World” Program in PERL

#!/usr/local/bin/perl

print "Hello World";

The #!/usr/local/bin/perl line. Put this in the top of the file.

Line No 1. What does it do? This tells the system where the

perl interpreter is located. Different systems may have perl at

different locations. Sometimes it will be #!/usr/bin/perl.

Now if you are using perl in Windows, the script will run

without this line..

Page 4: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

PERL Syntax Overview

• There is no need to have a main() function or anything of that kind.

• Perl statements end in a semi-colon:

print "Hello, world";

• Comments start with a hash symbol and run to the end of the line:

# This is a comment

• Whitespace is irrelevant:

print "Hello, world";

• ... except inside quoted strings:

# this would print with a line break in the middle

print "Hello world";

Page 5: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

• Double quotes or single quotes may be used around literal strings:

print "Hello, world";

print 'Hello, world';

• However, only double quotes "interpolate" variables and special characters such as newlines (\n ):

print "Hello, $name\n"; # works fine

print 'Hello, $name\n'; # prints $name\n literally

• Numbers don't need quotes around them:

print 42;

• You can use parentheses for functions' arguments or omit them according to your personal taste. Following two statements produce same result.

print("Hello, world\n");

print "Hello, world\n";

Page 6: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

PERL File Extension

Regardless of the program you choose to use, a PERL

file must be saved with a .pl (.PL) file extension in order

to be recognized as a functioning PERL script.

So, Type this above in a text editor like notepad and

save this to a file called 'hello.pl'. If you are using

Linux/Unix, run the command 'perl hello.pl'. If you are

using Windows and you have installed ActivePerl, just

double click the file and the script will be executed by

the interpreter.

Page 7: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

variable types

Perl has three built in variable types:

Scalar

Array

Hash

Page 8: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

Scalar variable type

A scalar represents a single value as follows:

my $animal = "camel"; my $answer = 42;

Scalar values can be used in various ways:

print $animal;

print "The animal is $animal\n";

print "The square of $answer is ", $answer * $answer,

"\n";

Page 9: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

Array variable type

• An array represents a list of values:

my @animals = ("camel", "llama", "owl");

my @numbers = (23, 42, 69);

my @mixed = ("camel", 42, 1.23);

• Arrays are zero-indexed but you can change this setting by changing default variable $[ or $ARRAY_BASE. Here's how you get at elements in an array:

print $animals[0]; # prints "camel“

print $animals[1]; # prints "llama"

Page 10: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

Hash variable type

• A hash represents a set of key/value pairs. They are prefixed by % sign as follows:

my %fruit_color = ("apple", "red", "banana", "yellow");

• You can use whitespace and the => operator to lay them out more nicely:

my %fruit_color = ( apple => "red", banana => "yellow",);

• To get a hash elements:

$fruit_color{"apple"}; # gives "red“

• You can get at lists of keys and values with keys() and values() built-in functions.

my @fruits = keys %fruit_colors;

my @colors = values %fruit_colors;

Page 11: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

First CGI Program

#!/usr/bin/perl

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

print '<html>';

print '<head>';

print '<title>Hello Word - First CGI Program</title>';

print '</head>';

print '<body>';

print '<h2>Hello Word! This is my first CGI program</h2>';

print '</body>';

print '</html>';

If you click hello.cgi then this produces following output:

Hello Word! This is my first CGI program

Page 12: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

Lets make a little form. Save the following into a file called

guestbook.htm

<html>

<head><title>Guestbook</title></head>

<body>

<form action="/cgi-bin/guestbook.pl" method="get">

<table>

<tr><td>Name</td>

<td><input name="name" type="text" value=""></td></tr>

<tr><td>E-Mail</td>

<td><input name="email" type="text" value=""></td></tr>

<tr><td>Location</td><td>

<input name="loc" type="text" value=""></td></tr>

<tr><td>Comments</td>

<td><TEXTAREA name="comments" rows="10" cols="32“></TEXTAREA></td></tr>

</table>

<br><br><input type="submit" value="Add Entry"></form>

</body></html>

Page 13: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

result

Page 14: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

Now lets create a perl script to get the input from this file.

Create a file called 'guestbook.pl' in the cgi-bin folder

#!/usr/local/bin/perl

#Now lets get the input

my $query_string = "";

#Get the input

if ($ENV{REQUEST_METHOD} eq 'POST') {read(STDIN, $query_string, $ENV{CONTENT_LENGTH});

}

else {$query_string = $ENV{QUERY_STRING};

}

##### We will remove this

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

print "Query String is \n<br> $query_string";

Page 15: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

• Now put the perl file(save the above lines to a file called guestbook.pl) in the cgi-bin folder. Now copy the guestbook.htm file to the documents folder - the root folder. Now fire up your server and open the guestbook.htm file from within the server

• you will see the html file we created here. Now enter the below given values

Name - Binny E-Mail - [email protected] Location - Omnipresent Comments - Hello World! Here I am. and press the "Add Entry" button.

Page 16: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

• Now you will be taken to another page. If there is some mistake, it will be an error page. If not, the address bar will look something like this...

http://127.0.0.1/cgi-bin/guestbook.pl?name=Binny&[email protected]&loc=Omnipresent&comments=Hello+World%21+Here+I+am.

• This is the information that was passed between two files. Now the content part will look like this.

• Query String is name=Binny&[email protected]&loc=Omnipresent&comments=Hello+World%21+Here+I+am.

• This is the data we got from the form.

Page 17: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

The full script will look something like this...

#!/usr/local/bin/perl

my $query_string = '';

#Get the input

if ($ENV{REQUEST_METHOD} eq 'POST')

{read(STDIN, $query_string, $ENV{CONTENT_LENGTH});

}

else

{$query_string = $ENV{QUERY_STRING};}

# Split the name-value pairs

@pairs = split(/&/, $query_string);

foreach $pair (@pairs)

{ ($name, $value) = split(/=/, $pair);

# Making the input English. And removing unwanted things

$value =~ tr/+/ /;

$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$FORM{$name} = $value;}

Page 18: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

#Give output

print <<START;

Content-Type: text/html\n\n

<html>

<head><title>Guest book Result</title>

<body>

<h1 align="center">Guest book Results</h1>

Dear $FORM{'name'},<BR>Thank You for filling out our Guest Book. I appreciate this effort in your part.<br><br>

<table>

<tr><td>Name</td>

<td>$FORM{'name'}</td></tr>

<tr><td>E Mail</td>

<td><a href="mailto:$FORM{'email'}">$FORM{'email'}</a></td></tr>

<tr><td>Location</td>

<td>$FORM{'loc'}</td></tr>

<tr><td>Comments</td><td>

$FORM{'comments'}</td></tr>

</table> </body>

</html>

Page 19: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

START

# Open Guest Book

Fileopen (FILE, ">>guests.txt") || die "Can't open

guests.txt: $!\n";

#Write the information to the file

print FILE "$FORM{'name'} came from

$FORM{'loc'}.";

print FILE "E-mail address is $FORM{'email'}.";

print FILE "Comments : $FORM{'comments'}\n";

close(FILE);

Page 20: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

Output

Page 21: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

Completed Websites based on

CGI::Application

• These are examples of completed websites based on CGI::Application.

Source code is usually not available.

• proyectoweb2.com

– Centro de capacitacion use CMS - CGI::Application

• Unlibrary.com

– book sharing and cataloging site, uses CGI::Application, My SQL, Apache on Linux

• Dis-Order (metal music webshop)

– uses CGI::App, Postgre SQL, Apache (mod_perl) on Free BSD

• Adopt-A-Pet.com, by Summersault. Also uses mod_perl.

• Page-A-Day® Calendars Online

– CGI::Application, My SQL & Apache on Linux

• Workman.com

– Ajax, site search, shopping cart and Gift-O-Matic are all CGI::Apps

• iMeleon photography

– uses CGI::App, My SQL, Apache on Linux

• And Many more..

Page 22: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

Scope of Research

Popularity Contest for CPAN Modules

Offline Command Line Search for CPAN

Which tests matter?

Binary CPAN mirror

Page 23: Contentsggnindia.dronacharya.info/IT/Downloads/SubInfo/6thSem/PPT/Web... · PERL Syntax Overview • There is no need to have a main() function or anything of that kind. • Perl

Assignment 14

What is difference between CGI and Perl?