[svn:perlfaq] r7875 - perlfaq/trunk

[email protected] Wed, 4 Oct 2006 13:39:27 -0700 (PDT)
Newsgroups perl.cvs.perlfaq
Message-ID <[email protected]>
Author: comdog
Date: Wed Oct  4 13:39:26 2006
New Revision: 7875

Modified:
   perlfaq/trunk/perlfaq.pod
   perlfaq/trunk/perlfaq3.pod
   perlfaq/trunk/perlfaq4.pod
   perlfaq/trunk/perlfaq5.pod
   perlfaq/trunk/perlfaq7.pod
   perlfaq/trunk/perlfaq9.pod

Log:
* trailing whitespace cleanups:

* perlfaq5: How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file?
	+ Changed question to "How do I change, delete, or insert a line in a file, or append to the beginning of a file?"
	+ Rewrote the answer. The old answer disappeared because
	it didn't really answer the question. The current answer
	pointing to Tie::File was just as bad. Now I've gone
	through and done it right.
	+ Jim Gibson provided the initial push :)



Modified: perlfaq/trunk/perlfaq.pod
==============================================================================
--- perlfaq/trunk/perlfaq.pod	(original)
+++ perlfaq/trunk/perlfaq.pod	Wed Oct  4 13:39:26 2006
@@ -707,7 +707,7 @@
 
 =item *
 
-How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file?
+How do I change, delete, or insert a line in a file, or append to the beginning of a file?
 
 =item *
 

Modified: perlfaq/trunk/perlfaq3.pod
==============================================================================
--- perlfaq/trunk/perlfaq3.pod	(original)
+++ perlfaq/trunk/perlfaq3.pod	Wed Oct  4 13:39:26 2006
@@ -929,7 +929,7 @@
 L<perlboot>, L<perltoot>, L<perltooc>, and L<perlbot> for reference.
 
 A good book on OO on Perl is the "Object-Oriented Perl"
-by Damian Conway from Manning Publications, or "Intermediate Perl" 
+by Damian Conway from Manning Publications, or "Intermediate Perl"
 by Randal Schwartz, brian d foy, and Tom Phoenix from O'Reilly Media.
 
 =head2 Where can I learn about linking C with Perl?

Modified: perlfaq/trunk/perlfaq4.pod
==============================================================================
--- perlfaq/trunk/perlfaq4.pod	(original)
+++ perlfaq/trunk/perlfaq4.pod	Wed Oct  4 13:39:26 2006
@@ -970,11 +970,11 @@
 appear as part of the data.
 
 	my $line    = ' fred barney   betty   ';
-	my @columns = split /\s+/, $line; 
+	my @columns = split /\s+/, $line;
 		# ( '', 'fred', 'barney', 'betty' );
 
 	my $line    = 'fred||barney||betty';
-	my @columns = split /\|/, $line;  
+	my @columns = split /\|/, $line;
 		# ( 'fred', '', 'barney', '', 'betty' );
 
 If you want to work with comma-separated values, don't do this since
@@ -989,7 +989,7 @@
 
 	my @fields = unpack( $line, "A8 A8 A8 A16 A4" );
 
-Note that spaces in the format argument to C<unpack> do not denote literal 
+Note that spaces in the format argument to C<unpack> do not denote literal
 spaces. If you have space separated data, you may want C<split> instead.
 
 =head2 How do I find the soundex value of a string?
@@ -2087,7 +2087,7 @@
 =head2 How do I handle binary data correctly?
 
 Perl is binary clean, so it can handle binary data just fine.
-On Windows or DOS, however, you have to use C<binmode> for binary 
+On Windows or DOS, however, you have to use C<binmode> for binary
 files to avoid conversions for line endings. In general, you should
 use C<binmode> any time you want to work with binary data.
 

Modified: perlfaq/trunk/perlfaq5.pod
==============================================================================
--- perlfaq/trunk/perlfaq5.pod	(original)
+++ perlfaq/trunk/perlfaq5.pod	Wed Oct  4 13:39:26 2006
@@ -55,11 +55,162 @@
 
 	$sock->autoflush();
 
-=head2 How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file?
+=head2 How do I change, delete, or insert a line in a file, or append to the beginning of a file?
 X<file, editing>
 
-Use the Tie::File module, which is included in the standard
-distribution since Perl 5.8.0.
+(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 "# 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 "# 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. Use C<next> to skip all lines up to line 5, make a change and
+print the result, then stop further processing with C<last>.
+
+	while( <$in> )
+		{
+		next unless $. == 5;
+		s/\b(perl)\b/Perl/g;
+		print $out $_;
+		last;
+		}
+
+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.
+
+	perl -pi -e 's/Fred/Barney/' inFile.txt
+
+To make a backup of C<inFile.txt>, give C<-i> a file extension to add:
+
+	perl -pi.bak -e 's/Fred/Barney/' inFile.txt
+
+To change only the fifth line, you can add a test checking C<$.>, the
+input line number, then only perform the operation when the test
+passes:
+
+	perl -pi -e 's/Fred/Barney/ if $. == 5' inFile.txt
+
+To add lines before a certain line, you can add a line (or lines!)
+before Perl prints C<$_>:
+
+	perl -pi -e 'print "Put before third line\n" if $. == 3' inFile.txt
+
+You can even add a line to the beginning of a file, since the current
+line prints at the end of the loop:
+
+	perl -pi -e 'print "Put before first line\n" if $. == 1' inFile.txt
+
+To insert a line after one already in the file, use the C<-n> switch.
+It's just like C<-p> except that it doesn't print C<$_> at the end of
+the loop, so you have to do that yourself. In this case, print C<$_>
+first, then print the line that you want to add.
+
+	perl -ni -e 'print; print "Put after fifth line\n" if $. == 5' inFile.txt
+
+To delete lines, only print the ones that you want.
+
+	perl -ni -e 'print unless /d/' inFile.txt
+
+		... or ...
+
+	perl -pi -e 'next unless /d/' inFile.txt
 
 =head2 How do I count the number of lines in a file?
 X<file, counting lines> X<lines> X<line>
@@ -237,7 +388,6 @@
 		print {$fhs[$i]} "just another Perl answer, \n";
 		}
 
-
 Before perl5.6, you had to deal with various typeglob idioms
 which you may see in older code.
 

Modified: perlfaq/trunk/perlfaq7.pod
==============================================================================
--- perlfaq/trunk/perlfaq7.pod	(original)
+++ perlfaq/trunk/perlfaq7.pod	Wed Oct  4 13:39:26 2006
@@ -634,8 +634,8 @@
 This is explained in more depth in the L<perlsyn>.  Briefly, there's
 no official case statement, because of the variety of tests possible
 in Perl (numeric comparison, string comparison, glob comparison,
-regex matching, overloaded comparisons, ...).  Larry couldn't decide 
-how best to do this, so he left it out, even though it's been on the 
+regex matching, overloaded comparisons, ...).  Larry couldn't decide
+how best to do this, so he left it out, even though it's been on the
 wish list since perl1.
 
 Starting from Perl 5.8 to get switch and case one can use the

Modified: perlfaq/trunk/perlfaq9.pod
==============================================================================
--- perlfaq/trunk/perlfaq9.pod	(original)
+++ perlfaq/trunk/perlfaq9.pod	Wed Oct  4 13:39:26 2006
@@ -191,7 +191,7 @@
 (contributed by brian d foy)
 
 The CGI.pm module (which comes with Perl) has functions to create
-the HTML form widgets. See the CGI.pm documentation for more 
+the HTML form widgets. See the CGI.pm documentation for more
 examples.
 
 	use CGI qw/:standard/;
@@ -205,7 +205,7 @@
 			-values => [ qw( Llama Alpaca Camel Ram ) ]
 			),
         submit,
- 
+
  		end_form,
         end_html;