The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Tie::DirHandle - definitions for tied directory handles


    use Tie::DirHandle;
    [$ref =] tie *FH, "Tie::DirHandle", *DH, "/usr/local/lib";
    while (<FH>){
    (tied *FH)->rewind; # or $ref->rewind;
    untie *FH;


This module provides filehandle-like read access to directory handles. There are not many available methods, because directory handles are read-only. The only methods are TIEHANDLE, READLINE, DESTROY, and rewind.

To tie a filehandle to a directory handle, the syntax is as follows: tie *FILEHANDLE, "Tie::DirHandle", *DIRHANDLE, "/path/to/dir";

The module will open the directory (and croak with an error if not able to do so). When untying the filehandle, the directory is closed.

After a filehandle has been tied to a directory handle, you can read from the directory using the <HANDLE> syntax. This syntax calls READLINE.

To rewind the directory, there are two possible syntaxes: (tied *FH)->rewind; or $ref->rewind;

The second works if you have stored the return value of the tie in a variable $ref. The value of tied *FH and $ref are the same.

The variable $ref (or tied *FH) contains a hash reference, with three keys. $ref->{HANDLE} returns the directory handle it references. $ref->{PATH} and $ref->{DIR} are synonymous, and return the path of the directory.


This ties the specified directory handle to the filehandle given as the first argument to tie(). DIR is the pathname of the directory.


This returns the next value (if called in a scalar context) or the next values (if returned in a list context) of readdir().


This closes the directory.

See Also

Look into perltie, the documentation on the tie() function.


 Jeff Pinyan (CPAN ID: PINYAN)