2019年8月31日土曜日

開発環境

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

コード

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

say '7.';
my $is_sym = $ARGV[0] eq '-s';

shift @ARGV if $is_sym;

my ($src, $dst) = @ARGV;

if (-d $dst) {
    $src = basename $src;
    $dst = File::Spec->catfile($dst, $src) if -d $dst;
}
if ($is_sym) {
    symlink $src, $dst or warn "can't symlink $src to $dst: $!";
} else {
    link $src, $dst or warn "can't link $src to $dst: $!";
}

入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal)

$ tree
.
├── a.txt
├── sample7.pl
├── tmp
└── tmp1

2 directories, 3 files
$ ls -l a.txt 
-rw-r--r--  1 {username}  staff  0  8 30 15:02 a.txt
$ ./sample7.pl a.txt b.txt
7.
$ tree
.
├── a.txt
├── b.txt
├── sample7.pl
├── tmp
└── tmp1

2 directories, 4 files
$ ls -l *.txt
-rw-r--r--  2 {username}  staff  0  8 30 15:02 a.txt
-rw-r--r--  2 {username}  staff  0  8 30 15:02 b.txt
$ ./sample7.pl -s a.txt c.txt
7.
$ tree
.
├── a.txt
├── b.txt
├── c.txt -> a.txt
├── sample7.pl
├── tmp
└── tmp1

2 directories, 5 files
$ ls -l *.txt
-rw-r--r--  2 {username}  staff  0  8 30 15:02 a.txt
-rw-r--r--  2 {username}  staff  0  8 30 15:02 b.txt
lrwxr-xr-x  1 {username}  staff  5  8 31 15:05 c.txt@ -> a.txt
$ ./sample7.pl c.txt tmp
7.
$ tree
.
├── a.txt
├── b.txt
├── c.txt -> a.txt
├── sample7.pl
├── tmp
│   └── c.txt
└── tmp1

2 directories, 5 files
$ ls -l tmp/*.txt *.txt
-rw-r--r--  3 {username}  staff  0  8 30 15:02 a.txt
-rw-r--r--  3 {username}  staff  0  8 30 15:02 b.txt
lrwxr-xr-x  1 {username}  staff  5  8 31 15:05 c.txt@ -> a.txt
-rw-r--r--  3 {username}  staff  0  8 30 15:02 tmp/c.txt
$ ./sample7.pl -s c.txt tmp
7.
can't symlink c.txt to tmp/c.txt: File exists at ./sample7.pl line 21.
$ ./sample7.pl c.txt tmp
7.
can't link c.txt to tmp/c.txt: File exists at ./sample7.pl line 23.
$ ./sample7.pl -s tmp/c.txt tmp1
7.
$ tree
.
├── a.txt
├── b.txt
├── c.txt -> a.txt
├── sample7.pl
├── tmp
│   └── c.txt
└── tmp1
    └── c.txt -> c.txt

2 directories, 6 files
$ ls -l tmp/*.txt
-rw-r--r--  3 {username}  staff  0  8 30 15:02 tmp/c.txt
$ ls -l tmp1/*.txt
lrwxr-xr-x  1 {username}  staff  5  8 31 15:08 tmp1/c.txt@ -> c.txt
$ ls -l *.txt
-rw-r--r--  3 {username}  staff  0  8 30 15:02 a.txt
-rw-r--r--  3 {username}  staff  0  8 30 15:02 b.txt
lrwxr-xr-x  1 {username}  staff  5  8 31 15:05 c.txt@ -> a.txt
$ 

0 コメント:

コメントを投稿

関連コンテンツ