in reply to IIS6 + Perl CGI: finding locations on disk

$cgi->script_name() or $ENV{SCRIPT_NAME} for the url of the script relative to the root url of the server.

Until you do a chdir, rel2abs($0) gives the absolute file system path to the script.

My server provides the document root in $ENV{DOCUMENT_ROOT}.

Combined with File::Spec and CGI, you have all you need.

Update: Example:

use File::Basename qw( dirname ); use File::Spec::Functions qw( rel2abs ); my $script_path = rel2abs($0); my $script_dir = dirname($script_path); my $script_url = $ENV{SCRIPT_NAME}; my $doc_root = $ENV{DOCUMENT_ROOT}; my $redirect_to = URI->new_abs('bar.cgi', $script_url); # /dir/bar.cgi

I can't come up with any reason to use $script_path or $doc_root.

Replies are listed 'Best First'.
Re^2: IIS6 + Perl CGI: finding locations on disk
by rgcosma (Beadle) on Sep 13, 2008 at 16:48 UTC
    Hello - and thank you for the hints. Unfortunately script_name or getcwd return the location of the script in the filesystem (let's say c:\test\test.pl). This can be mapped in to various addresses using the Virtual Folder option of IIS. If my site is at d:\webroot and test is aliased as http://localhost/somename, I can find my location as c:\test\test.pl or /somename/test.pl but have no hints on the existence of d:\webroot

      Like I already said, my server provides the document root in $ENV{DOCUMENT_ROOT}. Check your server's documentation or just dump the environment variables

      #!/usr/bin/perl use strict; use warnings; use List::Util qw( max ); print("Content-Type: text/plain\n\n"); my $max_size = max map length, keys %ENV; my $format = "%-" . ($max_size+1) . "s %s\n"; for (sort keys %ENV) { printf($format, "$_:", $ENV{$_}); }
        I have tried to print all environment variables, and document_root is simply not there. Some googling revealed IIS6 does not export this variable at all for CGI applications. I will check if ISAPI is treated differently.
      Then IIS is broken. For url
      http://server.port/this/is/all/SCRIPT_NAME?que=ry
      SCRIPT_NAME =/this/is/all/SCRIPT_NAME

      The documentation ( IIS Server Variables ) confirms this.