perl - Determine if directory is the root directory -


I am searching for the name of the directory, if the directory is found, give me the code chdir In other words, otherwise give an error message. For example:

  Use warnings; Strict use; Use cdwd getcwd; Die "base directory not found!" If (! Gaotier ()); All goto {my $ baseDir = '.test'; My $ curdir = getcwd (); While (1) {return 1 if (-d $ baseDir); If (Chdir ("..")) {chdir ($ curdir); Return 0; The problem is that  chdir  is not unsuccessful after going out of the root, so the above program enters an infinite loop if  .test  is not found. 

Of course, when I'm on Linux, I could just test for / , but I want to do it independently in a system. / P>

As @GNUK replied, the original directory portable with its rootdir in the module The representation is the method.

In this way I will write my goto_dir subroutine to note that capital letters are traditionally reserved for global identifiers such as Package :: Names . I think it is best to pass that directory which you are searching for as a parameter in sub-routine Make it more general I have also written that substantine is a chdir to .test < / Code> in the directory if it is found, which you say you want, but not your own solution tries to do this.

Finally, since portability is important, I have a in place of '..' instead of file :: spice- & gt; Updir has used to reference the origin of the current directory.

  #! Use / usr / bin / env perl strict; Use warnings; Cwd use 'cwd'; Use the file: Spec; Goto_dir ('test') or dead 'base directory was not found!'; All goto_rate {my ($ base_dir) = @_; My $ original_dir = cwd; While () {if (-d $ base_dir) {chdir $ base_dir; Return 1; } Elsf (CWD ek file :: spice-> RootDIR) {chdir $ original_dir; Return 0; } And {chdir file :: device-> updir; }}}  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -