in reply to Re: working with relative paths
in thread working with relative paths

Maybe it "should" work (at least I think so), but the problem is that it doesn't. I'm beginning to wonder if it's some kind of system configuration issue. Is there any reason chdir() would work differently for a scalar variable than for hardcoded paths? Obviously, I'm trying to make a script that works for more than one filepath without having to rewrite the script every time.

- apotheon
CopyWrite Chad Perrin

Replies are listed 'Best First'.
Re^3: working with relative paths
by itub (Priest) on Oct 30, 2004 at 02:29 UTC
    chdir has to work or I'll eat my hat, ;-) as it is one of the most fundamental functions. Perhaps if you posted a short but complete example, along with its actual output and the expected result, it would be easier to figure out what's going on.

    A couple of tricks that might help: use strict, warnings, and diagnostics; try chdir $direct or die $! to see if there is an error message.

      First, the code I just used:

      #!/usr/bin/perl -w use strict; my ($direct); chomp($direct = $ARGV[0]); shift @ARGV; chdir($direct) || die "failed to change to directory: $!"; opendir(WORKDIR,$direct) || die "failed to open directory: $!"; closedir(WORKDIR) || die "failed to close directory: $!";

      Next, the output:

      username@computer:~$ perl testchdir.pl music failed to open directory: No such file or directory at testchdir.pl li +ne 9. username@computer:~$
      - apotheon
      CopyWrite Chad Perrin

        You need

        opendir(WORKDIR,".")

        For example, if $direct is "xyz" and you cd xyz, then opendir xyz will operate on xyz/xyz, which is not what you want.