3

Hi I am a beginner at perl. I came across this statement and read some of the answers in Stackoverflow that said that it checks for non-zero file size. Pls explain.

Saurav Ojha
  • 145
  • 1
  • 1
  • 7

2 Answers2

2

The explanation covers the -s operator, nothing more. It returns the file's size; if it is empty, the numeric zero is interpreted as false in boolean context, while any nonzero number is true.

print "has contents" if -s "/path/to/file";

The ! is a negation, and && is logical "and"; so the entire expression is true when either the file is empty or the other condition is false, or both. (Remember de Morgan's laws; !(x&y) is equivalent to !x | !y.)

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    -s will also return false (specifically, undef) if the file does not exist (or is a broken link) – ysth Sep 11 '16 at 20:43
0

-s returns the size of a file. Zeros in perl are treated as falses (similar to like ). If you put this all together, it's a shorthand way checking that the given file is not empty.

Mureinik
  • 297,002
  • 52
  • 306
  • 350