I just used this program to make a change to the XEmacs sources, and
I thought people might be interested:
: #-*- Perl -*-
# Copyright (C) 1999 Martin Buchholz
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with XEmacs; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
eval 'exec perl -w -S $0 ${1+"$@"}'
if 0;
use strict;
use FileHandle;
use Carp;
(my $myName = $0) =~ s@.*/@@; my $usage="
Usage: $myName PERLEXPR FILE...
Typical usage is like this:
find . -name '*.[ch]' -print | xargs $0 's/\bCONST\b/const/g'\n";
die $usage unless @ARGV > 1;
my $code = shift;
die $usage if grep (-d || ! -w, @ARGV);
sub SafeOpen {
open ((my $fh = new FileHandle), $_[0]);
confess "Can't open $_[0]: $!" if ! defined $fh;
return $fh;
}
sub WriteStringToFile {
my $fh = SafeOpen ("> $_[0]");
print $fh $_[1] or confess "$_[0]: $!\n";
close $fh or confess "$_[0]: $!\n";
}
foreach my $file (@ARGV) {
my $changed_p = 0;
my $fh = SafeOpen $file;
my $new_contents = "";
while (<$fh>) {
my $save_line = $_;
eval $code;
$changed_p = 1 if $save_line ne $_;
$new_contents .= $_;
}
WriteStringToFile ($file, $new_contents) if $changed_p;
}