bravismore has asked for the wisdom of the Perl Monks concerning the following question: (files)

How do I test for a blank line when reading from a file?

Originally posted as a Categorized Question.

  • Comment on How do I test for a blank line when reading from a file?

Replies are listed 'Best First'.
Re: How do I test for a blank line when reading from a file?
by merlyn (Sage) on Oct 11, 2000 at 20:54 UTC
    print "it's blank" unless /\S/;
    (This assumes that a line consisting of only whitespace characters is considered blank.)
Re: How do I test for a blank line when reading from a file?
by thanos1983 (Parson) on May 29, 2014 at 21:01 UTC

    Extending runrig's answer to cover the case where "blank" could include whitespace characters (as merlyn's answer already does, negatively): Use /^\s*$/
    As in:

    while (<>) { print "blank line detected\n" if /^\s*$/; }

Re: How do I test for a blank line when reading from a file?
by runrig (Abbot) on Oct 11, 2000 at 20:52 UTC
    If by "blank" you mean "empty", then /^$/, as in:
    while (<>) { print "blank line detected\n" if /^$/; }
Re: How do I test for a blank line when reading from a file?
by abhishek_akj (Initiate) on Aug 26, 2009 at 10:28 UTC
    while (<>) { print __LINE__ " | This line is blank\n" if /^$/; }
    A little friendlier output

    Originally posted as a Categorized Answer.