Tuesday, July 26, 2011

Perl Interfaces

Perl Interfaces:


1. Perl CGI - common gateway interface.

2. Perl GUI - Graphical user interface.


Perl CGI - common gateway interface:

Introduction:

Perl CGI is a html interface. Its serve the Apache server.

exm: http://localhost/perl.cgi

This page is designed to help novice programmers learn the Perl programming language. Specifically, it's designed to help them learn enough to run CGI scripts on a Unix Web server.

This page grows out of my own experience. When I started out on the Web I was new to Unix, and had no formal training as a programmer. I wanted to create dynamic pages for my Web site, though, and everyone said Perl was the way to go. They were right: It was the way to go. It sounds trite to say that "Perl changed my life," but that's basically what happened.

Still, it was an uphill battle. In particular, many of the resources for learning Perl seemed to assume that I was already an experienced programmer, or at least an experienced Unix user. I've made some assumptions in these pages, but not those assumptions.



installation:

1. we need install perl
2. sudo aptitude install libapache2-mod-perl2
3. mkdir /var/www/cgi
4. cd /etc/apache2/sites-available/
5. cp default perl
6. nano perl
7. put
<VirtualHost *80>
DocumentRoot /var/www/cgi
ServerName perl

ScriptAlias /cgi/ /var/www/cgi/

<Directory /var/www/cgi/ >
Options ExecCGI
AddHandler cgi-script cgi pl
</Directory>

</VirtualHost>

8 . cd /etc/apache2/sites-enable/

9. ln -s /etc/apache2/sites-available/

10. nano /etc/hosts

11. add 127.0.0.1 perl

12. sudo /etc/init.d/apache2 restart


First CGI program Hello world

1. create file hello.cgi on /var/www/cgi/hello.cgi


#!/usr/local/bin/perl

# hello.pl -- my first perl script!

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

print "Hello, world!\n";

2. open web browser type http://perl/hello.cgi

3. enjoy perl cgi


Perl GUI - Graphical user interface:

Introduction

1. we need CPAN so install CPAN

2. Use perl GUI Tk.

Perl/Tk Requirements

Before starting with the tutorial, make sure you have the following things. If some are missing you still can learn perl - but you will not be able to use it to its full power.

  1. ActivePerl from http://www.activestate.com/ActivePerl/ for windows - for programming in Windows. Linux don't need any special outside interpreter because it already has it in most of the distributions.
  2. A good text editor. I would recommend Crimson Editor(http://www.crimsoneditor.com/) for Windows and XEmacs for Linux.

Installing/Using Perl

In Unix/Linux you can execute your perl scripts by typing "perl <filename>" at command prompt. But before you do that make sure you have both Perl and its Tk module. Most linux distributions have perl - but quite a few don't have the Tk module. Make sure that the system you are using have the Tk module. If you don't have it, go to http://www.cpan.org and download the perl module. Or you can use the perl's CPAN module to install the Tk module. To do this, open a terminal and enter the following command
perl -MCPAN -e shell
cpan> install Bundle::CPAN
cpan> reload cpan
cpan> install Tk


(or)

sudo perl -MCPAN -e "install Tk"

First program Hello world

create hello.pl

#!/usr/local/bin/perl
use Tk;
# Main Window
my $mw = new MainWindow;
my $label = $mw -> Label(-text=>"Hello World") -> pack();
my $button = $mw -> Button(-text => "Quit",
-command => sub { exit })
-> pack();
MainLoop;


To run cli :> perl hello.pl

enjoy...............




No comments:

Post a Comment