Skip to content

Instantly share code, notes, and snippets.

@mamemomonga
Last active December 14, 2015 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamemomonga/5063220 to your computer and use it in GitHub Desktop.
Save mamemomonga/5063220 to your computer and use it in GitHub Desktop.
uuencodeしてターミナル経由でファイルを送信する。送信元はOSX専用。
#!/usr/bin/env perl
#
# uuencodeしてターミナル経由でファイルを送信する。
# $ uutrans.pl hoge.jpg
# を実行するとクリップボードにデータがコピーされるので、送信先で
# $ perl
# を実行してペーストすると、そこにデータが保存される。
#
# 参考: http://blog.livedoor.jp/dankogai/archives/50683970.html
use strict;
use warnings;
use File::Basename;
my @buf;
push @buf,<<'__EOS__';
while(<DATA>) {
if (/^begin\s+([0-7][0-7][0-7])\s+(\S+)/o){
($begin, $perm, $filename) = (1, oct($1), $2);
open $wfh, '>', $filename or die "$filename : $!";
} elsif(/^end$/o){
$begin = 0; close $wfh; chmod $perm, $filename;
}
next unless $begin;
print $wfh unpack("u", $_);
}
__END__
__EOS__
for my $filename (@ARGV){
open my $rfh, '<', $filename or die "$filename : $!";
my $content = eval{ local $/; <$rfh> }; close $rfh;
my $perm = (stat($filename))[2] & 0777;
push @buf,sprintf "begin %o %s\n",$perm,basename($filename);
push @buf,pack("u", $_) while($_ = substr($content, 0, 45, ''));
push @buf,"`\nend\n";
}
push @buf,"__DATA__\n";
open(my $ofh,' | pbcopy'); print $ofh join('',@buf);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment