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

I liked your post++

To further clarify this and another thread having to do with while loop and $line=<filehande>:

#!usr/bin/perl use strict; use warnings; while (my $line =<DATA> and $line !~ /^\s*$/) { print "do somehing with non blank line\n"; } __END__ Value of <HANDLE> construct can be "0"; test with defined() at C:\User +s\mmtho\Documents\PerlProjects\Monks\test_myVar_inwhile.pl line 5. Global symbol "$line" requires explicit package name (did you forget t +o declare "my $line"?) at C:\Users\mmtho\Documents\PerlProjects\Monks +\test_myVar_inwhile.pl line 5. Execution of C:\Users\mmtho\Documents\PerlProjects\Monks\test_myVar_in +while.pl aborted due to compilation errors.
To fix the first error message,...
#!usr/bin/perl use strict; use warnings; while (defined (my $line =<DATA>) and $line !~ /^\s*$/) { print "do somehing with non blank line\n"; } __END__ Global symbol "$line" requires explicit package name (did you forget t +o declare "my $line"?) at C:\Users\mmtho\Documents\PerlProjects\Monks +\test_myVar_inwhile_ver2.pl line 6. Execution of C:\Users\mmtho\Documents\PerlProjects\Monks\test_myVar_in +while_ver2.pl aborted due to compilation errors.
Back up and fix just this error message...
#!usr/bin/perl use strict; use warnings; my $line; while ($line =<DATA> and $line !~ /^\s*$/) { print "do somehing with non blank line\n"; } __END__ Value of <HANDLE> construct can be "0"; test with defined() at C:\User +s\mmtho\Documents\PerlProjects\Monks\test_myVar_inwhile_ver2.pl line +7. do somehing with non blank line do somehing with non blank line
To make this work completely:
#!usr/bin/perl use strict; use warnings; my $line; while (defined($line =<DATA>) and $line !~ /^\s*$/) { print "do somehing with non blank line\n"; } __END__ do somehing with non blank line do somehing with non blank line do somehing with non blank line do somehing with non blank line

Replies are listed 'Best First'.
Re^4: First attempt at bringing in file for input/output
by hippo (Archbishop) on Oct 24, 2018 at 08:43 UTC
    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
      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 :-)