Thanks Charles,
as a begginer I am having some problems to understand this compact code perl
let us write.
In my script I was testing like this if (IsEmptyDir($directory) = = 0)
and, when I included the new code you suggested, it didn work. Then I
removed the = = 0 and started to work.
Could you explain what exactly this part of the line does. grep !
/^\.{1,2}/
Is it a search over the readdir result set ?
Jair
----- Original Message -----
From: "Charles K. Clarkson" <[EMAIL PROTECTED]>
To: "'Jair Santos'" <[EMAIL PROTECTED]>
Sent: Monday, June 16, 2003 1:36 PM
Subject: RE: Is empty directory?
> Jair Santos [mailto:[EMAIL PROTECTED] wrote:
> :
> : I am using the following snippet of code to
> : check if the directory is empty : and it is
> : working.
> :
> : sub IsEmptyDir {
> :
> : my ( $directory) = @_ ;
>
> You don't need that line.
>
> : return undef unless -d $_[0];
> : opendir my $dh, $_[0] or die $!;
> : my $count = () = readdir $dh;
> : return $count - 2;
> : }
>
> As Rob Dixon pointed out, this is less
> than the safest algorithm. Instead of
> decrementing the count, the "." and ".."
> files should be specifically excluded. I
> would recommend this modification.
>
> sub IsEmptyDir {
> return undef unless -d $_[0];
> opendir my $dh, $_[0] or die $!;
> my $count = () = grep ! /^\.{1,2}/, readdir $dh;
> return $count ? 0 : 1;
> }
>
> This also changes the logic to return
> true when $count is 0. Which would allow:
>
> print "Empty Directory\n" if IsEmptyDir( $foo_dir );
>
>
>
> HTH,
>
> Charles K. Clarkson
> --
> Head Bottle Washer,
> Clarkson Energy Homes, Inc.
> Mobile Home Specialists
> 254 968-8328
>
>
>
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]