5% assure discount from here only

Monday, April 18, 2011

(26) How to print a matching line, one line immediately above it and one line immediately below?

Solution:

In perl this type of problems or requirement can be easily solved because of its versatility.

my $line='';

open(FILE,'/home/lalit/Desktop/test.txt') or die "Could not open file " . $!;

while (<FILE>){

    if($_ =~ /how are you/ig){
        print "\nprevious line of matching pattern =>" . $line;
        print "\nCurrent Line of matching pattern =>" . $_;
        print "\nNext Line of matching pattern =>" . scalar <FILE>."\n";
    }
    $line = $_;
}

close FILE;

(25) How to use Apache2::Ajax with perl code?

Solution:

In the previous post, no. 24, i tried to show how to use CGI::Ajax with perl code. Now i am going to use Apache2::Ajax which is mod_perl interface to CGI::Ajax.

This module require mod_perl2, CGI::Ajax, as well as a CGI.pm-compatible CGI module for supplying the param() and header() methods. If available, CGI::Apache2::Wrapper will be used, which is a minimal module that uses methods of mod_perl2 and Apache2::Request to provide these methods; if this is not available, CGI (version 2.93 or greater) will be used.

To use this module you have to make some changes in your httpd.conf, which is located at /etc/httpd/conf/.

To make the use of the below example, some steps need to be followed:-

(i) Create a directory cgi-bin2 in /var/www/.

(ii) Following change must be done your httpd.conf :-

PerlModule ModPerl::Registry
Alias /cgi-bin2/ /var/www/cgi-bin2/
<Location /cgi-bin2>
     SetHandler perl-script
     PerlResponseHandler ModPerl::Registry
     PerlOptions +GlobalRequest
     PerlSendHeader On
     PerlOptions +ParseHeaders
     Options +ExecCGI
     Order allow,deny
     Allow from all
</Location>

(iii) Create a file named with myajax.pl with the following code:

#!/usr/bin/perl -w

use strict;
use CGI;
use lib '/usr/lib/perl5/site_perl/5.8.8/';

use Apache2::MyAjaxApp;

my $r =shift;
Apache2::MyAjaxApp->handler($r);

(iv) Create a file named MyAjaxApp.pm in following location:

/usr/lib/perl5/site_perl/5.8.8/Apache2/

Put the following code into the file

package Apache2::MyAjaxApp;

use Apache2::Ajax;
use mod_perl2;
 
sub check {
    my $arg = shift;
    return ( $arg . " with some extra" );
}

sub Show_Form_sub {

    my $html = "";
    $html .= "<HTML>";
    $html .= "<HEAD>";
   
    $html .= <<EOT;
    </HEAD>
    <BODY>
    <FORM name="form">
    <INPUT type="text" id="inarg"
        onkeyup="tester(['inarg'],['output_div']); return true;">
    <hr>
    <div id="output_div"></div>
    </FORM>
    <br/><div id='pjxdebugrequest'></div><br/>
    </BODY>
    </HTML>
EOT

    return $html;
}

sub Show_Form_sub1 {

    my $html = "";
    $html .= "<HTML>";
    $html .= "<HEAD>";
   
    $html .= <<EOT;
    </HEAD>
    <BODY>
    Invalid
    </BODY>
    </HTML>
EOT

    return $html;
}
 
sub handler {

    my ($class,$r) = @_;
    my $ajax = Apache2::Ajax->new($r, 'tester' => \&check);

    $r->print($ajax->build_html('html' => \&Show_Form_sub));
    return Apache2::Const::OK;

}

1;

Restart your apache service and run in the browser with the following url:

http://localhost/cgi-bin2/myajax.pl

Monday, March 28, 2011

(24) How to use CGI::Ajax with perl code?

Solution:

A cpan module CGI::Ajax is available to make the use of Ajax in your web application. It is an object-oriented module that provides a unique mechanism for using perl code asynchronously from javascript- enhanced HTML pages.

A very simple example of CGI::Ajax :-

#!/usr/bin/perl -w

use strict;
use CGI;

use CGI;
use CGI::Ajax;

my $cgi = new CGI;

my $pjx = new CGI::Ajax('checkname' => \&checkname);
print $pjx->build_html( $cgi, \&ajax_html);


sub checkname{
    my $input = shift;
    my $output = $cgi->param('name');
    return $output;
}

sub header {

    my $header = qq~<HTML>
            <HEAD>
            </HEAD>
            <BODY>~;
    return $header;
}

sub ajax_html{
  
    my $html_header = header();
    my $html_footer = footer();
  
    my $html_body = qq~<input type="button" value="Click Me" onclick="checkname(['name__lalit'], ['lalitdiv']);">
        <div id="lalitdiv">Ajaxx</div>~;

    my $full_html = $html_header.$html_body.$html_footer;

    return $full_html;
}

sub footer{
  
    my $footer = qq~</BODY>
    </HTML>~;
    return $footer;

}

Friday, February 25, 2011

(23) What is difference between DBI and DBD?

Solution:

This is one of the most frequent question can be asked or can comes to mind when you worked on Perl.

DBI is database access library, whereas DBDs are "drivers" which are used by DBI
to access particular database (eg. there is one DBD for MySQL, another one for PostgreSQL etc).
You should use DBI rather than DBDs directly.

DBI is the interface. DBD are the implementations of that interface.

Wednesday, February 23, 2011

(22) What are the CGI Environment Variables?

Solution :

CGI Environment Variables plays an very important role in web application development. If you are using perl for developing web application then you must know about CGI Environment Variables. These are the series of some hidden values that the web server(apache or can be other) sends to every CGI program you run on browser. Your program can parse them and use the data they send. Environment variables are stored in a special hash named %ENV.

Most of the frequently used CGI Environment Variables are:

KeyValue
DOCUMENT_ROOT:The root directory of your server
CONTENT_LENGTH:The length, in bytes, of the input stream that is being passed through standard input.
PATH_INFO:The extra path information followin the script's path in the URL.
HTTP_COOKIE:The visitor's cookie, if one is set
HTTP_HOST:The hostname of the page being attempted
HTTP_REFERER:The URL of the page that called your program
HTTP_USER_AGENT:The browser type of the visitor
HTTPS:"on" if the program is being called through a secure server
PATH:The system path your server is running under
QUERY_STRING:The query string (see GET, below)
REMOTE_ADDR:The IP address of the visitor
REMOTE_HOST:The hostname of the visitor (if your server has reverse-name-lookups on; otherwise
this is the IP address again)
REMOTE_PORT:The port the visitor is connected to on the web server
REMOTE_USER:The visitor's username (for .htaccess-protected pages)
REQUEST_METHOD:GET or POST
REQUEST_URI:The interpreted pathname of the requested document or CGI (relative to the document
root)
SCRIPT_FILENAME:The full pathname of the current CGI
SCRIPT_NAME:The interpreted pathname of the current CGI (relative to the document root)
SERVER_ADMIN:The email address for your server's webmaster
SERVER_NAME:Your server's fully qualified domain name (e.g. www.cgi101.com)
SERVER_PORT:The port number your server is listening on
SERVER_SOFTWARE:The server software you're using (e.g. Apache 1.3)