2012年9月24日月曜日

開発環境

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

その他参考書籍

3.

コード(TextWrangler)

sample.pl

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

print "ディレクトリ名を入力: ";
chomp(my $dir = <STDIN>);
if($dir =~ /^\s*$/){
  chdir or die "can't chdir to your home directory: $!";
} else {
  chdir $dir or die "can't chdir to '$dir': $!";
}

opendir my $dh, '.' or die 'cannot open $dir: $!';
for(sort readdir $dh){
  print "$_\n";
}
closedir $dh;

入出力結果(Terminal)

$ ./sample.pl
ディレクトリ名を入力: sample_folder
.
..
.DS_Store
sample1.bak
test
$ ./sample.pl
ディレクトリ名を入力: 
.
..
.CFUserTextEncoding
.DS_Store
.MakeMKV
.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
.calibre_calibre GUI.lock
.config
.cpan
.cpanplus
.cups
.dropbox
.dvdcss
.emacs.d
.emacs.el
.emacs.el~
.exrc
.fontconfig
.fonts
.fonts.cache-1
.gem
.gnupg
.idlerc
.inputrc
.irb_history
.lesshst
.macports
.mono
.mysql_history
.pearrc
.pyhistory
.pystartup
.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
nltk
nltk_data
pear
$

0 コメント:

コメントを投稿