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

Try using -s from http://perldoc.perl.org/functions/-X.html. It will give you the size of the file.
my $wordlist = 'wordlist.txt'; if(! -s $wordlist) { print "File is empty\n"; exit 0; } else { # your code goes here }

Replies are listed 'Best First'.
Re^2: Testing if a .txt file contains any data
by Tux (Canon) on Jan 30, 2015 at 07:01 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
Re^2: Testing if a .txt file contains any data
by sigmaaa (Initiate) on Jan 30, 2015 at 01:10 UTC
    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." }