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

I have a cgi application running on Windows 2003 with IIS6 and using ActivePerl 5.10. It needs to find the size of a specific file before proceeding. The following code:
my $size = -e $filename; print "$filename size is $size\n";
results in the error "Use of unitialized variable $size in concatation..." pointing at the "print" line. If I use the test: if(-s $filename > 0) It never returns true. I've also tried replacing -s with -e just to see if the true/false works and it never returns true. The CGI program creates the file that it's attempting to size, so I don't believe permissions would be an issue. What else could stop these file test from working properly?

Replies are listed 'Best First'.
Re: File tests failing
by lostjimmy (Chaplain) on May 22, 2009 at 18:37 UTC

    I'm sure it's just a typo, but my $size = -e $filename; should be my $size = -s $filename;, although that really has nothing to do with your problem. With your current code, if the file existed, -e would at least return a 1, so you wouldn't get the uninitialized error.

    Are you sure the file exists? Are you sure the file is in the CWD of the script? Have you tried running a test script on the command line to see if that at least works?

    You could also try stat, but i thought the file tests used it internally anyway: my $size = (stat $filename)[7]

Re: File tests failing
by graff (Chancellor) on May 23, 2009 at 00:05 UTC
    You said:
    If I use the test: if(-s $filename > 0) It never returns true.

    Given that fact, one of the following must be true: (a) the file exists but its directory entry indicates that it contains zero bytes, or (b) the file does not exist.

    I've also tried replacing -s with -e just to see if the true/false works and it never returns true.

    This seems to indicate that the path/file named in $filename really does not exist, or can't be found given the current context of the script (e.g. permissions and/or current working directory).

    Have you inspected the value stored in $filename to confirm that it contains the string you were expecting? If the string assigned to $filename is a relative file path, are you certain that the script's current working directory is the correct location to start from in order to follow that path?

    The CGI program creates the file that it's attempting to size...

    So you say, but suppose we ask you to prove that. Did you include "... or die ..." on the statements that opened, wrote and closed that file? (You did close it before trying "-s" on it, didn't you?)

    Have you gone in with a command-line shell, "cd" to the directory in question, and check for the file yourself?

    And in case you're still confused, look again at perldoc -f -X: -e returns 0 or 1 (false or true) according to whether a file exists; -s returns a number greater-than-or-equal-to zero, where zero could mean "non-existent" or "existent but empty".