in reply to more ways to filesize

Once you stat a file (by either using the stat function or the -s thingy) you can get to the stat structure by using the special filehandle '_' (underscore). Using this you avoid more than one stat for each file.
if (-s $file) { my $size = (stat _)[7];

Another thing to keep in mind, although not very important here, is to keep your loops small. the (30000*1024) constant doesn't need to be calculated everytime.

By the same token, you only need to int() your $meg if you are going to print it out, and since you are using printf, you don't even need it there.

Tiago

Replies are listed 'Best First'.
Re: Re: more ways to filesize
by graff (Chancellor) on Aug 06, 2002 at 02:24 UTC
    Well actually, instead of this:
    if (-s $file) { my $size = (stat _)[7]; }
    I tend to prefer this:
    my $size = ( -s $file );
    because the -s returns the file size in bytes. If you want anything else from stat along with the file size, then check "perldoc -f stat" to recall how stat orders things, and do something like this:
    my ($mode,$uid,$size,$modtime) = (stat $file)[2,4,7,9];
    And I agree with Jeffa -- perltidy is a Blessing To Us All, because proper indentation is every programmer's friend.
Re: Re: more ways to filesize
by theorbtwo (Prior) on Aug 06, 2002 at 01:22 UTC

    BTW, slight correction. The multipication is only done once, at compile-time. OTOH, the result doesn't need to be assigned to a variable every time through the loop. In fact, even better, it doesn't need to be assigned to a variable at all; use constant MAXSIZE => 30000*1024 at the top. Even better, 30*1024*1024, which is probably what you meant.


    Confession: It does an Immortal Body good.