Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: working with relative paths

by steves (Curate)
on Oct 30, 2004 at 01:57 UTC ( #403952=note: print w/replies, xml ) Need Help??

in reply to working with relative paths

Relative paths should work with chdir, being relative to where you are when you run (not where the script is). Example from a Solaris Perl 5.8 system:

use strict; use Cwd; print "cwd [1] = ", cwd(), "\n"; chdir("/tmp"); print "cwd [2] = ", cwd(), "\n"; chdir("steve"); # relative! print "cwd [3] = ", cwd(), "\n";
cwd [1] = /export/home/foo cwd [2] = /tmp cwd [3] = /tmp/steve

Replies are listed 'Best First'.
Re^2: working with relative paths
by apotheon (Deacon) on Oct 30, 2004 at 02:19 UTC

    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
      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 music failed to open directory: No such file or directory at li +ne 9. username@computer:~$
        - apotheon
        CopyWrite Chad Perrin

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://403952]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2016-10-24 16:34 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (309 votes). Check out past polls.