2014年4月30日水曜日

開発環境

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

その他参考書籍

13.13(練習問題)2.

コード(BBEdit, Emacs)

sample289_2.pl

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

print 'ディレクトリ名を入力: ';
chomp (my $dir_to_process = <STDIN>);

if ($dir_to_process =~ /\A\s*\Z/) {
    chdir or die "cannot chdir to home directory: $!";
} else {
    chdir $dir_to_process or die "cannot chdir to $dir_to_process: $!";
}

opendir my $dh, '.' or die "Cannot open current directory: $!";

for (sort readdir $dh) {
    # next if $_ =~ /\A\./;
    say $_;
}

入出力結果(Terminal)

$ ./sample289_2.pl 
ディレクトリ名を入力: 
.
..
.CFUserTextEncoding
.DS_Store
.Trash
.bash_history
.dropbox
.dropbox-master
.emacs
.emacs.d
.emacs.el~
.emacs_bash-4.2
.emacs~
.profile
.python_history
.ssh
Desktop
Documents
Downloads
Dropbox
Library
Movies
Music
Pictures
Public
Sites
$ ./sample289_2.pl 
ディレクトリ名を入力: .
.
..
sample289_1.pl
sample289_1.pl~
sample289_2.pl

0 コメント:

コメントを投稿