Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: working with relative paths

by steves (Curate)
on Oct 30, 2004 at 01:57 UTC ( [id://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";
produces:
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 testchdir.pl music failed to open directory: No such file or directory at testchdir.pl li +ne 9. username@computer:~$
        - apotheon
        CopyWrite Chad Perrin

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-19 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found