in reply to Cannot open a file using a relative path

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.

Replies are listed 'Best First'.
Re^2: Cannot open a file using a relative path
by Nik (Initiate) on Apr 24, 2007 at 21:38 UTC
    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.

        Then i guess somehting like this might work:
        open FILE, ">>$ENV{'DOCUMENT_ROOT'}/folder/subfolder/digest.passwd" or + die $!;
        DocumentRoot = "d:\www"

        I though that Perl was asking apache for paths but now that ima thinking it more and more if Perl asked apache about interpreting '/' and apache said that '/ = "d:\www"' then Perl would have no way of differentiate real hdd paths from apache relative ones.

        But linkign to files dows work in a relative way: look
        print p( {align=>'center'}, a( {href=>'/cgi-bin/vault.pl'}, img{src=>'/data/images/hellas.gif'} ));
        Here Perl can find is own way to determine and opne the file, how come it doesnt need an absolute path here?

        One more thing is that if i move my passwd file to a place outside of my web space aapche sees the i ould be needign absolute paths and this ame script is about to run to 2 different hosts with different os , hence paths. How would i be able to specify the correct path since the paths would differ?