stat file behaviour

Discussion in 'Perl Misc' started by mike, Jan 29, 2004.

  1. mike

    mike Guest

    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
     
    mike, Jan 29, 2004
    #1
    1. Advertising

  2. In article <>,
    mike <> wrote:
    :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"
    --
    Beware of bugs in the above code; I have only proved it correct,
    not tried it. -- Donald Knuth
     
    Walter Roberson, Jan 29, 2004
    #2
    1. Advertising

  3. mike

    Web Surfer Guest

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

    In article <>,
    says...
    > 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];

    > 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
    >
     
    Web Surfer, Jan 29, 2004
    #3
  4. mike

    Don Stefani Guest

    Re: stat file behaviour ( and just a bit more )

    mike wrote:
    > 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);
     
    Don Stefani, Jan 29, 2004
    #4
  5. Re: stat file behaviour ( and just a bit more )

    On 29 Jan 2004, "Don Stefani" <> wrote in
    comp.lang.perl.misc:

    > 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.
    --
    bclennox AT eos DOT ncsu DOT edu
     
    LaDainian Tomlinson, Jan 29, 2004
    #5
  6. Re: stat file behaviour ( and just a bit more )

    On 29 Jan 2004, "LaDainian Tomlinson" <> wrote in
    comp.lang.perl.misc:

    <snip>

    > 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.
    --
    bclennox AT eos DOT ncsu DOT edu
     
    LaDainian Tomlinson, Jan 29, 2004
    #6
  7. mike

    Don Stefani Guest

    Re: stat file behaviour ( and just a bit more )

    LaDainian Tomlinson wrote:

    > On 29 Jan 2004, "Don Stefani" <> wrote in
    > 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.

    >
    >>#!/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.


    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
     
    Don Stefani, Jan 29, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven T. Hatton
    Replies:
    3
    Views:
    9,713
    Pete Becker
    Jun 2, 2005
  2. Patrick Useldinger

    os.stat('<filename>')[stat.ST_INO] on Windows

    Patrick Useldinger, Feb 27, 2005, in forum: Python
    Replies:
    6
    Views:
    1,178
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Mar 3, 2005
  3. Magesh
    Replies:
    3
    Views:
    444
    Gordon Burditt
    Oct 5, 2007
  4. Rolf Krüger
    Replies:
    2
    Views:
    616
    Ian Collins
    Mar 12, 2008
  5. ruck
    Replies:
    10
    Views:
    1,278
Loading...

Share This Page