in reply to Re^3: First attempt at bringing in file for input/output
in thread First attempt at bringing in file for input/output

To make this work completely

In such loops I prefer to limit the scope of $line but this might not concern you.

#!usr/bin/perl use strict; use warnings; while (my $line = <DATA>) { last unless $line =~ /\S/; print "Read: $line"; } __DATA__ do a thing with non blank line do a thing with non blank line last one no action on this line nor this

Replies are listed 'Best First'.
Re^5: First attempt at bringing in file for input/output
by Marshall (Canon) on Oct 25, 2018 at 18:53 UTC
    I prefer to put the main condition that "ends the loop" in the conditional.
    For a command line input, that might be detection of /(Q)uit/i or something like that.
    Other secondary conditions can go in the body of the loop.
      but but but … isn't "end of <DATA>" the end-of-loop condition?

      reacting to a /Q(uit)?/i input is more or less just a convenience function, thus secondary :-)

        You are quite correct.
        My examples were quickly contrived with the focus on variable scoping of something in a while loop which has a logical conditional expression.

        For a normal while (my $line = <$fh>){} loop, I would usually have something like "next if blank line" in the body of the loop. The idea expressed in the loop conditional being that all lines will be looked at.

        For a command loop, one that is interactive with the user, I would express the idea that "this loop usually ends when the user types "quit" of some variant of that" within the while() statement. I would prefer that as opposed to "last if ...." within the body of the loop. I didn't frame examples in the context of a command loop from <STDIN> because testing such things takes a few extra steps in my dev environment. My code tends to have either a super fancy GUI or no UI at all, with the later case being far and away the most common. I tend to write "middleware" data processing or system code.