File::Remote is a Perl module to read/write/edit remote files transparently.
# Two ways to use File::Remote
# First, the function-based style. Here, we can use the
# special :replace tag to overload Perl builtins!
use File::Remote qw(:replace); # special :replace tag
# read from a remote file
open(REMOTE, "host:/remote/file") or die $!;
print while (< REMOTE >);
# writing a local file still works!
print LOCAL "This is a new line.n";
unlink("/local/file"); # still works too!
chown("root", "other", "host:/remote/dir/file");
# Next, the object-oriented style, if you don't want to
# mess with the builtins.
my $remote = new File::Remote;
# Standard filehandles
$remote->open(FILE, ">>host:/remote/file") or die $!;
print FILE "Here's a line that's added.n";
# Create a new file and change its permissions
# Move files around
$remote->copy("/local/file", "host:/remote/file") or warn $!;
# Read and write whole files
my @file = $remote->readfile("host:/remote/file");
# Backup a file with a suffix
# Use secure connection methods
my $secure = new File::Remote (rsh => "/usr/local/bin/ssh",
rcp => "/usr/local/bin/scp");
This module takes care of dealing with files regardless of whether they're local or remote. It allows you to create and edit files without having to worry about their physical location on the network. If a file passed into a function is of the form host:/path/to/file, then File::Remote uses rsh/rcp (or ssh/scp, depending on how you configure it) to edit the file remotely. Otherwise, it assumes the file is local and passes calls directly through to Perl's core functions.
The nice thing about this module is that you can use it for all your file calls, since it handles both remote and local files transparently. This means you don't have to put a whole bunch of checks for remote files in your code. Plus, if you use the function-oriented interface along with the :replace tag, you can actually redefine the Perl builtin file functions. This means that your existing Perl scripts can automatically handle remote files with no re-engineering(!).