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

Hello,
I am currently posting information from a file to the web, but I am now trying to get the information from a file in a different directory then my perl script. I need to first change directory to the home directory and then change directory to the directory where the file is located but am unsure of how to do this so far I have

#!/usr/bin/perl use strict; my $directory = './mydirectory'; chdir($directory) || die "Fail to change directory: $!\n";

I keep getting fail to change directory. I'm trying to change the directory before I access the file is this correct ? Thanks for the help in advance!

Formatting and code tags added by davido to match author's intent.

Replies are listed 'Best First'.
Re: How do I change directory to root directory
by amw1 (Friar) on Mar 04, 2005 at 16:52 UTC
    I'm a little unclear as to what is wrong so I'll take a few stabs :)

    Your code will successfuly change to ./mydirectory as long as the script is executed in the directory that contains ./mydirectory.

    If you need to change to that directory from anywhere on the filesystem you just need to use a full path. i.e.

    #!/usr/bin/perl use strict; my $dir = "/foo/bar/mydirectory"; chdir($dir) || die "Whoops: $!\n";

    When the script exits you will still be in the directory you started in. The shell forks and runs your perl script, the script changes directory and exits. Nowhere in this process does the child process change the CWD of the parent. I don't actually believe that thisis possible.

    It's kinda cheesy but if you want to run the script and be at the directory the script chdir'd to when it finishes you could run the chdir and then exec a shell. You would get a new shell to do with what you want. However, this would be a sub-shell of the one you started in.

    Hope something in this rambling helps.

Re: How do I change directory to root directory
by ikegami (Patriarch) on Mar 04, 2005 at 16:52 UTC
    You're getting the error because there's no subdirectory named "mydirectory" in the subdirectory in which the script runs. You could use an absolute directory name, like chdir("/home/ikegami/mydirectory/") for example.
Re: How do I change directory to root directory
by Taulmarill (Deacon) on Mar 04, 2005 at 16:53 UTC
    you can access a file withour changing the directory. just provide the full path to open. $ENV{HOME} should contain your homedir on linux/unix platforms.
      So if I type in my $file ="home/test/sever/default/test.log.2005.03.04";
      That should work ? I don't need to change to the Test directory from home ?
        Correct. As long as the directory you run the script from has home as a directory underneath it.

        As a side note, use full paths unless there is a very good reason not to. It makes your life much easier.

Re: How do I change directory to root directory
by jpeg (Chaplain) on Mar 04, 2005 at 17:04 UTC
    Your script works for me. What error do get in $! ?
    can you try adding debug statements like:
    my $cwd = `pwd`; print ("$cwd\n");

    As a side note, you can pass a full path to an open statement. You don't need to be in the same directory as the file to open it and get a valid filehandle.

    You've probably truncated your script and you say that your die statement does print, so this might not be applicable, but you should see perlfaq 8.34. A script can't change the parent shell's directory.