2012年6月21日木曜日

開発環境

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

2.

やり方の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)

Last login: Thu Jun 21 15:55:53 on ttys000
$ ./sample.pl
移動するディレクトリ
.
ディレクトリの内容
.
..
.DS_Store
.out
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
$ ./sample.pl
移動するディレクトリ
../ruby
ディレクトリの内容
.
..
.DS_Store
Ruby
birth_day_helper
ruby_kamimura_blog
sample.rb
tcl8.4.19
tk8.4.19
$ ./sample.pl
移動するディレクトリ
../python
ディレクトリの内容
.
..
.DS_Store
__pycache__
c_program
c_program.c
c_program.dSYM
changer.py
myclient1.py
myclient2.py
myfile.txt
mymod.pyc
mypkg
python_kamimura_blog
python_program.pyc
python_program1.py
recur1.py
recur2.py
sample.py
sample.pyc
sample.txt
sample1.py
tmp.txt
$ 

0 コメント:

コメントを投稿