$! = undef; @a = readdir(D); if ( defined($!) ) { # Process a readdir error }