Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the Perl equivalent of PHP's $_SERVER[...]?

Tags:

cgi

perl

What's the Perl equivalent for the following PHP calls?

$_SERVER["HTTP_HOST"]
$_SERVER["REQUEST_URI"]

Any help would be much appreciated.

like image 435
dandemeyere Avatar asked Aug 05 '10 06:08

dandemeyere


3 Answers

Another way, than variable environement, is to use CGI :


use strict;
use warnings;
use CGI ;

print CGI->new->url();

Moreover, it also offers a lot of CGI manipulation such as accessing params send to your cgi, cookies etc...

like image 153
benzebuth Avatar answered Nov 18 '22 02:11

benzebuth


Environment variables are a series of hidden values that the web server sends to every CGI you run. Your CGI can parse them and use the data they send. Environment variables are stored in a hash called %ENV.

For example, $ENV{'HTTP_HOST'} will give the hostname of your server.

#!/usr/bin/perl

print "Content-type:text/html\n\n";
print <<EndOfHTML;
<html><head><title>Print Environment</title></head>
<body>
EndOfHTML

foreach my $key (sort(keys %ENV)) {
    print "$key = $ENV{$key}<br>\n";
}

print "</body></html>";

For more details see CGI Environmental variables

like image 6
Nikhil Jain Avatar answered Nov 18 '22 03:11

Nikhil Jain


Or you can do this and use the variable $page_url.

my $page_url = 'http';
$page_url.='s' if $ENV{HTTPS};
$page_url.='://';
if($ENV{SERVER_PORT}!=80)
{
    $page_url.="$ENV{SERVER_NAME}:$ENV{SERVER_PORT}$ENV{REQUEST_URI}";
}
else
{
    $page_url.=$ENV{SERVER_NAME}.$ENV{REQUEST_URI};
}
like image 5
TechDex Avatar answered Nov 18 '22 03:11

TechDex