5% assure discount from here only

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)

Sunday, February 20, 2011

(21) Create a HTML Table based on the result of a database query.

Solution :

This type of requirement generally comes when u r working on website development. And generally we create this type of table by typing some html syntax of Table, TR, TH, TD etc. and than fulfill the requirement accordingly,

my $sth = $dbh->prepare( "select id, name, status from mytable where something = ... " );
$sth->execute() or die "Failed to query";
while (my $row = $sth->fetchrow_hashref) {

Here we fill the column with the database values.

}

$sth->finish;

Every time, whenever this type of requirement arise, same type of code implies. But HTML::Table makes things better by taking out most of the HTML drudgery, but you still need to loop through adding rows to your table.

This is where HTML::Table::FromDatabase comes in - it’s a subclass of HTML::Table which accepts an executed DBI statement handle, and automatically produces the table for you.

For instance:

my $sth = $dbh->prepare(
"select id, name, status from mytable where something = ..."
);
$sth->execute() or die "Failed to query";

my $table = HTML::Table::FromDatabase->new( -sth => $sth );
$table->print;

This is the very simple way to do it, You can also include more option as required. As HTML::Table::FromDatabase is a subclass of HTML::Table, all of HTML::Table’s options can still be used to control how the generated table appears, for example:

* -class => ‘classname’ to give the table a specific class to help you apply CSS styling
* -border => 1 to apply borders, -padding => 3 to set cell padding
* -evenrowclass and -oddrowclass if you want to have different styling for even and odd rows (e.g. alternating row backgrounds).

The full list of options can be found in the HTML::Table documentation.

Saturday, February 5, 2011

(20) How to get html content of a site?

Solution :

I love to do programming in PERL because of so many reasons, out of them one is to get content of a site is very easy. You can get any site content by using the following script.

For running this script the only thing you should have is cpan module named WWW::Mechanize which can be easily downloaded from search.cpan.org or can be installed from you machine by typing following command on terminal

$ su -
#Enter your root password

cpan WWW::Mechanize

#!/usr/bin/perl -w

use strict;
use WWW::Mechanize;

### Enter url like (http://www.facebook.com) ###

print "Enter the URL\n";
my $url = <>;

my $mech = WWW::Mechanize->new;
$mech->get($url);

print $mech->content();

(19) Setting Up Perl Development Environment in Windows

Solution :

Basically PERL runs on Linux by default, but in Today's world peoples are using Microsoft Windows on Huge basis because of its functionality, features, GUI and of some many reasons. Here are some steps which are very useful in setting up PERL development in Microsoft Windows. We will use Windows environment through tutorial series. For your convenience, we will use all free development tools. You can also find such the same free tool in your platform such as Linux or Mac.

Download and install ActivePerl


You first need to download ActivePerl the latest version from http://www.activestate.com/activeperl/. The file we downloaded at the time of this tutorial being written was ActivePerl-5.10.0.1004-MSWin32-x86-287188.msi. You can also find the installation files for other systems and versions via following link

http://www.activestate.com/activeperl/downloads/

To install ActivePerl just double click on the installation file. It will guide you through steps with options. Just make your own choices and click next.

To check you installation successfully, you can open command line through Run window and type following command:

perl -version.

If you installed Perl successfully, you will see the version and copyright information.

Download and install Perl IDE: Open Perl IDE

Open Perl IDE stands for integrated development environment. It includes source code editor with syntax highlight, interpreter, and debugger tool. The IDE helps you a lot while develop Perl Program. We highly recommend that you should use Open Perl IDE because it is free, simple, intuitive and easy to use.

You can download the Open Perl IDE the latest version via following link: http://open-perl-ide.sourceforge.net/

With Open Perl IDE you don’t need to install to run it. First you unzip the downloaded file. (If you don’t have zip tool you can download a free tool called 7-ZIP from http://www.7-zip.org/ ). To launch the Open Perl IDE, you can open the unzip folder, find the file PerlIDE.exe, double click on it.

Now you are ready to start learning Perl!