Prev

Perl Notes

Next

Chapter 9.1 Accessing a directory

Imagine saving each message in a guestbook to an individual file within the Messages directory. It would be very usefull to be able to look at the directory's contents from within the script to see the files it contains. The opendir function lets you do just that.

To access a directory:

  1. Type opendir(LABEL,, where LABEL is the label you will use to reference the directory. Don't forget the comma after the label.
  2. Type "path/directory", where path/directory (with no trailing forward slash) is the path to the desired directory on your server.
  3. Type ).
  4. Type ; to complete the line.


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 =~ /^\.+$/);
  }
}

Don't forget to close the directory after your done.


Prev Home Next