in reply to Testing if a .txt file contains any data

Hey guys, I got it to work, thanks for all the help! Heres the code I used incase you were curious, its kind of a mix between both of yours.
#!perl use warnings; use strict; my $wordlist = 'wordlist.txt'; open (LIST, $wordlist) or die "\n Cannot open $wordlist: $!\n"; my @listofwords; while (<LIST>) { chomp; push @listofwords, $_; } print "There are " . scalar(@listofwords) . " words in $wordlist\n"; if (@listofwords eq 0) { print "\n$wordlist contains nothing! \n"; exit 0 } else { print "\n$wordlist is loaded! \n"}

Replies are listed 'Best First'.
Re^2: Testing if a .txt file contains any data
by locked_user sundialsvc4 (Abbot) on Jan 30, 2015 at 15:08 UTC

    And of course, if you simply wanted to discover as soon as possible that the file is empty, and do not need to retrieve all of the words from it, then you simply try to read something:

    if (<LIST> { ... file is not empty ...

    Testing the file’s size as reported in its directory-entry is not as satisfactory, I think, because the information in a directory entry is not always up-to-date and accurate.   Whereas, if you actually try to read something and succeed in doing so (assuming that there are no file-access privilege issues), this always tells you what you need to know.

    If you do need to grab a list-of-words from the file when it turns out to be nonempty, then the approach that you took would be appropriate.