Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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";
produces:
cwd [1] = /export/home/foo cwd [2] = /tmp cwd [3] = /tmp/steve


Comment on Re: working with relative paths
Select or Download Code
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
Node Status?
node history
Node Type: note [id://403952]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (15)
As of 2014-09-16 20:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (46 votes), past polls