2020年5月29日金曜日

開発環境

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

コード

#!/usr/bin/env perl
use strict;
use warnings;
use v5.28;

say '2.';

print 'ディレクトリ名を入力: ';
chomp(my $dirname = <STDIN>);
if ($dirname =~ /\A\s*\z/) {
    chdir;
} else {
    chdir $dirname or die $!;
}
opendir my $dh, '.' or die $!;

foreach (sort readdir $dh) {
    say;
}

入出力結果(Zsh、PowerShell、Terminal)

% ./sample2.pl 
2.
ディレクトリ名を入力: 
.
..
.CFUserTextEncoding
.DS_Store
…
Applications
Desktop
Documents
Downloads
…
% ./sample2.pl 
2.
ディレクトリ名を入力: .
.
..
sample1.pl
sample2.pl
temp
% ./sample2.pl 
2.
ディレクトリ名を入力: a
No such file or directory at ./sample2.pl line 13, <STDIN> line 1.
% ./sample2.pl 
2.
ディレクトリ名を入力: temp
.
..
temp1.txt
temp2.txt
temp3.txt
temp4.txt
temp5.txt
% 

0 コメント:

コメントを投稿