[svn:perlfaq] r8579 - perlfaq/trunk
[email protected] Sun, 14 Jan 2007 10:28:14 -0800 (PST)
| Newsgroups | perl.cvs.perlfaq |
|---|---|
| Message-ID | <[email protected]> |
Author: comdog
Date: Sun Jan 14 10:28:09 2007
New Revision: 8579
Modified:
perlfaq/trunk/perlfaq5.pod
Log:
* perlfaq5: How do I change, delete, or insert a line in a file, or append to the beginning of a file?
+ Fix example for changing a single line to show handling
the lines before and after it.
Modified: perlfaq/trunk/perlfaq5.pod
==============================================================================
--- perlfaq/trunk/perlfaq5.pod (original)
+++ perlfaq/trunk/perlfaq5.pod Sun Jan 14 10:28:09 2007
@@ -117,17 +117,25 @@
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>.
+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> )
+ while( <$in> ) # print the lines before the change
{
- next unless $. == 5;
- s/\b(perl)\b/Perl/g;
print $out $_;
- last;
+ 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__>.