in reply to Getting file size

these are the two common ways (-s is more idiomatic).

it seems that you do not use strict;. i suggest you to put it together with use warnings; at the top of your script.

if $size is blank, an error has occured. you can check this by using this for example:
my $filename = "test.pl"; my $size = -s $filename; unless ($size) { die "error getting file size [$filename]: [$!]"; }
then check the error and correct it.

a common error seems to be the permission issue because the cgi script is executed using another user OR you are ín the wrong path (if relative). HTH

Replies are listed 'Best First'.
Re^2: Getting file size
by Aristotle (Chancellor) on Sep 24, 2002 at 20:28 UTC
    That will croak for 0-byte files though. You have to test for definedness, not falseness.

    Makeshifts last the longest.

      yo, that's right. thanks for correction. <badjoke> exactly speaking, it will not croak, it will die :p </badjoke>
Re: Re: Getting file size
by Willman023 (Scribe) on Sep 24, 2002 at 21:25 UTC
    Thanks, I'm kinda new to this environment and the way they have it setup here in the office I don't have access to server logs (They use a NT box that kicks of a batch file to run the perl script, goes by so fast I can barely see the error messages.) This is a Perl script not a CGI file I dunno what difference that makes in this case.

    But I tryed your code with a file that is in the same directory as the script I am running and it dies for some strange reason.

    my $filename = "tools2.pl";
    my $size = -s $filename;
    unless ($size) { die "+++++***** error getting file size [$filename]: [$!] *****+++++";}

    I appreciate all the help I get from this site, everybody replied so darn fast!

      aha. and what is the error message you are getting?
      and be sure to change the last line to
      unless (defined $size) {...}
      , as aristotle correctly suggested.
      it is always a good idea to log important issue to a file which either is specified in the script itself or can be specified via command line. any fatal error is an important issue.