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

open(FILE, ">>/somefolder/somesubfolder/digest.passwd") or die $!; print FILE "$user:$realm:" . Digest::MD5::md5_hex("$user:$realm:$ +pass") . "\n"; close(FILE);
Wehn i try to use this relative path instead of d:\www\blabla which is absolute apache tells me it cant find the file.
Why i dotn sue an absolute path then? Because the same script will run both on localhost and remote server and the paths are not the same, so i need a relative path in order to work.
I could use the $ENV{'SERVER_NAME'} to determine in which host the script runs and then set an appropriate path variable but i wan to avoid that by using a relative path.

Replies are listed 'Best First'.
Re: Cannot open a file using a relative path
by ikegami (Patriarch) on Apr 24, 2007 at 21:34 UTC

    Don't assuming your work directory is set to the directory which contains your script. Putting the following at the top of your script should solve the issue.

    use File::Basename qw( dirname ); BEGIN { # Set working directory to the one in which the script resides. chdir(dirname($0)); }

    By the way, the above is more reliable than FindBin.

      Actually i have an index.html file pointing to cgi-bin/index.pl file
      What si my workign directory? And why we even care about whats is the cwd, iam askign to open a file starting from Cocument Root which is / , why apache can locate th file based on that?

        My apologies, shigetsu and I did not read your question carefully enough.

        So if I understand clearly now, you used /somefolder/somesubfolder/digest.passwd thinking it would be relative to the DocumentRoot. The catch is that Perl knows absolutely nothing about DocumentRoot. The leading / tells Perl it's an absolute path. While you want to access d:\www\blabla/somefolder/somesubfolder/digest.passwd, you're actually attempting to access d:/somefolder/somesubfolder/digest.passwd.

        It's a bit safer if you don't store your password file in your public web space. Solve two problems with one stone by moving your password file to somewhere outside of your DocumentRoot directory tree.

        In short,

        Cocument Root which is /

        I think you meant "/ is DocumentRoot". If so, that's only the case in Apache's configuration files.

        why apache can locate th file based on that?

        Apache has nothing to do with it. Perl is the one trying to open the file.

Re: Cannot open a file using a relative path
by shmem (Chancellor) on Apr 24, 2007 at 21:52 UTC

    Nik - If you are dyslexic, skip this post, please.

    Otherwise: Please, Nik. Your spelling hurts.

    Before posting, re-read what you are going to post and correct any spelling errors. Your English isn't bad to the extent that the "I have no clue" argument would hold. I perceive your negligence in this behalf as a disrespect to all of us.

    You want correct answers? Ask correct questions, then. Put some effort into them - at least with regard to the spelling.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Cannot open a file using a relative path
by shigetsu (Hermit) on Apr 24, 2007 at 20:25 UTC

    You might find FindBin (which ships with Perl 5.8.8 and later) useful:

    Use it as following:

    use FindBin qw($Bin);

    Then insert $Bin (path to bin directory from where script was invoked) after >> and before your relative path in your open() call.

Re: Cannot open a file using a relative path
by blazar (Canon) on Apr 29, 2007 at 17:58 UTC
    Wehn i try to use this relative path instead of d:\www\blabla which is absolute apache tells me it cant find the file.

    Once again, why don't you say that you posted the very same question in clpmisc? (link @ GG.)

    A reply falls below the community's threshold of quality. You may see it by logging in.