Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by Hen_true (Initiate)
on Oct 15, 2000 at 11:15 UTC ( #36796=perlquestion: print w/replies, xml ) Need Help??

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;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://36796]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2021-04-20 04:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?