2020年5月28日木曜日

開発環境

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

コード

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

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) {
    next if /\A\./;
    say;
}

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

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

0 コメント:

コメントを投稿