2012年6月22日金曜日

開発環境

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

3.

やり方の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: $!";
}
my @all = glob ".* *";
print "ディレクトリの内容\n";
for(sort @all){
  # next if $_ =~ /^\./;
  print "$_\n";
}

入出力結果(Terminal)

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

ディレクトリの内容
.
..
.CFUserTextEncoding
.DS_Store
.Trash
.Xauthority
.Xcode
.Xmodmap
.appcfg_nag
.bash_history
.bash_profile
.bash_profile.macports-saved_2011-12-06_at_18:40:28
.bash_profile.pysave
.cache
.config
.cpan
.cups
.dropbox
.dvdcss
.emacs.d
.emacs.el
.emacs.el~
.exrc
.fontconfig
.fonts
.fonts.cache-1
.gem
.gnupg
.idlerc
.irb_history
.lesshst
.macports
.mono
.mysql_history
.pearrc
.recently-used
.recently-used.xbel
.rnd
.serverauth.87078
.sh_history
.subversion
.swl
.swm
.swn
.swo
.swp
.viminfo
.vimrc
.wing101-4
Calibre Library
Desktop
Documents
Downloads
Dropbox
Google Drive
KindleGen
Library
Movies
Music
Pictures
Projects
Public
Sites
pear
$ ./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
移動するディレクトリ
../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
$ ./sample.pl
移動するディレクトリ
aaaaa
cannot chdir to aaaaa: No such file or directory at ./sample.pl line 10, <STDIN> line 1.
$

0 コメント:

コメントを投稿