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

I am new to programming and I took the initiative to learn Perl on my own and I need your wisdom to figuring out the simplest way to check if a file has more than one line? In other words, how to check if the file contains multiple lines. Thank you
  • Comment on How to check if a file has more than one line?

Replies are listed 'Best First'.
Re: How to check if a file has more than one line?
by GrandFather (Saint) on Dec 05, 2014 at 01:05 UTC

    What have you tried? Do you know how to open a file and read from it - see open if not?

    Checking for more than 1 line should be as simple as reading two lines and checking that you got the second (for an empty file you don't get the first either, but for your purpose you can ignore than and just take note of success with the second read).

    When you have some code to show report back for comment.

    Perl is the programming world's equivalent of English
Re: How to check if a file has more than one line?
by toolic (Bishop) on Dec 05, 2014 at 00:46 UTC
Re: How to check if a file has more than one line?
by RonW (Parson) on Dec 05, 2014 at 00:48 UTC

    The special variable $. gives you the current line number for most recently used file handle. So, if you are reading a file, $. will tell you how lines in to the file you have read.

Re: How to check if a file has more than one line?
by james28909 (Deacon) on Dec 05, 2014 at 01:23 UTC
    Heres something I found out, if you assign an array to a variable, it will return the total amount of lines. Then you can do a simple if statement to see if it equals what you expect.
    use strict; use warnings; use File::Slurp; my @array = read_file("filename"); my $lines = @array; if ( $lines == 1 ) { print "total amount of lines are 1"; } else { print "file has $lines lines"; }

      That requires reading the whole file. For small files that's not a problem, but as a general thing you should only read as much at a time as you need. For that reason for loops and other techniques that read all the lines of a file into a list or array should be avoided unless you actually need random access to the whole file.

      Perl is the programming world's equivalent of English
        What about something like this? lol
        use strict; use warnings; open my $file, '<', "filename"; my $count_lines; my @array; while (<$file>) { $count_lines++; push @array, qw($_); last if ( $count_lines == 2 ); } my $lines = @array; print $lines; if ( $lines == 1 ) { print "this file only has 1 line"; } else { print "this file has 2 or more lines"; }
Re: How to check if a file has more than one line?
by karlgoethebier (Abbot) on Dec 05, 2014 at 07:39 UTC

    Take a look at Count number of lines in a text file.

    Update: my 2 cents...

    use strict; use warnings; use IO::All; my $file = shift || die $!; my $io = io $file; print qq(I think it's more than one!\n) if $io->[1];

    Note: "IO::All file used as an array reference becomes tied using Tie::File".

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: How to check if a file has more than one line?
by Laurent_R (Canon) on Dec 05, 2014 at 09:38 UTC
    Perhaps this:
    perl -e 'my $c = <> for 1..2; print "One line\n" if $. == 1;' file.txt