stat file behaviour

M

mike

hi

i have a directory testdir with a single file called test.txt
i tried to stat the file like this

opendir(DIR, "$dir" ) or die "Cannot open directory $dir\n";
my @list = readdir(DIR) or die "Unable to enter directory $dir\n";
foreach ( @list)
{
local $name = $_;
next if ( "$name" eq "." );
next if ( "$name" eq "..") ;
my $size = (stat($name))[7];
print "size is $size\n";
}

the output shows: size is

the size is not shown. Even if i change the stat statement to
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks) = stat($name);

the result is still the same..
what is wrong with the code? I am using 5.005_03
thanks
 
W

Walter Roberson

:i have a directory testdir with a single file called test.txt
:i tried to stat the file like this

use strict;
use warnings;

:eek:pendir(DIR, "$dir" ) or die "Cannot open directory $dir\n";
:my @list = readdir(DIR) or die "Unable to enter directory $dir\n";
:foreach ( @list)
:{
: local $name = $_;

Probably 'my' would be better than 'local' there.

: next if ( "$name" eq "." );

Useless use of double quotes.

next if ( $name eq '.' );

: next if ( "$name" eq "..") ;

Again useless use of double quotes.

: my $size = (stat($name))[7];
: print "size is $size\n";
:}

:the output shows: size is

:the size is not shown.

You aren't chdir'ing into the directory, and you then stat the file
by only its name instead of its path. Try stat'ing on "$dir/$name"
 
W

Web Surfer

[This followup was posted to comp.lang.perl.misc]

hi

i have a directory testdir with a single file called test.txt
i tried to stat the file like this

opendir(DIR, "$dir" ) or die "Cannot open directory $dir\n";
my @list = readdir(DIR) or die "Unable to enter directory $dir\n";
foreach ( @list)
{
local $name = $_;
next if ( "$name" eq "." );
next if ( "$name" eq "..") ;
my $size = (stat($name))[7];

# Note : You need to specify the path to the file
my $size = (stat("$dir/$name"))[7];
 
D

Don Stefani

mike said:
hi

i have a directory testdir with a single file called test.txt
i tried to stat the file like this

The next rookie steps up to the help booth and humbly inquires...

This seemed like a very simple thing to do, and after a bit of checking,
I found that it was.
But I'm posting to ask:

1/ Can it be done with less code? ( 99.999% of the time it can )

2/ My file test for 'only if -f or NOT -d' is not working.
I've tried the test in different place in the script, the last one I'll
show just happens to be the last place I tried it.
The things I've tried I found in 'Learning Perl' and 'google groups'.

A good hint is always appreciated.

Thanks,

Don

##############################

#!/usr/bin/perl -w
use strict;

my ($name, $size);
my $dir = '/usr/local/';

opendir(DIRLIST,$dir) || die "Cannot opendir $dir: $!";

foreach $name (sort readdir(DIRLIST))
{
if (! -d $name) # also tried (-f $name) and got no returned files
$size = (stat($dir.$name))[7];
print "$name : $size\n" if $name =~ /^[^.]+/;
}
}
closedir(DIRLIST);
 
L

LaDainian Tomlinson

This seemed like a very simple thing to do, and after a bit of
checking, I found that it was.
But I'm posting to ask:

1/ Can it be done with less code? ( 99.999% of the time it can )

Sure can, with a slight loss in clarity and one more of those fancy
-X functions.
2/ My file test for 'only if -f or NOT -d' is not working.
I've tried the test in different place in the script, the last one
I'll show just happens to be the last place I tried it.
The things I've tried I found in 'Learning Perl' and 'google
groups'.

I'm surprised you didn't find an answer on Google. It's a fairly
common mistake (in fact, it's the same mistake as made by the OP).
#!/usr/bin/perl -w
use strict;

my ($name, $size);

Declare your variables in the smallest scope in which they are used.
Both $name and $size are only used inside the for loop, so declare
them there.
my $dir = '/usr/local/';

opendir(DIRLIST,$dir) || die "Cannot opendir $dir: $!";

foreach $name (sort readdir(DIRLIST))
{
if (! -d $name) # also tried (-f $name) and got no returned
# files

You got it right on the next line! You needed to append the
directory name up front.
$size = (stat($dir.$name))[7];
print "$name : $size\n" if $name =~ /^[^.]+/;
}
}
closedir(DIRLIST);

A few lines shorter:


my $dir = '/usr/local/bin'; #nothing in /usr/local for me

....

for (map "$dir/$_" => sort readdir DIRLIST){
print((split '/')[-1] . ': ' . -s) if (-f && ! /^\./);
}


Brandan L.
 
L

LaDainian Tomlinson

comp.lang.perl.misc:

for (map "$dir/$_" => sort readdir DIRLIST){
print((split '/')[-1] . ': ' . -s) if (-f && ! /^\./);
}

Of course, this makes sure that your path doesn't start with a dot, and
that's not so helpful. It isn't testing the filename. It looks like
you need the full path in two places and the filename in two places, so
you can't really save any effort by aliasing.

Brandan L.
 
D

Don Stefani

LaDainian said:
comp.lang.perl.misc:
I'm surprised you didn't find an answer on Google. It's a fairly
common mistake (in fact, it's the same mistake as made by the OP).

I thought I had fixed them all, thanks for seeing that.
I should have waited before posting I probably would have found it.
Declare your variables in the smallest scope in which they are used.
Both $name and $size are only used inside the for loop, so declare
them there.

Thanks for the tip
I guess at this point it's short enough, If I make it too slick I won't know what I wrote
in a coule of weeks!

Thanks again,,
Don
 

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,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top