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

Hey,

I am a beginner for Perl. Had a basic question. Would appreciate the help.

______________________________________________________

#!/usr/bin/perl # change_directory.pl use warnings; use strict; use Cwd; #list the Timepoint directories my @TP_dir = glob("T*"); print @TP_dir, "\n"; #list the first timepoint directory print $TP_dir[0], "\n"; my $current_dir = getcwd(); print $current_dir; chdir ("$current_dir/$TP_dir[0]") or die "Can't change directory to TP + $!\n"; ________________________________________________________

I am running my perl script in DOS and here is the output:

C:\Documents and Settings\Punita Sharma\Desktop\ExperimentDate>pe tory.pl T1T2 T1 C:/Documents and Settings/Punita Sharma/Desktop/ExperimentDate C:\Documents and Settings\Punita Sharma\Desktop\ExperimentDate>

_________________________________________________

Essentially my problem is that I am not able to get the chdir to work. I'd like to use the mkdir within the T1 and T2 subdirectories.

Thanks

20101027 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: chdir problem
by eyepopslikeamosquito (Archbishop) on Oct 25, 2010 at 10:29 UTC

    I am getting a headache trying to read your post. Please edit your post and change your <end code> to </CODE>.

    I fear you do not understand how chdir works in Perl. After calling chdir, you must call getcwd() again to refresh your $current_dir variable. Please run this program and let us know what it prints:

    # change_directory.pl use warnings; use strict; use Cwd; my $current_dir = getcwd(); #list the Timepoint directories my @TP_dir = glob("T*"); for my $dir (@TP_dir) { my $fulldir = "$current_dir/$dir"; if (-d $fulldir) { chdir($fulldir) or die "error: chdir '$fulldir': $!"; my $currdir = getcwd(); print "cd worked: currdir is now '$currdir'\n"; } else { print "oops: '$fulldir' is not a dir\n"; } }

Re: chdir problem
by ambrus (Abbot) on Oct 25, 2010 at 10:30 UTC

    In perlfaq8, look at the question "I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible?"

Re: chdir problem
by ww (Archbishop) on Oct 25, 2010 at 12:02 UTC
    In addition to your chdir() and getcwd() problems, your use of glob rather than readdir has the potential of bringing you to grief.

    glob("T*") will return all the files (and directories) in your current directory which contain an upper or lower case "T". There's no assurance that the directories will appear at the head of the list (it's sorted ascii-beticly). In other words, $TP_dir[0] may very well contain a file. Cconsider, for example, a directory like this:

    dir T1 T2 _t.txt foo.txt bar.txt ~out.out out.c
    I would expect the file "_t.txt" to appear as $TP_dir[0].

    See eyepopslikeamosquito's response for the method of testing if you must use glob.

    And, as others have said, please read-and-heed-and-use the Monastery's modified html markup standards and/or Markup in the Monastery so we need not go to such trouble to read your post.

Re: chdir problem
by choroba (Cardinal) on Oct 25, 2010 at 10:30 UTC
    Please, use <code> and </code> around your code and output, otherwise no one would be able to help you. Also note that if you change a directory in your Perl script, once the script ends, your shell will be back in the directory you called the script from.
Re: chdir problem
by Marshall (Canon) on Oct 26, 2010 at 00:19 UTC
    I agree with ww. This glob stuff can get you into trouble. There are a number of versions of glob(). There is even a DOS glob, one more variant to take into account when dealing with a MS platform. On one program I was able to coerce a module into using Posix glob, but couldn't make the same thing happen in main() which caused problems for file names with embedded spaces.

    Using readdir and grep{} will work on any version of Perl and on any platform. Important to know is that readdir only returns the file names in the directory - not the full path name. So when you do the tests in grep{} to filter out non-directories, you have to use the full path on the -d test. Since you can use a regex inside the grep{}, you can get exactly what you want even if it is much more complex than the example below.

    In general, I would advise against using cwd to change your working directory. Use full pathnames or relative pathnames from where your program is running. That way, you won't get confused about "where you are", ie where your program is running.And later in the program if you need to do something like delete some file (or perhaps make a sub-directory), you know exactly how to get a valid path name which will allow that. Plus see how much "cleaner" the code looks.

    I use the following "formula"...

    #!/usr/bin/perl use warnings; use strict; #list the Timepoint directories my $dir = "."; opendir (DIR, $dir) || die "couldn't open directory $dir\n"; my @TP_dirs = grep{-d "$dir/$_" && /^T/ }readdir DIR; foreach (@TP_dirs) { print "relative path of TP: $dir/$_\n"; } __END__ in my dir, prints: relative path of TP: ./TESTING