To reproduce the directory structure, use catfilealso abs2relfrom the module File::Spec: catfilecombines fragments into the enter path, and abs2relsets the path relative to some base directory.
File::Copy copy . , sshopen3 , .
:
mkdir -p $dst_dir, , .cat >$dst_file, SENDmd5sum $dst_file, ,
:
use warnings;
use strict;
use File::Basename;
use File::Copy;
use File::Spec::Functions qw/ abs2rel catfile /;
use Net::SSH qw/ sshopen3 /;
my $HOST = "user\@host.com";
my $SRC_BASE = "/tmp/host";
my $SRC_FILE = "$SRC_BASE/a/b/c/file";
my $DST_BASE = "/tmp/dest";
system("md5sum", $SRC_FILE) == 0 or exit 1;
my $dst_file = catfile $DST_BASE, abs2rel $SRC_FILE, $SRC_BASE;
my $dst_dir = dirname $dst_file;
sshopen3 $HOST, *SEND, *RECV, *ERRORS,
"mkdir -p $dst_dir && cat >$dst_file && md5sum $dst_file"
or die "$0: ssh: $!";
binmode SEND;
copy $SRC_FILE, \*SEND or die "$0: copy failed: $!";
close SEND or warn "$0: close: $!";
undef $/;
my $errors = <ERRORS>;
warn $errors if $errors =~ /\S/;
close ERRORS or warn "$0: close: $!";
print <RECV>;
close RECV or warn "$0: close: $!";
:
$ ./create-file
746308829575e17c3331bbcb00c0898b /tmp/host/a/b/c/file
746308829575e17c3331bbcb00c0898b /tmp/dest/a/b/c/file