small fix to perl58delta for MIME::QuotedPrint, from Jarkko
[p5sagit/p5-mst-13.2.git] / pod / perlfaq5.pod
index d3c8c96..3dfc646 100644 (file)
@@ -1,6 +1,6 @@
 =head1 NAME
 
-perlfaq5 - Files and Formats ($Revision: 1.17 $, $Date: 2002/05/23 19:33:50 $)
+perlfaq5 - Files and Formats ($Revision: 1.18 $, $Date: 2002/05/30 07:04:25 $)
 
 =head1 DESCRIPTION
 
@@ -42,7 +42,7 @@ per-filehandle variables.
 Some idioms can handle this in a single statement:
 
     select((select(OUTPUT_HANDLE), $| = 1)[0]);
-    
+
     $| = 1, select $_ for select OUTPUT_HANDLE;
 
 Some modules offer object-oriented access to handles and their
@@ -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.
@@ -162,11 +187,11 @@ You can then pass these references just like any other scalar,
 and use them in the place of named handles.
 
        open my    $fh, $file_name;
-       
+
        open local $fh, $file_name;
-       
+
        print $fh "Hello World!\n";
-       
+
        process_file( $fh );
 
 Before perl5.6, you had to deal with various typeglob idioms
@@ -175,7 +200,7 @@ which you may see in older code.
        open FILE, "> $filename";
        process_typeglob(   *FILE );
        process_reference( \*FILE );
-       
+
        sub process_typeglob  { local *FH = shift; print FH  "Typeglob!" }
        sub process_reference { local $fh = shift; print $fh "Reference!" }