From: Mark-Jason Dominus Date: Sun, 25 Aug 2002 12:09:53 +0000 (-0400) Subject: Patch: Put local($^I, @ARGV) = ... trick back into perlfaq5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4750257bd21f5a4355221e101326277c013826ec;p=p5sagit%2Fp5-mst-13.2.git Patch: Put local($^I, @ARGV) = ... trick back into perlfaq5 Message-ID: <20020825160953.15987.qmail@plover.com> p4raw-id: //depot/perl@17800 --- diff --git a/pod/perlfaq5.pod b/pod/perlfaq5.pod index 83e3494..3dfc646 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 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 for more information.