2020年6月3日水曜日

開発環境

初めてのPerl 第7版 (Randal L. Schwartz(著)brian d foy(著)Tom Phoenix(著)近藤 嘉雪(翻訳)嶋田 健志(翻訳)、オライリージャパン)の13章(ディレクトリ操作)、13.14(練習問題)7の解答を求めてみる。

コード

#!/usr/bin/env perl
use strict;
use warnings;
use v5.28;
use File::Basename;
use File::Spec;

say '7.';

if (@ARGV < 2) {
    die "usage: sample7.pl [-s] src dst";
}
my $s = shift @ARGV if $ARGV[0] eq '-s';
if (@ARGV < 2) {
    die "usage: sample7.pl [-s] src dst";
}
my ($src, $dst) = @ARGV;

if (-d $dst) {
    $dst = File::Spec->catfile($dst, basename $src);
}

if ($s) {
    symlink $src, $dst or warn "symlnk $src, $dst: $!";
} else {
    link $src, $dst or warn "link $src, $dst: $!";
}

入出力結果(Zsh、PowerShell、Terminal)

% ./sample7.pl 
7.
usage: sample7.pl [-s] src dst at ./sample7.pl line 11.
% ./sample7.pl a
7.
usage: sample7.pl [-s] src dst at ./sample7.pl line 11.
% ./sample7.pl -s a
7.
usage: sample7.pl [-s] src dst at ./sample7.pl line 15.
% ./sample7.pl a b
7.
link a, b: No such file or directory at ./sample7.pl line 26.
% ./sample7.pl -s a b
% cat b
cat: b: No such file or directory
% rm b
remove b? y
% mkdir tmp    
% echo hello > temp.txt
% ./sample7.pl temp.txt temp1.txt
7.
% cat temp.txt 
hello
% cat temp1.txt 
hello
% ./sample7.pl temp.txt tmp/temp2.txt
7.
% cat tmp/temp2.txt 
hello
% ./sample7.pl tmp/temp2.txt .
7.
% cat temp2.txt 
hello
% rm temp1.txt temp2.txt tmp/temp2.txt 
remove temp1.txt? y
remove temp2.txt? y
remove tmp/temp2.txt? y
% ./sample7.pl -s temp.txt temp1.txt
7.
% cat temp1.txt 
hello
% ./sample7.pl -s temp.txt tmp
7.
% diff temp.txt tmp/temp.txt 
%              

0 コメント:

コメントを投稿