Next Tricks of the Wizards 72

Tied Filehandles

        package Dir;
        sub open {
          my ($package, $dir) = @_;
          opendir my $dh ,$dir or return;
          local *FH;
          tie *FH => 'Dir', $dh, $dir;
          return \*FH;
        }        
        sub TIEHANDLE {
          my ($class, $dirhandle, $dirname) = @_;
          my $self = { DH => $dirhandle, NAME => $dirname };
          bless $self => $class;
        }
        sub READLINE {
          my ($self) = @_;
          readdir($self->{DH});
        }


Next Copyright © 2003 M. J. Dominus