Perl redirect - a Perl CGI redirect example

Perl CGI redirect FAQ: Can you share some Perl CGI redirect example code?

Here's the source code for a simple Perl CGI redirect example. An HTML redirect is pretty easy to program manually, but the Perl CGI.pm module makes it even easier to redirect a user from one page to another.

Perl redirect CGI script example

Here's the source code for this Perl CGI redirect example script:

#!/usr/bin/perl -Tw
#
#  PROGRAM:  redirect.cgi
#
#  PURPOSE:  A simple Perl redirect CGI example.
#            Demonstrates how to generate a simple redirect request 
#            for the remote browser that accesses this CGI program.
#
#  Created by alvin alexander, devdaily.com.
#

#-----------------------------------#
#  1. Create a new Perl CGI object  #
#-----------------------------------#

use CGI;
$query = new CGI;

#----------------------------------------------------------------------#
#  2. Issue the redirection request.  (Always use an 'absolute' URL.)  #
#----------------------------------------------------------------------#

print $query->redirect('http://www.devdaily.com/');

(I was going to share a redirect example here, but it wouldn't be too exciting, as it will just redirect you from the given URL to the URL I specify. I'll have to leave that as "an exercise for the reader." :)

Make your Perl redirect script executable

When I first wrote this blog post I forgot to note that you need to make your Perl CGI redirect script executable for it to work, at least on a Unix or Linux system. If you haven't done this before, see the second comment below for an example of how to make your script executable.

Other than that, I hope this Perl redirect CGI example has been helpful.