sigmaaa has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys,

So I'm opening a .txt file with perl but I want it to check to make sure there is data written to the file and if not to end the program. So far all I can do is get it to run like there is data or run like there isn't any data regardless if there actually is. I've tried just about everything so at this point I've just made two .txt files and left one blank so I can see if wordlist.txt is equal to shit.txt and if they are to kill the program. Still isn't working though, if you could help that would be great!

my $wordlist = 'wordlist.txt'; my $highschool = 'bathroom.txt'; open (LIST, $wordlist) || die "\n Cannot find wordlist.txt \n"; open (lol, $highschool) || die "\n Cannot find bathroom.txt \n"; + print "\n professional site \n"; # After opening the two .txt files I'm checking if they are equal because bathroom.txt is a blank file if ($highschool =~ $LIST) { die "\n No data in wordlist.txt \n"; } else { print "\n All clear \n"};

Replies are listed 'Best First'.
Re: Testing if a .txt file contains any data
by RonW (Parson) on Jan 29, 2015 at 22:22 UTC
    #!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";
Re: Testing if a .txt file contains any data
by sigmaaa (Initiate) on Jan 30, 2015 at 01:33 UTC
    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"}

      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.

Re: Testing if a .txt file contains any data
by gpapkala (Acolyte) on Jan 29, 2015 at 23:41 UTC

      <petpeeve>THERE SHALL NEVER BE AN ELSE AFTER EXIT OR RETURN!!!</petpeeve>

      my $wordlist = 'wordlist.txt'; if (! -s $wordlist) { print "File is empty\n"; exit 0; } # NO ELSE HERE! # your code goes here

      An else is only useful if code is executed after the if/else for BOTH branches. As the first branch is exiting, the "common" code is never executed and the else is only causing unneeded clutter indent and might confuse the reader.


      Enjoy, Have FUN! H.Merijn
      Using this code it will always print "File is loaded." regardless if there is any text in the file or not. I've also tried using -z and still get the same problem.
      if(! -s $wordlist) { print "File is empty\n"; exit 0; } else { print "File is loaded." }