2014年6月3日火曜日

開発環境

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

その他参考書籍

13.13(練習問題)7.

コード(BBEdit, Emacs)

sample290_7.pl

#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
binmode STDIN, ':utf8';
binmode STDOUT, ':utf8';
binmode STDERR, ':utf8';
use File::Basename;
use File::Spec;

my $switch = 0;
if ($ARGV[0] eq '-s') {
    $switch = 1;
    shift @ARGV;
}

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

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

if ($switch) {
    symlink $src, $dst or die "Can't symlink '$src' to $dst: $!";
} else {
    link $src, $dst or die "Can't link '$src' to '$dst: $!";
}

入出力結果(Terminal)

$ ls -l temp.txt temp_dir
-rw-r--r--  1 kamimura  staff  0 May 20 12:27 temp.txt

temp_dir:
total 0
drwxr-xr-x   2 kamimura  staff   68 Jun  3 17:32 ./
drwxr-xr-x  14 kamimura  staff  476 Jun  3 17:32 ../
$ ./sample290_7.pl temp.txt temp1.txt
$ ls -l temp*.txt temp_dir
-rw-r--r--  2 kamimura  staff  0 May 20 12:27 temp.txt
-rw-r--r--  2 kamimura  staff  0 May 20 12:27 temp1.txt

temp_dir:
total 0
drwxr-xr-x   2 kamimura  staff   68 Jun  3 17:32 ./
drwxr-xr-x  15 kamimura  staff  510 Jun  3 17:34 ../
$ ./sample290_7.pl -s temp.txt temp2.txt
$ ls -l temp*.txt temp_dir
-rw-r--r--  2 kamimura  staff  0 May 20 12:27 temp.txt
-rw-r--r--  2 kamimura  staff  0 May 20 12:27 temp1.txt
lrwxr-xr-x  1 kamimura  staff  8 Jun  3 17:34 temp2.txt@ -> temp.txt

temp_dir:
total 0
drwxr-xr-x   2 kamimura  staff   68 Jun  3 17:32 ./
drwxr-xr-x  16 kamimura  staff  544 Jun  3 17:34 ../
$ ./sample290_7.pl -s temp.txt temp_dir
$ ls -l temp*.txt temp_dir
-rw-r--r--  2 kamimura  staff  0 May 20 12:27 temp.txt
-rw-r--r--  2 kamimura  staff  0 May 20 12:27 temp1.txt
lrwxr-xr-x  1 kamimura  staff  8 Jun  3 17:34 temp2.txt@ -> temp.txt

temp_dir:
total 8
drwxr-xr-x   3 kamimura  staff  102 Jun  3 17:35 ./
drwxr-xr-x  16 kamimura  staff  544 Jun  3 17:34 ../
lrwxr-xr-x   1 kamimura  staff    8 Jun  3 17:35 temp.txt@ -> temp.txt
$ ./sample290_7.pl -s temp.txt temp_dir/temp3.txt
$ ls -l temp*.txt temp_dir
-rw-r--r--  2 kamimura  staff  0 May 20 12:27 temp.txt
-rw-r--r--  2 kamimura  staff  0 May 20 12:27 temp1.txt
lrwxr-xr-x  1 kamimura  staff  8 Jun  3 17:34 temp2.txt@ -> temp.txt

temp_dir:
total 16
drwxr-xr-x   4 kamimura  staff  136 Jun  3 17:35 ./
drwxr-xr-x  16 kamimura  staff  544 Jun  3 17:34 ../
lrwxr-xr-x   1 kamimura  staff    8 Jun  3 17:35 temp.txt@ -> temp.txt
lrwxr-xr-x   1 kamimura  staff    8 Jun  3 17:35 temp3.txt@ -> temp.txt
$ ./sample290_7.pl  ./temp_dir/temp.txt .
Can't link './temp_dir/temp.txt' to './temp.txt: Too many levels of symbolic links at ./sample290_7.pl line 24.
$ ./sample290_7.pl  ./temp_dir/temp.txt temp4.txt
Can't link './temp_dir/temp.txt' to 'temp4.txt: Too many levels of symbolic links at ./sample290_7.pl line 24.
$ ./sample290_7.pl temp2.txt  temp_dir
$ ls -l temp*.txt temp_dir
-rw-r--r--  3 kamimura  staff  0 May 20 12:27 temp.txt
-rw-r--r--  3 kamimura  staff  0 May 20 12:27 temp1.txt
lrwxr-xr-x  1 kamimura  staff  8 Jun  3 17:34 temp2.txt@ -> temp.txt

temp_dir:
total 16
drwxr-xr-x   5 kamimura  staff  170 Jun  3 17:37 ./
drwxr-xr-x  16 kamimura  staff  544 Jun  3 17:34 ../
lrwxr-xr-x   1 kamimura  staff    8 Jun  3 17:35 temp.txt@ -> temp.txt
-rw-r--r--   3 kamimura  staff    0 May 20 12:27 temp2.txt
lrwxr-xr-x   1 kamimura  staff    8 Jun  3 17:35 temp3.txt@ -> temp.txt
$ ./sample290_7.pl ./temp_dir/temp2.txt  .
Can't link './temp_dir/temp2.txt' to './temp2.txt: File exists at ./sample290_7.pl line 24.
$ ./sample290_7.pl ./temp_dir/temp2.txt  temp3.txt
$ ls -l temp*.txt temp_dir
-rw-r--r--  4 kamimura  staff  0 May 20 12:27 temp.txt
-rw-r--r--  4 kamimura  staff  0 May 20 12:27 temp1.txt
lrwxr-xr-x  1 kamimura  staff  8 Jun  3 17:34 temp2.txt@ -> temp.txt
-rw-r--r--  4 kamimura  staff  0 May 20 12:27 temp3.txt

temp_dir:
total 16
drwxr-xr-x   5 kamimura  staff  170 Jun  3 17:37 ./
drwxr-xr-x  17 kamimura  staff  578 Jun  3 17:39 ../
lrwxr-xr-x   1 kamimura  staff    8 Jun  3 17:35 temp.txt@ -> temp.txt
-rw-r--r--   4 kamimura  staff    0 May 20 12:27 temp2.txt
lrwxr-xr-x   1 kamimura  staff    8 Jun  3 17:35 temp3.txt@ -> temp.txt
$ ./sample290_7.pl -s ./temp_dir/temp2.txt  temp4.txt
$ ls -l temp*.txt temp_dir
-rw-r--r--  4 kamimura  staff   0 May 20 12:27 temp.txt
-rw-r--r--  4 kamimura  staff   0 May 20 12:27 temp1.txt
lrwxr-xr-x  1 kamimura  staff   8 Jun  3 17:34 temp2.txt@ -> temp.txt
-rw-r--r--  4 kamimura  staff   0 May 20 12:27 temp3.txt
lrwxr-xr-x  1 kamimura  staff  20 Jun  3 17:39 temp4.txt@ -> ./temp_dir/temp2.txt

temp_dir:
total 16
drwxr-xr-x   5 kamimura  staff  170 Jun  3 17:37 ./
drwxr-xr-x  18 kamimura  staff  612 Jun  3 17:39 ../
lrwxr-xr-x   1 kamimura  staff    8 Jun  3 17:35 temp.txt@ -> temp.txt
-rw-r--r--   4 kamimura  staff    0 May 20 12:27 temp2.txt
lrwxr-xr-x   1 kamimura  staff    8 Jun  3 17:35 temp3.txt@ -> temp.txt
$ 

0 コメント:

コメントを投稿