summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark-Jason Dominus <mjd@plover.com>2002-08-25 08:09:53 -0400
committerhv <hv@crypt.org>2002-08-29 12:08:59 +0000
commit4750257bd21f5a4355221e101326277c013826ec (patch)
tree70f8eaf77d9f7851b81a21129a60f2b96bfd1422
parenta3e5cfd4c30bff9d1d50fd3f8db258b33693d62c (diff)
downloadperl-4750257bd21f5a4355221e101326277c013826ec.tar.gz
Patch: Put local($^I, @ARGV) = ... trick back into perlfaq5
Message-ID: <20020825160953.15987.qmail@plover.com> p4raw-id: //depot/perl@17800
-rw-r--r--pod/perlfaq5.pod25
1 files changed, 25 insertions, 0 deletions
diff --git a/pod/perlfaq5.pod b/pod/perlfaq5.pod
index 83e34942c0..3dfc646fd7 100644
--- a/pod/perlfaq5.pod
+++ b/pod/perlfaq5.pod
@@ -81,6 +81,31 @@ proper text file, so this may report one fewer line than you expect.
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?
Use the File::Temp module, see L<File::Temp> for more information.