+ # now should alternate . and +
+ while( 1 )
+ {
+ sleep 1;
+ print STDOUT ".";
+ print STDERR "+";
+ print STDOUT "\n" unless ++$count % 25;
+ }
+
+Besides the C<$|> special variable, you can use C<binmode> to give
+your filehandle a C<:unix> layer, which is unbuffered:
+
+ binmode( STDOUT, ":unix" );
+
+ while( 1 ) {
+ sleep 1;
+ print ".";
+ print "\n" unless ++$count % 50;
+ }
+
+For more information on output layers, see the entries for C<binmode>
+and C<open> in L<perlfunc>, and the C<PerlIO> module documentation.
+
+If you are using C<IO::Handle> or one of its subclasses, you can
+call the C<autoflush> method to change the settings of the
+filehandle:
+
+ use IO::Handle;
+ open my( $io_fh ), ">", "output.txt";
+ $io_fh->autoflush(1);
+
+The C<IO::Handle> objects also have a C<flush> method. You can flush
+the buffer any time you want without auto-buffering
+
+ $io_fh->flush;
+
+=head2 How do I change, delete, or insert a line in a file, or append to the beginning of a file?
+X<file, editing>
+
+(contributed by brian d foy)
+
+The basic idea of inserting, changing, or deleting a line from a text
+file involves reading and printing the file to the point you want to
+make the change, making the change, then reading and printing the rest
+of the file. Perl doesn't provide random access to lines (especially
+since the record input separator, C<$/>, is mutable), although modules
+such as C<Tie::File> can fake it.
+
+A Perl program to do these tasks takes the basic form of opening a
+file, printing its lines, then closing the file:
+
+ open my $in, '<', $file or die "Can't read old file: $!";
+ open my $out, '>', "$file.new" or die "Can't write new file: $!";
+
+ while( <$in> )
+ {
+ print $out $_;
+ }
+
+ close $out;
+
+Within that basic form, add the parts that you need to insert, change,
+or delete lines.
+
+To prepend lines to the beginning, print those lines before you enter
+the loop that prints the existing lines.
+
+ open my $in, '<', $file or die "Can't read old file: $!";
+ open my $out, '>', "$file.new" or die "Can't write new file: $!";
+
+ print $out "# Add this line to the top\n"; # <--- HERE'S THE MAGIC
+
+ while( <$in> )
+ {
+ print $out $_;
+ }
+
+ close $out;
+
+To change existing lines, insert the code to modify the lines inside
+the C<while> loop. In this case, the code finds all lowercased
+versions of "perl" and uppercases them. The happens for every line, so
+be sure that you're supposed to do that on every line!
+
+ open my $in, '<', $file or die "Can't read old file: $!";
+ open my $out, '>', "$file.new" or die "Can't write new file: $!";
+
+ print $out "# Add this line to the top\n";
+
+ while( <$in> )
+ {
+ s/\b(perl)\b/Perl/g;
+ print $out $_;
+ }
+
+ close $out;
+
+To change only a particular line, the input line number, C<$.>, is
+useful. First read and print the lines up to the one you want to
+change. Next, read the single line you want to change, change it, and
+print it. After that, read the rest of the lines and print those:
+
+ while( <$in> ) # print the lines before the change
+ {
+ print $out $_;
+ last if $. == 4; # line number before change
+ }
+
+ my $line = <$in>;
+ $line =~ s/\b(perl)\b/Perl/g;
+ print $out $line;
+
+ while( <$in> ) # print the rest of the lines
+ {
+ print $out $_;
+ }
+
+To skip lines, use the looping controls. The C<next> in this example
+skips comment lines, and the C<last> stops all processing once it
+encounters either C<__END__> or C<__DATA__>.
+
+ while( <$in> )
+ {
+ next if /^\s+#/; # skip comment lines
+ last if /^__(END|DATA)__$/; # stop at end of code marker
+ print $out $_;
+ }
+
+Do the same sort of thing to delete a particular line by using C<next>
+to skip the lines you don't want to show up in the output. This
+example skips every fifth line:
+
+ while( <$in> )
+ {
+ next unless $. % 5;
+ print $out $_;
+ }
+
+If, for some odd reason, you really want to see the whole file at once
+rather than processing line-by-line, you can slurp it in (as long as
+you can fit the whole thing in memory!):
+
+ open my $in, '<', $file or die "Can't read old file: $!"
+ open my $out, '>', "$file.new" or die "Can't write new file: $!";
+
+ my @lines = do { local $/; <$in> }; # slurp!
+
+ # do your magic here
+
+ print $out @lines;
+
+Modules such as C<File::Slurp> and C<Tie::File> can help with that
+too. If you can, however, avoid reading the entire file at once. Perl
+won't give that memory back to the operating system until the process
+finishes.
+
+You can also use Perl one-liners to modify a file in-place. The
+following changes all 'Fred' to 'Barney' in F<inFile.txt>, overwriting
+the file with the new contents. With the C<-p> switch, Perl wraps a
+C<while> loop around the code you specify with C<-e>, and C<-i> turns
+on in-place editing. The current line is in C<$_>. With C<-p>, Perl
+automatically prints the value of C<$_> at the end of the loop. See
+L<perlrun> for more details.