cvsuser 02/09/04 17:48:40
Modified: . perlfaq.pod perlfaq5.pod
Log:
* How can I use Perl's C<-i> option from within a program?
+ new answer from mjd
Revision Changes Path
1.12 +5 -1 perlfaq/perlfaq.pod
Index: perlfaq.pod
===================================================================
RCS file: /cvs/public/perlfaq/perlfaq.pod,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -w -r1.11 -r1.12
--- perlfaq.pod 4 Sep 2002 22:30:10 -0000 1.11
+++ perlfaq.pod 5 Sep 2002 00:48:40 -0000 1.12
@@ -1,6 +1,6 @@
=head1 NAME
-perlfaq - frequently asked questions about Perl ($Date: 2002/09/04 22:30:10 $)
+perlfaq - frequently asked questions about Perl ($Date: 2002/09/05 00:48:40 $)
=head1 DESCRIPTION
@@ -623,6 +623,10 @@
=item *
How do I count the number of lines in a file?
+
+=item *
+
+How can I use Perl's C<-i> option from within a program?
=item *
1.25 +26 -1 perlfaq/perlfaq5.pod
Index: perlfaq5.pod
===================================================================
RCS file: /cvs/public/perlfaq/perlfaq5.pod,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -w -r1.24 -r1.25
--- perlfaq5.pod 4 Sep 2002 22:39:29 -0000 1.24
+++ perlfaq5.pod 5 Sep 2002 00:48:40 -0000 1.25
@@ -1,6 +1,6 @@
=head1 NAME
-perlfaq5 - Files and Formats ($Revision: 1.24 $, $Date: 2002/09/04 22:39:29 $)
+perlfaq5 - Files and Formats ($Revision: 1.25 $, $Date: 2002/09/05 00:48:40 $)
=head1 DESCRIPTION
@@ -80,6 +80,31 @@
close FILE;
This assumes no funny games with newline translations.
+
+=head2 How can I use Perl's C<-i> option from within a program?
+
+C<-i> sets the value of Perl's C<$^I> variable, which in turn affects
+the behavior of C<< <> >>; see L<perlrun> for more details. By
+modifying the appropriate variables directly, you can get the same
+behavior within a larger program. For example:
+
+ # ...
+ {
+ local($^I, @ARGV) = ('.orig', glob("*.c"));
+ while (<>) {
+ if ($. == 1) {
+ print "This line should appear at the top of each file\n";
+ }
+ s/\b(p)earl\b/${1}erl/i; # Correct typos, preserving case
+ print;
+ close ARGV if eof; # Reset $.
+ }
+ }
+ # $^I and @ARGV return to their old values here
+
+This block modifies all the C<.c> files in the current directory,
+leaving a backup of the original data from each file in a new
+C<.c.orig> file.
=head2 How do I make a temporary file name?
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.