http://qs1969.pair.com?node_id=36796

Hen_true has asked for the wisdom of the Perl Monks concerning the following question: (files)

I'm trying to test whether a file has some text in it or not. How do I do that ?

Originally posted as a Categorized Question.

  • Comment on How do I test whether a file has some contents or not?

Replies are listed 'Best First'.
Re: How do I test whether a file has some contents or not?
by ybiC (Prior) on Oct 15, 2000 at 17:13 UTC
    Filetest operators can be found at perlfunc:_X.   Just a day or so ago swiftone showed me the "_" shortcut for performing multiple tests on one file.   Yet another way to look at it.
    if (-s $file && -T _) { # do stuff with text file > 0bytes } if (-s $file && -B _) { # do stuff with binary file > 0bytes }
Re: How do I test whether a file has some contents or not?
by merlyn (Sage) on Oct 15, 2000 at 15:09 UTC
    Hmm, on the off chance that "contents" is not "text", and believing the description more than the title, I'd also add:
    print "it's a non-empty text-ish file" if -s $file and -T $file;
Re: How do I test whether a file has some contents or not?
by Anonymous Monk on Oct 15, 2000 at 12:28 UTC
    if ( -s 'file') { print "file exists and has size > 0\n"; }

    Originally posted as a Categorized Answer.