rgcosma has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am trying to run some tasks on files in the site root and in the current directory, while being called via CGI. The way to find where I am in managed languages is with Server.MapPath() with parameters like "." for the current directory, "~" for the application root or "/" for the website root. The problem is getcwd or PATH_TRANSLATED contain the location of the script on the disk, not the (virtual) folder it is called from. Also, I couldn't find any way to determine the root of the site. Could you suggest a Perl equivalent for that .net function? Thank you
  • Comment on IIS6 + Perl CGI: finding locations on disk

Replies are listed 'Best First'.
Re: IIS6 + Perl CGI: finding locations on disk
by ikegami (Patriarch) on Sep 13, 2008 at 16:31 UTC

    $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.

      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{$_}); }
        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.