In your
validate_file sub, you open the file and while looping through the lines, (if something's wrong), close the file and allow it to edit. But what happens after the editing is done? The file is still closed, the while loop will stop (since it can't read from the closed filehandle) it will attempt to close the filehandle again, and return 1. This is not what you intended :)
How about something like this (untested)?
sub validate_file {
my $file_ok = 0;
while (not $file_ok) {
$file_ok = 1; # Let's assume the file has no error
open( MY_FILE, $my_file ) or die "Unable to open file\n";
while( <MY_FILE> ) {
chomp( $_ );
unless ( $_ =~ /^[a-z]/ ) {
close( MY_FILE );
print "Error at line $.\n";
print "$_ does not begin with a lower case letter\n";
print "Hit return to continue: ";
<STDIN>;
&edit_file( $my_file );
$file_ok = 0;
}
}
close( MY_FILE );
}
return 1;
}
The initial
$file_ok-value seems a bit off, but this is to make sure the while loop triggers at least once :)
This method means that
validate_file is called once, and will only return when the file is ok. I'm not sure if that's what you intended. You could move the
while (not $file_ok)-loop out of
validate_file-sub and keep calling the sub while the file is not ok.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.