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

Hi Monks,
I am trying to open a file that is located outside if my cgi-bin directory. My problem is that I can't use an absolute link to the file; like the code I am showing.
I am using this code,
BEGIN { $0 =~ '(.*[\\\/])\w+\.\w+$'; $curr_dir = $1 || "./"; } open (FILE, "<${curr_dir}info.txt") || print "There is no INFO file he +re $0"; while ( my $line = <FILE> ) { ...code here... } #It doesn't work...

But I can use it if I am on the some directory level. How could I open a file using similar code to open a file that is outside of the current directory, I know I could use a link to the file like "../info.txt" but it doesn't open the file, unless the path is absolute to the file.
Any help?
Thanks!!!
open (FILE, "<c:/progra~1/apache~1/apache2/htdocs/info.txt") || print +"There is no INFO file here $0"; while ( my $line = <FILE> ) { ...code here... }

Replies are listed 'Best First'.
Re: File Open Issue
by jZed (Prior) on May 31, 2005 at 15:52 UTC
    I would suggest printing $! to find out what the actual error is. Can't fix an error if you don't know what it is.
      There is no error printed it just says that it can't open the file based on the path
        File open errors do not print by default. You must specifically put something along the lines of "or die $!" after the open.
Re: File Open Issue
by thundergnat (Deacon) on May 31, 2005 at 20:03 UTC

    I know I could use a link to the file like "../info.txt" but it doesn't open the file, unless the path is absolute to the file.

    Where did you get that idea? Of course you can use . and .. directories in a path to a file for opening.

    Since you don't state where the files' directory is relative to the cgi-bin directory, it is difficult to give very precise advice. However, in general, use ../ to go up one level in the directory hierarchy and the exact directory name to go down.

    Say you have a directory structure like this:

    |--apache2--|---cgi-bin
                |
                |---htdocs
                |
                |---images
    

    or whatever. To access a file from the htdocs directory, starting at the cgi-bin directory, you would specify: '../htdocs/info.txt'. Substituted back into your script:

    use warnings; use strict; $0 =~ '(.*[\\\/])\w+\.\w+$'; my $curr_dir = $1 || "./"; # Note, you would be much better off with # use Cwd; # and # my $curr_dir = cwd(); $info_file = $curr_dir . '../htdocs/info.txt'; open my $fh, '<', $info_file or die "Could not open file, $!"; while (my $line = <$fh>){ #process line }
      Your code doesn't work, here is what I am trying:
      use Cwd 'chdir'; chdir "../htdocs/"; open (FILE, "<$ENV{'PWD'}info.txt") || print "There is no file here in + $ENV{'PWD'}info.txt"; while ( my $line = <FILE> ) { chomp($line); #code.... }

        Sigh. My fault for posting untested code.
        You are still missing the point about using Cwd. You don't need the chdir and you shouldn't rely on $ENV{PWD}. Here is a (tested) version showing what I mean.

        use warnings; use strict; use Cwd; my $curr_dir = cwd(); my $info_file = $curr_dir . '/../htdocs/info.txt'; open my $fh, '<', $info_file or die "Could not open file, $!"; while (my $line = <$fh>){ # process text }
Re: File Open Issue
by zentara (Cardinal) on May 31, 2005 at 17:01 UTC
    open (FILE, "<${curr_dir}info.txt")

    I don't do windows, but that file

    ${curr_dir}info.txt
    looks funny to me. Are you sure about that? Maybe
    open (FILE, "<"$curr_dir/info.txt")
    In any event, you should print out $curr_dir and see what it says. I'll bet it's messed up.

    I'm not really a human, but I play one on earth. flash japh
Re: File Open Issue
by tchatzi (Acolyte) on May 31, 2005 at 23:12 UTC
    Well it's windows what more can i say.....
    Use your open function like this
    open (FILE, "<..\\htdocs\\info.txt") || print........
    Tell us if it works

    ``The wise man doesn't give the right answers, he poses the right questions.'' TIMTOWTDI
      Yes it's Windows, it didn't work like that.

      If this would work on windows:

      open (FILE, "<..\\htdocs\\info.txt") || print........

      then this would also work on windows:

      open (FILE, "<../htdocs/info.txt") || print........
      Well it's windows what more can i say.....

      I'll let you decide ... but nothing would probably be appropriate.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
        That's way I am going trough this because this line of code,
        open (FILE, "<../info.txt") || print "There is no file here info.txt";

        doesn't work unless it has a absolute link, and I can't have it like that, it has to be relative.