http://www.perlmonks.org?node_id=927950


in reply to -s testing for empty file; works on local, but not on remote

It doesn't take three screenfulls of code to demonstrate a problem with -s. Furthermore, it appears that you didn't even show the code that fails! I'm just going to offer a couple of general tips.

First, what exactly does -s return. If it's zero, that means it thinks the file exists and that it's empty. But I suspect it's returning undef, in which case $! contains an error message. Is -s returning undef, and it so, what's $!?

my $size = -s "/home/vcg/Documents/Trial/temp"; die("-s: $!") if !defined($size); if ($size) {

A common mistake is to pass paths with trailing spaces or newlines. Or improper escaping of \. Make sure the variable contains what you think it does.

Replies are listed 'Best First'.
Re^2: -s testing for empty file; works on local, but not on remote
by Jeri (Scribe) on Sep 27, 2011 at 13:12 UTC

    Also, I pasted in the entire code, because, I'm new at perl and I'm just guessing the -s is the problem from what checking I've done with the code. I could be wrong. Maybe someone else could catch my error? That was the idea anyway.

Re^2: -s testing for empty file; works on local, but not on remote
by Jeri (Scribe) on Sep 27, 2011 at 13:26 UTC

    Okay, just checked. There is no error message, and the temp file is definitely created.

      Everything is working smoothly. Thanks for your patience and wisdom

        What was the problem?
Re^2: -s testing for empty file; works on local, but not on remote
by Jeri (Scribe) on Sep 27, 2011 at 18:57 UTC
    I'm not exactly certain, but when I started using -z, the remote program began catching my empty files like the local one.

      There is one difference:

      If you don't check for errors, errors (incl non-existent files) being returned by -s will be mistaken as empty file.

      If you don't check for errors, errors (incl non-existent files) being returned by -z will be mistaken as non-empty file.

        What exactly do you mean by 'error'? if the -s/-z can't figure if the file has anything in it or not?
        I see your point. Thanks.
Re^2: -s testing for empty file; works on local, but not on remote
by Jeri (Scribe) on Sep 27, 2011 at 12:57 UTC

    This is the code that fails! Well it doesn't really fail. The same code that runs on the local, runs slightly differently (because of the -s, it think) in the remote. I'll look into what it's returning. Thanks for the heads up.

      You're saying /home/vcg/Documents/Trial/temp is a path to a remote file? Sorry, it didn't look like it to me.