#!/usr/local/bin/perl

###############################################################################
##                                                                           ##
##    Copyright (c) 1995, 1996, 1997, 1998 by Steffen Beyer.                 ##
##    All rights reserved.                                                   ##
##                                                                           ##
##    This program is free software; you can redistribute it                 ##
##    and/or modify it under the same terms as Perl itself.                  ##
##                                                                           ##
###############################################################################

#  Usage: modify perlexpr [files]

$self = $0;
$self =~ s!^.*/!!;

($op = shift) || die "Usage: $self perlexpr [filenames]\n";

if (!@ARGV)
{
    @ARGV = <STDIN>;
    chop(@ARGV);
}

FILE:
while (@ARGV)
{
    $filename = shift;
    next FILE unless (-f $filename);
    unless (open(INPUT, "<$filename"))
    {
        warn "unable to read '$filename': $!\n";
        next FILE;
    }
    unless (rename($filename,"$filename.bak"))
    {
        warn "unable to rename '$filename' to '$filename.bak': $!\n";
        next FILE;
    }
    unless (open(OUTPUT, ">$filename"))
    {
        warn "unable to write '$filename': $!\n";
        next FILE;
    }
    while (<INPUT>)
    {
        $was = $_;
        eval $op;
        die $@ if $@;
        print OUTPUT;
    }
    close(INPUT);
    close(OUTPUT);
}
