Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hello, I am trying to write a program that will allow a user to edit a file using editor. Once the user quits the editor, I am going to parse the file to make sure that the expected format of this file has been followed. If not, my program will print out an error message for the line number and reopen the file to allow the user to correct their mistakes. This will go on until they have correctly conformed to the expected format. For example, my rule is that all lines should begin with a lowercase letter. My code so far does this, but I know that I am messing up on dealing with file handles. I am using a loop to parse each line of my file to make sure the string is valid. If it is not, I close my current file handle and allow another edit to occur. Below is my code so far:
my $string = join( "\n", qw(howdy partner goodbye Friend saynora adios + Amigo) ); open(MY_FILE, '>my_file.txt'); print MY_FILE $string; close(MY_FILE); &edit_file('my_file.txt'); sub edit_file { my $my_file = shift; my $editor = $ENV{'editor'}; system('vim', $my_file); my $validate = &validate_file($my_file); print "File is ok\n" if $validate; } sub validate_file { my $my_file = shift; 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 ); } } close( MY_FILE ); return 1; }
I am not sure if my approach is correct. If anyone can give me pointers how to improve my code, it would be great. Thank you.

In reply to 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-25 07:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found