Redirecting URL in Perl

pradeep's Avatar author of Redirecting URL in Perl
This is an article on Redirecting URL in Perl in Perl.
A very common technique used by many developers is to redirect the user to another URL or the referrer URL after doing some work e.g. processing a form. In Perl all headers have to printed before any actual content is outputed.

A very simple way of redirecting to another URL
Code: Perl

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

Doing the same thing using the CGI module.
Code: Perl

use CGI;

my $cgi = new CGI;

print $cgi->header(-location => q[]);

You may also pass the HTTP status code, sometimes it's needed e.g. if you pass a 302 status code, the URL of the page in the browser remains the same. Here's all the status codes used for redirecting a page.

301 Moved Permanently
302 Found
303 See Other
So, here's how we redirect with the status.
Code: Perl

use CGI;

my $cgi = new CGI;

print $cgi->header(-location => q[], -status=>301);
shabbir, sundeep.kumar likes this
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Nomination for article of the month for December 2008 Started.
dsouza's Avatar, Join Date: Jan 2009
Go4Expert Member
nice post
fazalca's Avatar, Join Date: Apr 2009
Newbie Member
print redirect('URL');

will do the same thing
sundeep.kumar like this