Willman023 has asked for the wisdom of the Perl Monks concerning the following question:

I have to get a script working by tomorrow afternoon that involves simply getting a file size,I've looked on the net, and I have 4 CGI/Perl books,but all I found was
$size = -s "$filename"; and someone eluded to: $size = (stat("$filename"))[7];
but when I run this, and print out to file it's just blank between the parenthesis. print OUTFILE "($size)"; So is there any other way to get a file's size, and how come its blank and I don't get an error or some indication that something is wrong? Thanks so much!

Edit by dws to add <code> tags

Replies are listed 'Best First'.
Re: Getting file size
by Aristotle (Chancellor) on Sep 24, 2002 at 20:18 UTC

    Check $! to see if any error occured. See perldoc perlvar.

    Also, this isn't shell so you shouldn't be putting the variable name in quotes if you're only using it by itself. -s $filename is fine. If the variable you were trying to pass to -s were a filehandle glob reference, the quotes would actually break your code entirely by turning the reference into a useless string representation of its address.

    my $size = -s $filename; die "Couldn't stat $filename: $!" if not defined $size;

    Makeshifts last the longest.

Re: Getting file size
by broquaint (Abbot) on Sep 24, 2002 at 20:19 UTC
    Have you checked to see if an error has occurred e.g
    defined(my $size = -s $filename) or die "ack: $!"; # or using stat() defined(my $size = [stat $filename]->[7]) or die "ack: $!";
    As this is the most likely cause of your missing filesize.
    HTH

    _________
    broquaint

    update: changed code to check definedness instead of the truthfulness of size ... hmm I'm sure there's a joke in their somewhere

Re: Getting file size
by thelenm (Vicar) on Sep 24, 2002 at 20:17 UTC
    Either one of those should be fine. Try looking at $filename itself to make sure it contains what you think it does. Also, there's no need for the double quotes around $filename when you use it by itself. You can just say $size = -s $filename.

    -- Mike

    --
    just,my${.02}

Re: Getting file size
by vladb (Vicar) on Sep 24, 2002 at 20:17 UTC
    Google, Google, Google! Right here is an answer to your question ;-)

    Or, here's actually a closer match: Finding the FILE SIZE

    _____________________
    # Under Construction
Re: Getting file size
by kabel (Chaplain) on Sep 24, 2002 at 20:25 UTC
    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
      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>
      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.
Re: Getting file size
by MZSanford (Curate) on Sep 24, 2002 at 21:30 UTC
    <sarcasim>
    open(F,$filename) || die "Could not open file '$filename' : $!\n"; while (<F>) { $size += length($_); } close(F);
    </sarcasim>
    from the frivolous to the serious
      open(F, "> $filename") or die "Could not open file '$filename': $!\n"; close(F); $size = 0;

      I'll see your sarcasm and raise you one dose of pedantry :-)

      On Win32, your code would not work reliably unless you add binmode(F) since every CRLF pair will get converted to the single character LF.