2012年9月28日金曜日

開発環境

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

その他参考書籍

7.

コード(TextWrangler)

sample.pl

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

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

my($old, $new) = @ARGV;

die unless -e $old;

$new = catfile($new, basename $old) if -d $new;

if($symlink){
  symlink $old, $new or warn "can't symlink $old to $new: $!";
} else {
  link $old, $new or warn "can't link $old to $new: $!";
}

入出力結果(Terminal)

$ ls -l
total 16
-rwxr-xr-x@ 1 kamimura  staff  477  9 28 15:58 sample.pl
-r--r--r--  1 kamimura  staff   28  9 27 14:38 some_file
$ ./sample.pl some_file some_file1
$ ls -l
total 24
-rwxr-xr-x@ 1 kamimura  staff  477  9 28 15:58 sample.pl
-r--r--r--  2 kamimura  staff   28  9 27 14:38 some_file
-r--r--r--  2 kamimura  staff   28  9 27 14:38 some_file1
$ ./sample.pl -s some_file some_file2
$ ls -l
total 32
-rwxr-xr-x@ 1 kamimura  staff  477  9 28 15:58 sample.pl
-r--r--r--  2 kamimura  staff   28  9 27 14:38 some_file
-r--r--r--  2 kamimura  staff   28  9 27 14:38 some_file1
lrwxr-xr-x  1 kamimura  staff    9  9 28 16:00 some_file2 -> some_file
$

0 コメント:

コメントを投稿