title: Overwriting a file in perl
body: <p>How would I go about overwriting a file with an array using Perl?</p>
  
  <p>My file looks like this:</p>
  
  <pre><code>username1
  comment
  comment
  comment
  username2
  comment
  comment
  username3
  comment
  comment
  comment
  ...
  </code></pre>
  
  <p>What I do is load the lines into an array first. Then go through the array adding lines to a new array. When it finds the line of the user I want to add the comment to it will trigger a flag that makes it add the comment on the next incrementation of the loop. Then after that it just adds the rest of the lines to the array. What I want to do then it use the new array to overwrite the file. That is where I am stuck.</p>
  
  <pre><code>sub AddComment() {
  
    my $username = shift;    # the username to find
    my $comment  = shift;    # the comment to add
    chomp($comment);
    chomp($username);
  
    open my $COMMENTS, '&lt;', "comments.txt" or die "$!";    #open the comments file
    my @lines = &lt;$COMMENTS&gt;;              #Make an array of the files lines
  
    my @NewCommentsFile;                  # make an array to store new file lines
    my $addCommentNow = 0;                # flag to know when to add comment
  
    for my $line (@lines) {
      if ($line eq $username) {           # if this line is the username
        $addCommentNow = 1;               # set flag that next line you add comment
        push(@NewCommentsFile, $line);    # add this line to new array
      }
      elsif ($addCommentNow eq 1) {       # if the flag is 1 then do this
        push(@NewCommentsFile, $comment); # add the comment to the array
        $addCommentNow = 0;               # reset flag
      }
      else {
        push(@NewCommentsFile, $line);       #add line to array
      }
    }
  
    open my $fh, '&gt;', "comments.txt" or die "Cannot open output.txt: $!";
  
    # Loop over the array
    foreach (@NewCommentsFile) {
      print $fh "$_";    # Print each entry in our new array to the file
    }
  
    close $fh;
  }
  </code></pre>