in reply to How do I determine whether a folder is empty?

Short, simple and direct using the glob function ...
sub is_empty { ! ( () = glob $_[0] . '/*' ); }

Note however that this approach employing glob will not include dot-files in the list used to determine whether the directory is empty - Thanks ++broquaint! If this is important, this code could be rewritten using the opendir, readdir and closedir sequence of commands, such as provided by perlplexer above.

Updated as per no_slogan's comments below.

 

perl -le 'print+unpack("N",pack("B32","00000000000000000000001001011011"))'

Replies are listed 'Best First'.
Re: Re: How do I determine whether a folder is empty?
by no_slogan (Deacon) on May 21, 2003 at 14:10 UTC

    This won't work, because glob called in scalar context wants to iterate through all the files in a directory, and won't even look at its argument until it finishes. It will remember what directory it was reading on a previous call to is_empty. You could call glob in list context like this:

    ! (() = glob $_[0] . '/*');