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

I have a list of paths in a file => small.txt i wish to open that file, copy the path and change my directory to the path collected from small.txt.

use Cwd; $BACK= getcwd(); open(LIST,"small.txt") or die "Cant open small.txt"; @arr= <LIST>; for($i = 0; $i < @arr; $i++) { chomp($arr[$i]); print "path read from file = $arr[$i]\n"; #it displays => ms_qu/the/path/written/in/file (fine till here but +it shud go to this place when i use chdir) chdir $arr[$i] or die "$1"; print "After chdir, cwd is = "; system("pwd"); chdir($BACK); print "Now im back to where i started"; system("pwd"); }

Now im not able to change my directory to the path in $arr$i. it shows me the die msg and doesnt change pwd. format of path in small.txt: xyx/abs/def/ijk Although if i write chdir(/abc/def/ijk), it works fine but i want chdir($variable) where value of variable is collected from small.txt PLEASE PLEASE PLEASE HELP ME!!!

Replies are listed 'Best First'.
Re: using chdir($variable_name) to change directory
by Tux (Canon) on Jul 05, 2011 at 11:36 UTC

    Do you return to the previous path before chdir? If not, relative PATH's will fail

    use strict; use warnings; use autodie; use Cwd; my $base = getcwd; open my $lh, "<", "small.txt"; chomp (my @arr= <$lh>); # remove newlines close $lh; foreach my $dir (@arr) { # don't use C-like for when you don't use the + index var chdir $base; # always return to base before the next chdir print "path read from file = $path\n"; # it displays => ms_qu/the/path/written/in/file # Which is a *relative* path name chdir $dir or die "$1"; # WTF is $1? print "After chdir, cwd is = ", getcwd, "\n"; }

    Enjoy, Have FUN! H.Merijn
Re: using chdir($variable_name) to change directory
by roboticus (Chancellor) on Jul 05, 2011 at 11:32 UTC

    shridhar22:

    You show that it displays "ms_qu/the/path/wirtten/in/file". But that path isn't rooted to "/", so chdir will be able to move to that directory if your program executes in a/the directory that leads to that location. Keep in mind that after changing to a directory, the next directory will likely fail for the same reason.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      thanks for the reply sir, i have actully trie every kind of possible path by editing the small.txt file which contains the list of path. i've tried: abc/def/ijk /abc/def/ijk ./abc/def/ijk "abc/def/ijk" all dont work please help thanks

        shridhar22:

        Instead of $1, use $! inside your error messages, and you'll get a more useful error message. If you want a more descriptive error message, add use diagnostics; to the beginning of your program. You may also want to put quotes around the input variable name inside your print statement so you can see if you've properly removed any extra whitespace from the path.

        I also use indentation to clarify my code, so I've put my suggestions in your program to come up with:

        use diagnostics; open(LIST,"small.txt") or die "Can't open small.txt: $!"; @arr= <LIST>; for($i = 0; $i < @arr; $i++) { chomp($arr[$i]); print "path read from file = '$arr[$i]'\n"; #it displays => ms_qu/the/path/written/in/file (fine till here +but it shud go to this place when i use chdir) chdir $arr[$i] or die "Can't change directory: $!"; print "After chdir, cwd is = "; system("pwd"); }

        I haven't tested it, though, so give it a try and see if the error messages give you the information you need to fix it.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

        Dear shridhar22, you need to read and understand Re: using chdir($variable_name) to change directory

        Relative paths are relative

        If you're standing in the living room, and ask me for directions to the bathroom and the bedroom, and I tell you:
        the bathroom is first door on the left
        the bedroom is second door on the right

        And you visit the bathroom successfully, and now wish to go to the bedroom, you can't go to the second door on the right!!

        The bedroom, is the second door on the right, only if your starting position is the living room.

        If your starting position is the bathroom, the second door on the right is not the bedroom!

        To find the bedroom by the path "the second door on the right" you have to go back to the living room.

Re: using chdir($variable_name) to change directory
by Anonymous Monk on Jul 05, 2011 at 11:32 UTC

    Relative paths are relative.

    Read http://en.wikipedia.org/wiki/Path_%28computing%29

    The solution, is to keep track of the starting path.

    One way

    use strict; ## IMPORTANT!!! use warnings; ## IMPORTANT!!! use File::Tools qw/ pushd popd /; use Cwd qw/ cwd /; ... for my $dir( @dirs ){ pushd( $dir ); print cwd(), "\n"; popd( $dir ); }

    Another way

    use strict; ## IMPORTANT!!! use warnings; ## IMPORTANT!!! use autodie; ## IMPORTANT!!! use Cwd qw/ cwd /; our $startingDir = cwd(); ... for my $dir( @dirs ){ chdir $dir; # autodie makes this die on error print cwd(), "\n"; chdir $startingDir; }