Forum Moderators: coopster & phranque

Message Too Old, No Replies

Checking if file has content, do I need -e AND -s ?

         

csdude55

6:33 am on Nov 26, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've been checking if a file exists and that the file size is greater than 0 like this:

if ($foo &&
-e '/path/' . $foo &&
-s '/path/' . $foo > 0) {
# do stuff
}

The question is, do I really need to check if $foo exists, then both -e and -s? I feel like the answer is no... if it doesn't exist then the file size would not be greater than 0, so it would fail. Right? My tests all come out as expected, but it's been so long since I've built it I can't remember why I did both.

If the concern is that a file not existing would return "false" or "undefined" that would confuse the match, then I could always do this:

if ((-s '/path/' . $foo // 0) > 0) {
# do stuff
}

Thoughts?

phranque

11:49 am on Nov 26, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you can avoid a stat() call for each file test operator by using a special filehandle named _ to use the results of the last stat() call:
if ($foo &&
-e '/path/' . $foo &&
-s _) {
# do stuff
}

(the "> 0" is superfluous since -s returns true for nonzero file size)

as of Perl 5.9.1 you can stack file test operators thusly:
if ($foo &&
-e -s '/path/' . $foo) {
# do stuff
}

csdude55

8:09 pm on Nov 26, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



you can avoid a stat() call for each file test operator by using a special filehandle named _ to use the results of the last stat() call:

Cool shortcut, thanks!

as of Perl 5.9.1 you can stack file test operators thusly

That would be great, but I'm using 5.16.3. After I finish my rebuild and take down all of the old stuff, though, then I'll probably update, so I'll keep that in mind.

Does your response mean that I need to use both -e AND -s, though?

phranque

11:48 pm on Nov 26, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



as of Perl 5.9.1 you can stack file test operators thusly

That would be great, but I'm using 5.16.3

5.16.3 > 5.9.1 so this feature is available to you.

Does your response mean that I need to use both -e AND -s, though?

your needs depend on what happens here:
{
# do stuff
}

phranque

12:18 am on Nov 27, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Does your response mean that I need to use both -e AND -s, though?

my response does mean that there is no measurable difference in execution cost between this:
-e -s '/path/' . $foo

and this:
-s '/path/' . $foo

csdude55

1:41 am on Nov 27, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



5.16.3 > 5.9.1 so this feature is available to you.

HA!

Now see, in my mind 5.9 = 5.90, which is obviously greater than 5.16. I forget that the Great and Powerful Gods of Programming have decided that 16 is greater than 90... LOL

your needs depend on what happens here:


The live code is (or, was) actually:

if (
$_COOKIE{'PHPSESSID'} &&
-e '/tmp/sess_' . $_COOKIE{'PHPSESSID'} &&
-s '/tmp/sess_' . $_COOKIE{'PHPSESSID'} > 0) {
my $sess = PHP::Session->new($_COOKIE{'PHPSESSID'});
for (keys %{$sess->{_data}}) { $$_ = $sess->get($_); }
}

In this case, if $_COOKIE{'PHPSESSID'} doesn't exist then -s '/tmp/sess_' would be undefined, and not greater than 0. So I think I wouldn't need either of the first two lines, regardless.

But if -e -s has no measurable difference in execution cost then I guess it really doesn't matter.