Also, you could change your verification code to be somewhat more like this:
#!/usr/bin/perl use strict; use warnings; use v5.10; sub edit_file { my $my_file = shift; my $editor = $ENV{'EDITOR'} || 'vim'; system($editor, $my_file); } sub validate_file { my ($my_file) = @_; my $error; open(MY_FILE, '<', $my_file) or die "Unable to open file [$my_file +]: $!"; while(<MY_FILE>) { chomp; given ($_) { print("Checking [$_]\n"); if (/^[^[:lower:]]/) { # Line does not start with a lowerc +ase letter $error = "$_ does not begin with a lower case letter"; } if (/somethingorother/) { $error = "$_ has somethingorother"; } default { # Line is ok } } if ($error) { $error = "At line [$.]: $error"; close( MY_FILE ); return $error; # Stop parsing the file after the first err +or } } close(MY_FILE); return undef; } my $string = join( "\n", qw(howdy partner goodbye Friend saynora adios + Amigo) ); open(MY_FILE, '>', 'my_file.txt') or die "Can not open my_file.txt: $! +"; print MY_FILE $string; close(MY_FILE); edit_file('my_file.txt'); my $error = 'Nothing went wrong'; while ($error) { $error = validate_file('my_file.txt'); if ($error) { print "Error validating file: $error\n"; print "Hit return to continue: "; <STDIN>; edit_file('my_file.txt'); } } print "File is ok\n";
This uses a given-when construct to allow you to easily add more checks. It also incorporates some of the suggestions mentioned by other monks.
The validation-loop has been moved out of validate_file and the returnvalue of validate_file has been used in a more descriptive matter.
Note that this relies on the truth and falsehood of the empty string (undef is false, empty string is false, nonempty string is true).

In reply to Re: File handles and loops by Neighbour
in thread File handles and loops by dr.jekyllandme

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.