2012年6月26日火曜日

開発環境

『初めてのPerl 第5版』(Randal L. Schwartz, Tom Phoenix, brian d foy 共著、近藤 嘉雪 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-427-9) の13章(ディレクトリ操作), 13.14(練習問題)7を解いてみる。

7.

やり方の1つ。(「やり方は何通りもある」(TIMTOWTDI(There Is More Than One Way To Do It.)))

コード(TextWrangler)

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

my $sym = $ARGV[0] eq '-s'? 1 : 0;
shift @ARGV if $sym;
my($old,$new) = @ARGV;
if(-d $new){
  my $basename = basename $old;
  $new = File::Spec->catfile($new,$basename);
}
if($sym){
  symlink $old,$new or die "cannot symlink $old to $new: $!\n";
} else {
  link $old,$new or die "cannot link $old to $new: $!\n";
}

入出力結果(Terminal)

a$ ./sample.pl -s test test_sym
$ ls -l test test_sym
-rw-r--r--  4 kamimura  staff  330  3 18 16:09 test
lrwxr-xr-x  1 kamimura  staff    4  6 26 15:55 test_sym -> test
$ ./sample.pl test test_h
$ ls -l test test_h
-rw-r--r--  5 kamimura  staff  330  3 18 16:09 test
-rw-r--r--  5 kamimura  staff  330  3 18 16:09 test_h
$ ./sample.pl -s test sample
$ ls -l test sample/test
lrwxr-xr-x  1 kamimura  staff    4  6 26 15:55 sample/test -> test
-rw-r--r--  5 kamimura  staff  330  3 18 16:09 test
$

0 コメント:

コメントを投稿