Travis M. has asked for the wisdom of the Perl Monks concerning the following question:

Gents, I am having a problem executing some functions within perl scripts via relative paths. This snippet of code comes from an HTML file that executes a perl script via a relative path. When the user selects the option, the PERL script is executed. Works like a champ.
<option VALUE="./cgi-bin/test.pl?sys=WMD">
However, on a different HTML page, I run into some problems. A form and submit button is automatically is generated by calling the following perl script:
<script src="./cgi-bin/check.pl"></script>
fine The output of the above is a javascript file that is called later in the html document. That js file creates a form tag with a submit button whose action is yet another PERL script, as follows:
<form style='margin-bottom:0; margin-top:0;' ACTION = './cgi-bin/upda +te.pl' TARGET = 'FRAME2'> <input type='submit' value='Update Page'></form>
It's apparent the script begins to do it's thing, however the problem results when a "open" function is called within the script (as listed):
open (UPDATE, "../update/update.txt") or die "Cannot open update.txt";
(relative to the perl script being called, the file is up one directory, over to the 'update' directory, then down to the 'update.txt' file.)
I get the dreaded "Cannot open update.txt" message.
The following is a rough schematic of my website:
ROOT_NODE html files cgi_bin/ all .pl and .cgi files are here update/ update.txt is here jsfiles/ all javascript files are here etc/ other files

I've checked the relative path from the working html document several hundred times and am convinced it is correct. The 'update.txt' file also exists in every case. File and folder permissions are not the problem either. The relative path simply does not work.
When I put in the absolute path, however, it works fine....
I also have the same type of problem when calling an 'opendir' function.
I'm using Windows 2000 Server.
Any thoughts?
Thanks,
Travis

Replies are listed 'Best First'.
Re: problems with relative paths when using open and opendir functions
by Zaxo (Archbishop) on Jan 19, 2005 at 05:31 UTC

    Chances are the working directory isn't what you think it is. Does it work with an absolute path like "$ENV{DOCUMENT_ROOT}/update/update.txt" ?

    After Compline,
    Zaxo

Re: problems with relative paths when using open and opendir functions
by Tanktalus (Canon) on Jan 19, 2005 at 05:25 UTC

    Who says anything about what the current directory is when your script is executing? Personally, I use FindBin;, and then use "full" pathnames based on $FindBin::Bin everywhere. Then current working directories never matter.

Re: problems with relative paths when using open and opendir functions
by larryp (Deacon) on Jan 19, 2005 at 18:08 UTC

    If you're using Windows 2000, chances are you're also using IIS 5. Is this correct?

    If so, that's the problem! :) Seriously, it's an issue with IIS 5. IIS 5 interprets relative pathing from the root of the site, not the current directory. In your case, change

    open (UPDATE, "../update/update.txt") or die "Cannot open update.txt";

    to

    open (UPDATE, "./update/update.txt") or die "Cannot open update.txt";

    and it should work. This problem seems to be remedied in IIS 6.

    HTH,

    /Larry