Harry Putnam wrote:
#!/usr/local/bin/perl -w

@directories = ("./dir1", "./dir2");

for(@directories) {
  opendir(WRK_DIR,"$_");

opendir(WRK_DIR,$_) or die "cannot opendir $_: $!\n";

  chdir $_;

chdir $_ or die "cannot chdir $_: $!\n";
# This fails for the second directory since it chdir to './dir2' when it's in './dir1' Try it from the command line:

  cd ./dir1
  cd ./dir2

Note the second one fails. TO get around this:

  use Cwd;
  my $cwd = cwd;
  chdir $_ or die "cannot chdir $_: $!\n";
...
  chdir $cwd; # Change back at end of the loop.

print "hpdb thisdir=<$_> \n";
  @fnames = grep { /\d/ } readdir(WRK_DIR);
  closedir(WRK_DIR);
  print "$_ has <" . @fnames> . "> files\n";
}

The output puzzles me:

hpdb thisdir=<./dir1> ./dir1 has <57> files -- hpdb thisdir=<./dir2> ./dir2 has <0> files

dir2 is not empty ... its a copy of dir1

Now if I comment out the `chdir' line:
I get the expected results.  (I don't understand why)

hpdb thisdir=<./dir1> ./dir1 has <57> files -- hpdb thisdir=<./dir2> ./dir2 has <57> files
--

Just my 0.00000002 million dollars worth,
   --- Shawn

"Probability is now one. Any problems that are left are your own."
  SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

"This statement is true but unprovable."
  Kurt Godel

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to