At some point while writing a CGI script in Perl, many newbies have needed to print all the environment varibles in Perl, sometimes for debugging or for the sake of curiosity. Well, here is the code to print all the CGI environment variables. Code: #!/usr/bin/perl print "Content-type: text/html\n\n"; print "<pre>\n"; foreach $key (sort keys(%ENV)) { print "$key = $ENV{$key}<p>"; } Output: Code: DOCUMENT_ROOT = /share/htmlGATEWAY_INTERFACE = CGI/1.1 HTTP_ACCEPT = text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 HTTP_ACCEPT_CHARSET = ISO-8859-1,utf-8;q=0.7,*;q=0.7 HTTP_ACCEPT_ENCODING = gzip,deflate HTTP_ACCEPT_LANGUAGE = en-us,en;q=0.5 HTTP_CACHE_CONTROL = max-age=0 HTTP_CONNECTION = keep-alive HTTP_HOST = 127.0.0.1 HTTP_KEEP_ALIVE = 300 HTTP_USER_AGENT = Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0 PATH = /sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin QUERY_STRING = REMOTE_ADDR = 127.0.0.1 REMOTE_PORT = 4520 REQUEST_METHOD = GET REQUEST_URI = /cgi-bin/test.pl SCRIPT_FILENAME = /share/cgi-bin/test.pl SCRIPT_NAME = /cgi-bin/test.pl SERVER_ADDR = 127.0.0.1 SERVER_ADMIN = root@localhost SERVER_NAME = localhost SERVER_PORT = 80 SERVER_PROTOCOL = HTTP/1.1 SERVER_SIGNATURE = Apache/2.0.52 (Red Hat) Server at localhost Port 80 SERVER_SOFTWARE = Apache/2.0.52 (Red Hat)
This may be a good point to introduce the module Data:umper. This module is one of the most useful modules for this kind of debug. Code: #!/usr/bin/perl use Data::Dumper; print "Content-type: text/html\n\n"; print "<html><head/><body><pre>\n"; print Dumper(\%ENV); print "</pre></body></html>\n"; Note: I'm not working in the internet environment so some errors can appear on my code, but the idea is there