#!perl -w

###############################################################################
##                                                                           ##
##    Copyright (c) 1995, 1996, 1997, 1998 by Steffen Beyer.                 ##
##    All rights reserved.                                                   ##
##                                                                           ##
##    This piece of software is "Non-Profit-Ware" ("NP-ware").               ##
##                                                                           ##
##    You may use, copy, modify and redistribute it under the terms of the   ##
##    "Non-Profit-License" (NPL).                                            ##
##                                                                           ##
##    Please refer to the file "NONPROFIT" in this distribution for details! ##
##                                                                           ##
###############################################################################

# 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);
}
