4WebHelp: Using Environment Variables in Perl

Ever had to change host, or change URL? You'll know how much of a pain it is to have to modify all your scripts... But the good news is that you don't have to! This tutorial will explain how...

This tutorial will show you how to get the address for your base directory, so that if you move your site, or your host does it for you, your script carries on working. It also shows you how to get the full address of the script you are executing (so you can move it between directories without changing it) and how to get the address of the HTML page that called the script (so you can return to it, even if you change its name, store it somewhere else, or have several pages calling the same script.)

The information you need is stored in environment variables and these are available to Perl scripts at run time in the array "ENV". Individual items are information which is retrieved by specifying the keyword associated with them. Thus the string containing the base directory address can be found in $ENV{'DOCUMENT_ROOT'} (note the squiggly brackets).

We need four items:
URL that called the script: $ENV{'HTTP_REFERER'}
address of the host server: $ENV{'HTTP_HOST'}
real address of your base directory: $ENV{'DOCUMENT_ROOT'}
location and name of the Perl script: $ENV{'REQUEST_URI'}

Some of these items are sufficient in themselves; some will need to be joined together, or joined with other text strings, to get a full address. To get the address of the HTML page that called the script, we just need $ENV{'HTTP_REFERER'}. In the script, this might look like this:

$calling_page = $ENV{'HTTP_REFERER'};

(in this example, and in the others that follow, you will need to alter the variable name on the left of the "=" to suit the script that you are amending.

To get the address of your home page, we use $ENV{'HTTP_HOST'}, like this:

$homeurl = "http://$ENV{'HTTP_HOST'}";

or

$homeurl = "http://$ENV{'SERVER_NAME'}";

(this is joining the text string "http://" to the host address (e.g. if the host address is "www.4webhelp.net", the full address derived by this statement would be "http://www.4webhelp.net/").

To get the address of the Perl script itself, we use $ENV{'HTTP_HOST'} and $ENV{'REQUEST_URI'}, like this:

$perlurl = "http://$ENV{'HTTP_HOST'}$ENV{'REQUEST_URI'}";

(this is joining the text string "http://" to the host address (e.g. www.4webhelp.net) and then to the address of the script in the site. So if the Perl script was called "perlscript.pl" and was stored in a directory called "cgi-bin" which in turn is in the root directory of "www.4webhelp.net", then the full address derived by this statement would be "http://www.4webhelp.net/cgi-bin/perlscript.pl".

All the addresses we have dealt with so far are URLs - full web addresses. Many scripts ask for "real addresses"; internal addresses which are different for each web hosting company, and sometimes different for each web site.

$basedir = $ENV{'DOCUMENT_ROOT'};

This gives you the "real" address of your root directory (the top level directory which contains your home page). To get the address of specific pages, you will need to add filenames, and in most cases directory names as well. For example, if you have a guestbook facility, and you store all the files associated with it in a directory called "guest", which in turn is stored in your root directory, then the "real" address of this directory could be derived like this:

$guestbookdir = $ENV{'DOCUMENT_ROOT'}."/guestbook/";

and if you had a file called "guestbook.html" in that directory, its real address could be derived like this:

$guestbookreal = $ENV{'DOCUMENT_ROOT'}."/guest/guestbook.html";

Of course, if you change the name of the directory in which these files are stored, or move it inside another directory, you'll need to change your script, otherwise your script will cease to work. If you can avoid specifying any explicit filenames or directory names in your script, then you will not need to change the script at all if files or directories are moved or renamed, and you can call the same script from different pages. For very simple scripts, you already have enough information to do this.

© 4WebHelp and Rod

Latest comments on this tutorial
NeedHelp
This doesnt work in a shared hosting env. DOCUMENT_ROOT points you to the actual path which doesnt work if you are using it to point to a script.
How can I fix that?
Rick Schulze
I have active state running on IIS. The environment variables are not available to Perl for some reason. If I run Komodo on the same script and simulate the CGI, the scripts fine. What in IIS config am I missing?

Thanks
Nitin
Hi,

Thanks for sharing this all with the world of perl learners.

Thanks
Nitin
sona
How to add a CLASSPATH environment variable
using %ENV?
Axel
You the Best!!

Eva
Very good site, thank you!
Bookmarked Smile
John
Very nice site!
S_Flex
I belive the $ENV{REQUEST_URI} variable is provided in apache servers.

A IIS server with Active State Perl does not provide  the $ENV{REQUEST_URI} Environment Variable.
James Allen
Thank you Thank you Thank you!!!

I have read so many articles about how to set an absolute path in perl.

This is so simple. (Quote:
it's excellent!
)
kumar
I have been pretty regular on this websiets i have always wanted to get some perl programming. but was never able to start at nayway this appers to be first step..
WillJ
Thanks for that, very handy. Nice Google ranking too!

Add a new comment


Page URL: http://www.4webhelp.net/tutorials/cgi/env_vars.php
Back to the "pretty" page!

© 2017, 4WebHelp Team.