O'Reilly logo

Learning Perl, Second Edition by Randal L. Schwartz, Tom Christiansen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

12.4. Opening and Closing a Directory Handle

The opendir function works like the C and C++ library call of the same name. You give it the name of a new directory handle and a string value denoting the name of the directory to be opened. The return value from opendir is true if the directory can be opened, false otherwise. Here's an example:

opendir(ETC,"/etc") || die "Cannot opendir /etc: $!";

Normally, at this point, we'd go playing with the directory handle ETC, but it's probably nice to know how to close the directory handle first. This is done with closedir , in a similar manner to using close, like so:

closedir(ETC);

Like close, closedir is often unnecessary, since all directory handles are automatically closed before they're reopened or at the end of the program.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required