【Perl】再帰的にファイルを置換

僕が「Perlって楽しいな」と感じたのは、再帰的にファイルを置換するスクリプトを書いたときだった。
こんなかんじに、実行する。

ソースは以下。

use strict;
use warnings;

use File::Find;

my $top_dir;
my $previous;
my $next;

if(@ARGV != 3){
    die "Usage: perl $0 [top_dir] [previous] [next]";
}else{
    $top_dir  = $ARGV[0];
    $previous = $ARGV[1];
    $next     = $ARGV[2];
}

find(\&process, $top_dir);

sub process{
    my $file = $_;
    if(-f $file){
        open my $fh, '<', $file or die "$!";
        my @file = <$fh>;
        close $fh;

        foreach my $line (@file){
            $line =~ s/\b$previous\b/$next/g;
        }

        open my $new_fh, '>', $file or die "$!";
        print $new_fh @file;
        close $new_fh;
    }
}