Prev

Perl Notes

Next

Chapter 9.2 Reading the contents of a directory

Once you've opened a directory, you can see which files it contains.

To see what's in a directory:

  1. Open the directory as described in Chapter 9.1
  2. Type @ARRAY, where ARRAY is the name of the array that will contain each of all the files (and directories) from the open directory.

    Or type $scalar, where scalar is the variable that will contain only the next single item in the directory.

  3. Type =.
  4. Type readdir(LABEL), where LABEL corresponds to the label you gave the opened directory (See step 1 on Chapter 9.1)
  5. 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 =~ /^\.+$/);
  }
}

Tips


Prev Home Next