Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- PERL 4.0 Reference Guide - Norton Guide http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]

     -iextension
          specifies that files processed by the <> construct  are
          to  be  edited  in-place.  It does this by renaming the
          input file, opening the output file by the  same  name,
          and selecting that output file as the default for print
          statements.  The extension, if supplied,  is  added  to
          the  name of the old file to make a backup copy.  If no
          extension is supplied, no backup is made.  Saying "perl
          -p  -i.bak  -e "s/foo/bar/;" ... " is the same as using
          the script:

               #!/usr/bin/perl -pi.bak
               s/foo/bar/;

          which is equivalent to

               #!/usr/bin/perl
               while (<>) {
                    if ($ARGV ne $oldargv) {
                         rename($ARGV, $ARGV . '.bak');
                         open(ARGVOUT, ">$ARGV");
                         select(ARGVOUT);
                         $oldargv = $ARGV;
                    }
                    s/foo/bar/;
               }
               continue {
                   print;     # this prints to original filename
               }
               select(STDOUT);

          except that the -i form doesn't need to  compare  $ARGV
          to  $oldargv to know when the filename has changed.  It
          does, however, use ARGVOUT for the selected filehandle.
          Note  that  STDOUT  is  restored  as the default output
          filehandle after the loop.

          You can use eof to locate the end of each  input  file,
          in  case you want to append to each file, or reset line
          numbering (see example under eof).

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson