Perl error with $ENV variable

Hello my friends i have created a perl cgi script to see browser support for text and graphics but $ENV variable is not working ,

Code is:

#!/usr/bin/perl
#Author Ravinder kumar
# START

use warnings;
$nongraphic = 'lynx|CERN-Linemode|elinks';
$client = $ENV{'HTTP_USER_AGENT'};
$text = "text.html";
$graphics = "graphics.html";

if ($client =~ /$nongraphic/)
{
$htmldoc = $text;
}
else
{
$htmldoc = $graphics;
}
print "Content-type: text/html\n\n";
$docroot = $ENV{'DOCUMENT_ROOT'};
$htmldoc = join ("/",$docroot,$htmldoc);
if(open(HTML,"<",$htmldoc))
{
while (<HTML>)
{
print ;
}
close (HTML);
}
else
{
print "There is some problem\n"
}
exit (0);

#END

--------------------------------------------------------------

OUTPUT IS :

[root@cent6 cgi-bin]# ./support.pl
Use of uninitialized value $client in pattern match (m//) at ./support.pl line 11.
Content-type: text/html

Use of uninitialized value $docroot in join or string at ./support.pl line 21.

There is some problem
---------------------------------------------------------------
here u can see $ENV variable is not working

any help

BASH is not an HTTP user agent. :wink: HTTP_USER_AGENT is not going to be set when you run it from a commandline! That's a CGI thing.

For testing purposes, you can always set them manually, yourself:

HTTP_USER_AGENT="angrysnail-5.0" DOCUMENT_ROOT="/path/to/whatever" HTML="whateverfile" ./support.pl

Be very careful with this perl script, by the way. It could potentially be exploited to open arbitrary files, regardless of .htaccess limits.

hello Corona see this script is working by set %ENV variable

#!/usr/bin/perl

# Author Ravinder kumar
# Cgi script Second part

#Start
print "Content-type: text/html\n\n";

print "<table>";
foreach (sort keys %ENV)
{
  print "<tr><td>$_</td><td>$ENV{$_}</td></tr>\n";
}
print "</table>";

# END

is there any way to set %ENV so i can access individual element by $ENV variable in above script

mean by declaring %ENV variable.

What version of Perl are you using?

tyler_durden

Version is

This is perl, v5.10.1

In the output of this script (your 2nd script), what is the value of the key "HTTP_USER_AGENT" ?

tyler_durden

Output of second script :--------

   DOCUMENT_ROOT /var/www/html
   GATEWAY_INTERFACE CGI/1.1
   HTTP_ACCEPT text/html, text/plain, text/css, text/sgml, */*;q=0.01
   HTTP_ACCEPT_ENCODING gzip, bzip2
   HTTP_ACCEPT_LANGUAGE en
   HTTP_HOST localhost
   HTTP_USER_AGENT Lynx/2.8.6rel.5 libwww-FM/2.14 SSL-MM/1.4.1
   OpenSSL/1.0.0-fips
   PATH /sbin:/usr/sbin:/bin:/usr/bin
   QUERY_STRING
   REMOTE_ADDR ::1
   REMOTE_PORT 46329
   REQUEST_METHOD GET
   REQUEST_URI /cgi-bin/env.pl
   SCRIPT_FILENAME /var/www/cgi-bin/env.pl
   SCRIPT_NAME /cgi-bin/env.pl
   SERVER_ADDR ::1
   SERVER_ADMIN root@localhost
   SERVER_NAME localhost
   SERVER_PORT 80
   SERVER_PROTOCOL	HTTP/1.1
   SERVER_SIGNATURE	Apache Server at 192.168.3.15 Port 80
   SERVER_SOFTWARE	Apache

It will not do that when you run it by ./programname as you did above. BASH is not an HTTP user agent. You'll have to export the variables yourself.

Sorry Corona for this but u cannot understand, bash variable is different thing
that access by this perl module

use Env;

this is cgi variable which is stored in %ENV hash variable through which we can access $ENV variable

u can see the second script it is working script .

There is way to access $ENV variable by set %ENV hash globally

but i do not know how

---------- Post updated at 02:32 PM ---------- Previous update was at 01:30 PM ----------

:slight_smile: Finally the script has created successfully

Thanks for your help my friends , especialy corona sorry for above words

Working CODE ------

#!/usr/bin/perl
#Author Ravinder kumar
use warnings;
print "Content-type: text/html\n\n";

# Set $ENV variable here , to set another $ENV variable use different
# foreach loop, otherwise it will break up the script

foreach(sort keys %ENV)
{
$client = $ENV{'HTTP_USER_AGENT'};
}

# Variables

$nongraphic = 'Lynx|CERN-Linemode|ELinks';
$text = "text.html";
$graphics = "graphics.html";

# Support Checking

if ($client =~ /$nongraphic/)
{
$htmldoc = $text;
}
else
{
$htmldoc = $graphics;
}
$path = "/var/www/cgi-bin";
$htmldoc = join ("/",$path,$htmldoc);

# Result

if(open(HTML,"<",$htmldoc))
{
while (<HTML>)
{
print ;
}
close (HTML);
}
else
{
print "There is some problem\n"
}
exit (0);

#END

There is nothing magical or CGI-only about the $ENV variable. They are just environment variables. BASH can also set as I illustrated above, which is necessary when you wish to run it like ./programname as you did above.