What is the difference between if( -f $f ) and unless( -d $f )?

H

hsk

#!/usr/bin/perl -w

use File::Find;

my ( @dirs, $sum, $cnt, $dirname, $base_dir );

$base_dir = "D:\\temp";

find(\&wanted, $base_dir);

sub wanted {

$name = $File::Find::name;


if( -d $name )
{
push( @dirs, $name );
}
}


foreach my $d ( @dirs )
{

opendir( DIR, $d );

# set back to 0 size
$sum = 0;


foreach my $f ( readdir( DIR ) )
{
# set back to 0 size
$size = 0;

# if( -f $f )
unless( -d $f )
{
$size = (stat($f))[7];
$sum = $sum + $size;
}
}
close( DIR );

}

There is no error in case of 'if( -f $f ), but gets some errors for
'unless( -d $f )'. The error comes up in the line of '$sum = $sum + $size'
, saying 'Use of Uninitialized value.'
Is there any of who might give an answer for this?

OS: window 2000, ActivePerl 5.8 version
 
G

Gunnar Hjalmarsson

hsk wrote:

[ Subject:
What is the difference between if( -f $f ) and unless( -d $f )? ]

You should get some hints if you check out "perldoc -f -X".
 
T

Tony Curtis

On 25 Oct 2004 19:07:52 -0700,
# if( -f $f ) unless( -d $f ) { $size = (stat($f))[7]; $sum
= $sum + $size;
There is no error in case of 'if( -f $f ), but gets some
errors for 'unless( -d $f )'. The error comes up in the line
of '$sum = $sum + $size' , saying 'Use of Uninitialized
value.' Is there any of who might give an answer for this?

Well, firstly -f and -d don't cover the entire file type
space, so the 2 tests you have don't trap all possible cases.

Secondly, you don't check whether the stat() succeeded, which
is more likely to be the root cause of your woes.

You can also write

$sum += $size;

to be more succinct.

hth
t
 
A

A. Sinan Unur

(e-mail address removed) (hsk) wrote in
What is the difference between if( -f $f ) and unless( -d $f )?

Please do not only put your question in the subject line.

see perldoc -f -x

Sinan
 
J

Jürgen Exner

hsk wrote:

Subject: What is the difference between if( -f $f ) and unless( -d $f )?

Uhh, I would say sockets, symbolic links, named pipes, devices, just to name
a few more common ones.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top