Prev

Perl Notes

Next

Chapter 9.4 Changing the working directory

The defualt working directory is the one that contains the script itself. If you want to work with another file (or directory) in the same directory, you don't need to specify any path information. However, if you want to work with a file in some other directory, you either have to specify the absolute or relative path that indicates the file's location, or you have to change the working directory to the directory which contains the file, and then just use the filename. If you'll be using several files within a directory, the second option is often quicker.

To change the working directory:

  1. Type chdir(.
  2. Type directory, where directory is the path that leads to the new working directory.
  3. Type ) to complete the fnuction.
  4. Type ; to finsh the line.


chdir("../../logs");

opendir(LOGDIR, ".") || @ErrorMessage;
@files = readdir (LOGDIR);
closedir(LOGDIR);
if (@files) {
  print "You can choose from the following logs:";
  foreach $filename (@files) {
    print $filename unless ($filename =~ /^\.+$/);
  }
}

Tips


Prev Home Next