File-Remote
view release on metacpan or search on metacpan
# writing a local file still works!
open(LOCAL, ">>/local/file");
print LOCAL "This is a new line.\n";
close(LOCAL);
mkdir("host:/remote/dir", 0755);
unlink("host:/remote/file");
unlink("/local/file"); # still works too!
symlink("host:/remote/src", "host:/remote/dest");
chown("root", "other", "host:/remote/dir/file");
chmod(0600, "host:/remote/dir/file");
#
# Next, the object-oriented style, if you don't want to
# mess with the builtins.
#
use File::Remote;
my $remote = new File::Remote;
# Standard filehandles
# Replace Perl's file methods with File::Remote's
use File::Remote qw(:replace);
open(FILE, ">host:/remote/file") or die "Open failed: $!\n";
print FILE "Hello, world!\n";
close(FILE) or die "Close failed: $!\n";
mkdir("/local/new/dir", "2775");
mkdir("host:/remote/new/dir");
chown("root", "other", "/local/new/dir");
unlink("host:/remote/file");
This is pretty neat; since "File::Remote" will pass calls to local files
straight through to Perl's core functions, you'll be able to do all this
"transparently" and not care about the locations of the files. Plus,
this has the big advantage of making your existing Perl scripts capable
of dealing with remote files without having to rewrite any code.
Because the names for the "File::Remote" methods clash with the Perl
builtins, if you use the function-oriented style with the ":standard"
copy(file1, file2)
Simply copies a file, just like File::Copy's function of the same name.
You can also address it as 'cp' (if you import the :aliases tag).
move(file1, file2)
Moves a file ala File::Copy. You can also address it as 'mv' (if you
import the :aliases tag).
chmod(mode, file) ; chown(owner, group, file)
Change the permissions or the owner of a file.
unlink(file)
Remove a file. You can also address it as 'rm' (if you import the
:aliases tag).
link(file1, file2)
require 5.005;
package File::Remote;
use strict;
use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS $VERSION
%RW_HANDLES %RO_HANDLES %RW_TMPFILES %RO_TMPFILES);
use Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(
rreadfile rwritefile rmkdir rrmdir rrm runlink rcp rcopy rtouch rchown
rchmod rmove rmv rbackup setrsh setrcp settmp ropen rclose rappend rprepend
rsymlink rlink readfile writefile mkdir rmdir rm unlink cp copy touch chown
chmod move mv backup open close append prepend symlink link readlink rreadlink
);
%EXPORT_TAGS = (
files => [qw(ropen rclose rreadfile rwritefile runlink rcopy rtouch rmove
rbackup rappend rprepend rlink rsymlink rreadlink)],
config => [qw(setrsh setrcp settmp)],
dirs => [qw(rmkdir rrmdir)],
perms => [qw(rchown rchmod)],
standard => [qw(ropen rclose rreadfile rwritefile runlink rcopy rtouch rmove
rbackup rappend rprepend setrsh setrcp settmp rmkdir rrmdir
rchown rchmod rsymlink rlink rreadlink)],
aliases => [qw(rrm rmv rcp)],
replace => [qw(open close readfile writefile unlink rm copy cp touch move mv
backup append prepend setrsh setrcp settmp mkdir rmdir chown chmod
symlink link readlink)]
);
# Straight from CPAN
$VERSION = do { my @r=(q$Revision: 1.17 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r };
# Errors
use Carp;
# Need the basic File classes to make it work
# This does NOT fall through to a standard rename command,
# simply because there are too many platforms on which this
# works too differently (Solaris vs. Linux, for ex).
(©(@_) && &unlink(@_)) || return undef;
return 1;
}
#######
# Usage: $remote->chown($file1, $file2);
#
# This chown's files just like UNIX chown.
#######
*rchown = \&chown;
sub chown {
# If remote, subshell it; else, use Perl's chown
# Form of chown is the same as normal chown
my($self, $uid, $gid, $file) = _self_or_default(@_);
croak "Bad usage of chown" unless ($uid && $gid && $file);
my($rhost, $lfile) = _parsepath($file);
if($rhost) {
$self->_system($self->setrsh, $rhost, "'chown $uid $lfile ; chgrp $gid $lfile'") or return undef;
} else {
# Check if we need to resolve stuff
($uid) = getpwnam($uid) if ($uid =~ /[a-zA-Z]/);
($gid) = getgrnam($gid) if ($gid =~ /[a-zA-Z]/);
chown($uid, $gid, $lfile) || return undef;
}
return 1;
}
#######
# Usage: $remote->chmod($mode, $file);
#
# This chmod's files just like UNIX chmod.
#######
*rchmod = \&chmod;
sub chmod {
# Same as chown, really easy
my($self, $mode, $file) = _self_or_default(@_);
croak "Bad usage of chmod" unless ($mode && $file);
my($rhost, $lfile) = _parsepath($file);
if($rhost) {
$self->_system($self->setrsh, $rhost, "'chmod $mode $lfile'") or return undef;
} else {
chmod($mode, $lfile) || return undef;
}
return 1;
# writing a local file still works!
open(LOCAL, ">>/local/file");
print LOCAL "This is a new line.\n";
close(LOCAL);
mkdir("host:/remote/dir", 0755);
unlink("host:/remote/file");
unlink("/local/file"); # still works too!
symlink("host:/remote/src", "host:/remote/dest");
chown("root", "other", "host:/remote/dir/file");
chmod(0600, "host:/remote/dir/file");
#
# Next, the object-oriented style, if you don't want to
# mess with the builtins.
#
use File::Remote;
my $remote = new File::Remote;
# Standard filehandles
# Replace Perl's file methods with File::Remote's
use File::Remote qw(:replace);
open(FILE, ">host:/remote/file") or die "Open failed: $!\n";
print FILE "Hello, world!\n";
close(FILE) or die "Close failed: $!\n";
mkdir("/local/new/dir", "2775");
mkdir("host:/remote/new/dir");
chown("root", "other", "/local/new/dir");
unlink("host:/remote/file");
This is pretty neat; since C<File::Remote> will pass calls to local files
straight through to Perl's core functions, you'll be able to do all this
"transparently" and not care about the locations of the files. Plus,
this has the big advantage of making your existing Perl scripts capable
of dealing with remote files without having to rewrite any code.
Because the names for the C<File::Remote> methods clash with the Perl builtins,
if you use the function-oriented style with the C<:standard> tag there is
=head2 copy(file1, file2)
Simply copies a file, just like File::Copy's function of the same name.
You can also address it as 'cp' (if you import the :aliases tag).
=head2 move(file1, file2)
Moves a file ala File::Copy. You can also address it as 'mv'
(if you import the :aliases tag).
=head2 chmod(mode, file) ; chown(owner, group, file)
Change the permissions or the owner of a file.
=head2 unlink(file)
Remove a file. You can also address it as 'rm' (if you import the :aliases tag).
=head2 link(file1, file2)
Create a hard link between two files. The caveat to this function
( run in 0.424 second using v1.01-cache-2.11-cpan-5511b514fd6 )