Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Can't find relative path

by jag150s (Initiate)
on Mar 24, 2012 at 20:26 UTC ( #961434=perlquestion: print w/ replies, xml ) Need Help??
jag150s has asked for the wisdom of the Perl Monks concerning the following question:

I am setting up Perl on a new server, Winserver 2008, IIS7.0 Got most everything working fine, but in moving existing code over from another server, it won't work because the new server thinks file references are all from the website root, rather than from the directory the code is executing from.
On the server, the website is located at C:/abc
I have a piece of code in directory C:/abc/def, using this code:
$cat2="data/file.txt"; open (FILE,"$cat2");
It is trying to read file.txt from C:/abc/data, instead of c:/abc/def/data
Why won't it use the addressing relative to where the code is executing? It worked fine on the previous server. Is there some configuration item I missed in IIS or in setting up ActivePerl?

Comment on Can't find relative path
Download Code
Replies are listed 'Best First'.
Re: Can't find relative path
by NetWallah (Abbot) on Mar 24, 2012 at 20:43 UTC
    Try adding this to your code:
    print "<b>$_</b>: $ENV{$_}<br>\n" for sort keys %ENV;
    You should see the variables SCRIPT_TRANSLATED and SCRIPT_NAME, which contain the information you need.
    SCRIPT_NAME is essentially the host absolute URL (minus the scheme, host, and port) for script, and SCRIPT_TRANSLATED is the corresponding physical file.
    You can extract the path from these, and use that to prefix your "data/file.txt" before opening it.

                 All great truths begin as blasphemies.
                       ― George Bernard Shaw, writer, Nobel laureate (1856-1950)

      Thanks, but the question isn't how to fix it in the code; I have several solutions for that. The question is why it's happening on this server and not on my previous (shared hosting) servers. Problem is I have a couple hundred previously-built Perl modules that expect to see a relative path, and I don't want to have to go through & rewrite them all if there's a way to fix Perl or the server to actually use the relative path.
        How about adding a symbolic link on the server, from the directory it is trying to access, to the directory where the data is ?

                     All great truths begin as blasphemies.
                           ― George Bernard Shaw, writer, Nobel laureate (1856-1950)

Re: Can't find relative path
by dasgar (Curate) on Mar 24, 2012 at 20:42 UTC

    I would recommend using the cwd function from the Cwd module, which will give you the current directory of script so long as your script isn't changing directories (i.e. system calls to cd). Then you can append the relative path of the file to the output of cwd. By doing so, you should then be able to move your script and folder structure to any folder and it should work as far as paths to files are concerned.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://961434]
Approved by dasgar
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2016-05-28 12:41 GMT
Find Nodes?
    Voting Booth?