in reply to Is too little too much? Coding under the microscope...

One thing you should consider before writing short code is "Am I missing possible error codes?". For example, If you get an error from your OS during the readdir() call above and it decides to return nothing (even though something is really there in the directory), can you live with it?

After too many years of writing too much code I'm always a slave to checking for errors on system calls -- even something as basic as "readdir(LOGS) or die". (Obviously this goes for opendir, closedir, etc.)

-- bluto

  • Comment on Re: Is too little too much? Coding under the microscope...

Replies are listed 'Best First'.
Re: Is too little too much? Coding under the microscope...
by Abigail (Deacon) on Jun 28, 2001 at 21:06 UTC
    There is a problem with using readdir(LOGS) or die, and that's it's normal for readdir to return a false value. If you have read all the files in a directory, readdir will return undef (in scalar context, an empty list in list context), signalling you have finished reading. dieing in this situation would really be bad. Also, if you have a file named 0 in your directory, readdir returns a false value.

    -- Abigail

      You are right, of course. In fact, it looks like you'd have to do something like...
      my @a = readdir(FH); die $! if !@a and $!;
      ... to catch an error for readdir (in list context). Now that I think about it, readdir() either fails when you fail to open the filehandle, in which case you should have check opendir(), or you have something really strange happen (i.e. reading from a NFS-ish directory and the server goes away and you have the filesystem soft mounted). So it was a very bad example.

      -- bluto