Found 157 Articles for PERL

How to use Cookies in CGI in Perl?

Mohd Mohtashim
Updated on 02-Dec-2019 08:14:12

1K+ Views

HTTP protocol is a stateless protocol. But for a commercial website it is required to maintain session information among different pages. For example one user registration ends after transactions which spans through many pages. But how to maintain user's session information across all the web pages?In many situations, using cookies is the most efficient method of remembering and tracking preferences, purchases, commissions, and other information required for better visitor experience or site statistics.How It WorksYour server sends some data to the visitor's browser in the form of a cookie. The browser may accept the cookie. If it does, it is ... Read More

Using POST Methods in Perl

Mohd Mohtashim
Updated on 02-Dec-2019 08:11:48

1K+ Views

A more reliable method of passing information to a CGI program is the POST method. This packages the information in exactly the same way as GET methods, but instead of sending it as a text string after a ? in the URL, it sends it as a separate message as a part of HTTP header. Web server provides this message to the CGI script in the form of the standard input.Below is the Perl script called hello_post.cgi, to handle input given by the web browser. This script will handle GET as well as POST method.#!/usr/bin/perl local ($buffer, @pairs, $pair, $name, $value, %FORM); # ... Read More

Using GET Methods in Perl

Mohd Mohtashim
Updated on 02-Dec-2019 08:09:56

451 Views

Here is a simple URL which will pass two values to hello_get.cgi program using GET method.http://www.tutorialspoint.com/cgi-bin/hello_get.cgi?first_name=ZARA&last_name=ALIBelow is hello_get.cgi script to handle input given by web browser.#!/usr/bin/perl local ($buffer, @pairs, $pair, $name, $value, %FORM); # Read in text $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/; if ($ENV{'REQUEST_METHOD'} eq "GET") {    $buffer = $ENV{'QUERY_STRING'}; } # Split information into name/value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs) {    ($name, $value) = split(/=/, $pair);    $value =~ tr/+/ /;    $value =~ s/%(..)/pack("C", hex($1))/eg;    $FORM{$name} = $value; } $first_name = $FORM{first_name}; $last_name = $FORM{last_name}; print "Content-type:text/html\r\r"; print ""; print ""; print "Hello - Second CGI ... Read More

Raise a "File Download" Dialog Box using Perl

Mohd Mohtashim
Updated on 02-Dec-2019 08:05:20

206 Views

Sometime it is desired that you want to give option where a user will click a link and it will pop up a "File Download" dialogue box to the user instead of displaying actual content. This is very easy and will be achieved through HTTP header using Perl Script.This HTTP header will be different from the header mentioned in previous section. For example, if you want to make a FileName file downloadable from a given link then it's syntax will be as follows −#!/usr/bin/perl # HTTP Header print "Content-Type:application/octet-stream; name = \"FileName\"\r"; print "Content-Disposition: attachment; filename = \"FileName\"\r"; # Actual ... Read More

Perl CGI Environment Variables

Mohd Mohtashim
Updated on 02-Dec-2019 08:04:03

1K+ Views

All the Perl CGI program will have access to the following environment variables. These variables play an important role while writing any CGI program in Perl.Sr.NoVariables Names & Description1CONTENT_TYPEThe data type of the content. Used when the client is sending attached content to the server. For example file upload, etc.2CONTENT_LENGTHThe length of the query information. It's available only for POST requests3HTTP_COOKIEReturns the set cookies in the form of key & value pair.4HTTP_USER_AGENTThe User-Agent request-header field contains information about the user agent originating the request. Its name of the web browser.5PATH_INFOThe path for the CGI script.6QUERY_STRINGThe URL-encoded information that is sent ... Read More

First CGI Program using Perl

Mohd Mohtashim
Updated on 02-Dec-2019 08:01:54

277 Views

Here is a simple Perl CGI program available in file called hello.cgi. This file has been kept in /cgi-bin/ directory and it has the following content. Before running your CGI program, make sure you have change mode of file using chmod 755 hello.cgi UNIX command.#!/usr/bin/perl print "Content-type:text/html\r\r"; print ''; print ''; print 'Hello Word - First CGI Program'; print ''; print ''; print 'Hello Word! This is my first CGI program'; print ''; print ''; 1;Now if you click hello.cgi link then request goes to web server who search for hello.cgi in /cgi-bin directory, execute it and whatever result got generated, web server sends that result ... Read More

Useful DBI Functions in Perl

Mohd Mohtashim
Updated on 02-Dec-2019 08:00:02

63 Views

Checking available_drivers@ary = DBI->available_drivers; @ary = DBI->available_drivers($quiet);Returns a list of all available drivers by searching for DBD::* modules through the directories in @INC. By default, a warning is given if some drivers are hidden by others of the same name in earlier directories. Passing a true value for $quiet will inhibit the warning.Checking installed_drivers%drivers = DBI->installed_drivers();Returns a list of driver name and driver handle pairs for all drivers 'installed' (loaded) into the current process. The driver name does not include the 'DBD::' prefix.Checking data_sources@ary = DBI->data_sources($driver);Returns a list of data sources (databases) available via the named driver. If $driver is ... Read More

Using NULL Values in Perl Database Operation

Mohd Mohtashim
Updated on 02-Dec-2019 07:57:00

688 Views

Undefined values, or undef, are used to indicate NULL values in Perl’s Database Operations. You can insert and update columns with a NULL value as you would a non-NULL value. These examples insert and update the column age with a NULL value −$sth = $dbh->prepare(qq {    INSERT INTO TEST_TABLE (FIRST_NAME, AGE) VALUES (?, ?) }); $sth->execute("Joe", undef);Here qq{} is used to return a quoted string to prepare API. However, care must be taken when trying to use NULL values in a WHERE clause. Consider −SELECT FIRST_NAME FROM TEST_TABLE WHERE age = ?Binding an undef (NULL) to the placeholder will ... Read More

COMMIT & Rollback Operations in Perl

Mohd Mohtashim
Updated on 02-Dec-2019 07:56:02

601 Views

COMMIT OperationCommit is the operation which gives a green signal to database to finalize the changes and after this operation no change can be reverted to its orignal position.Here is a simple example to call commit API.$dbh->commit or die $dbh->errstr;ROLLBACK OperationIf you are not satisfied with all the changes or you encounter an error in between of any operation, you can revert those changes to use rollback API.Here is a simple example to call rollback API.$dbh->rollback or die $dbh->errstr;Begin TransactionMany databases support transactions. This means that you can make a whole bunch of queries which would modify the databases, but none of the ... Read More

Database DELETE Operation in Perl

Mohd Mohtashim
Updated on 02-Dec-2019 07:54:26

534 Views

Perl DELETE operation is required when you want to delete some records from your database. Following is the procedure to delete all the records from TEST_TABLE where AGE is equal to 30. This operation will take the following steps.Preparing SQL query based on required conditions. This will be done using prepare() API.Executing SQL query to delete required records from the database. This will be done using execute() API.Releasing Stattement handle. This will be done using finish() API.If everything goes fine then commit this operation otherwise you can rollback complete transaction.$age = 30; my $sth = $dbh->prepare("DELETE FROM TEST_TABLE WHERE AGE = ?"); $sth->execute( $age ) or ... Read More

Advertisements