Domanda

I want to go to a directory, print in some files, then go back to the root directory. So, I did this :

chdir "corpus";
open (OUTFILE, ">para$i") or die "Impossible d'ouvrir le fichier\n";
print OUTFILE $tab[$i];
close OUTFILE;
`cd /`;

But it obviously does not work (the cd / part). How do I go back to the root directory once I moved to the child directory in a Perl script?

Thanks a lot :).

Ok, now I have an other issue with this :

for (my $i=0; $i<$number_para;$i++){
  open  (OUTFILE, ">", "para$i.txt") or die ;
  print OUTFILE $tab[$i];
}

worked fine, but when I added the chdir:

for (my $i=0; $i<$number_para;$i++){
  chdir "corpus"
  open  (OUTFILE, ">", "para$i.txt") or die ;
  print OUTFILE $tab[$i];
  chdir "/"
}

It says "print() on closed filehandle OUTFILE". I don't understand why, since it worked fine before...

È stato utile?

Soluzione

chdir "/" 

will work just fine. Or if you have a set directory in a variable:

chdir $dir or die $!;

Or as Miller says, you can refer to ... However, you should be aware that you do not have to change directory. If you want to open a file in another directory, you can supply the relative path to it:

open (my $out, ">", "Corpus/para$i") or die $!;

Note that you should use three argument open, with explicit mode, and lexical file handle.

Altri suggerimenti

You say root directory, but it looks like you actually just want the parent directory.

To go to the parent directory, use '..';

chdir "..";

Or if you want to be paranoid about cross platform compatability:

use File::Spec;

chdir File::Spec->updir();

To actually go the root directory, just use chdir like you did the first time:

chdir '/';

or once again being paranoid about cross platform compatability:

use File::Spec;

chdir File::Spec->rootdir();

It might be worth pointing out why using cd in backticks didn't work.

Running a command in backticks starts up a completely new shell environment for the command. That new environment starts with a copy of all of the environment variables from the environment that your program is running in. The current directory is one of those environment variables (it's in $ENV{PWD}).

You new environment starts up. The first (and only) thing that it does is to change directory. So the value of $ENV{PWD} in the new environment is changed. But the value in your original environment stays the same as it was.

Your new environment then closes down as its job is done. All of the environment variables that it has are removed from memory. And control returns to the original environment. Which still has the original value for the current directory.

A child environment cannot change the environment variables in its parent environment. So any attempt to change directory using an external program is doomed to failure.

But changing directory using Perl's built-in function chdir works just fine. Because that changes the value in the current environment.

Hope that's helpful.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top