2012年6月20日水曜日

開発環境

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

1.

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

コード(TextWrangler)

#!/usr/bin/env perl
use strict;
use warnings;

print "移動するディレクトリ\n";
chomp(my $dir = <STDIN>);
if($dir =~ /^\s*$/){
  chdir or die "cannot chdir to home directory: $!";
} else {
  chdir $dir or die "cannot chdir to $dir: $!";
}
opendir DH, "." or die "cannot open current directory: $!";
print "ディレクトリの内容\n";
for(sort readdir DH){
  next if $_ =~ /^\./;
  print "$_\n";
}

入出力結果(Terminal)

$ ./sample.pl
移動するディレクトリ

ディレクトリの内容
Calibre Library
Desktop
Documents
Downloads
Dropbox
Google Drive
KindleGen
Library
Movies
Music
Pictures
Projects
Public
Sites
pear
$ ./sample.pl
移動するディレクトリ
.
ディレクトリの内容
barney
betty
coconet.dat
date.txt
fred
gilligan.info
ginger.info
log_file.txt
lovey.info
ls.err
ls.out
maryann.info
monkeyman.info
numbers
oogaboogoo.pm
perl_kamimura_blog
perl_program.bak.bak
perl_program.pl
perl_program1
perl_program1.bak
professor.info
sample
sample.bak
sample.pl
sample.pl.bak
sample_folder
sample_text
sample_text.bak
sample_text.out
sample_text.out.bak
skipper.info
some_file
sortable_hash
test
test.bak
test.out
test_folder
test_link
test_new
thurston.info
tmp
tmp.pl
total_bytes.dat
$

0 コメント:

コメントを投稿