DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Perl Inline File Edit Straight From Perl FAQ

10.13.2009
| 4068 views |
  • submit to reddit
        // description of your code here

#!/usr/bin/perl

use strict;
use warnings;

my $filename = 'some_file.txt';
my $find = 'this';
my $replace = 'that';

{
   local @ARGV = ($filename);
   local $^I = '.bac';#<-- invokes inplace editing
   while( <> ){
      if( s/$find/$replace/ig ) {
         print;
      }
      else {
         print;
      }
   }
}