2020年5月31日日曜日

開発環境

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

コード

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

say '3.';

print 'ディレクトリ名を入力: ';
chomp(my $dirname = <STDIN>);
if ($dirname =~ /\A\s*\z/) {
    chdir;
} else {
    chdir $dirname or die $!;
}
say 'glob関数';
foreach (sort glob '.* *') {
    say;
}
say "山カッコ";
foreach (sort <.* *>) {
    say;
}

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

% ./sample3.pl
3.
ディレクトリ名を入力: 
glob関数
.
..
.CFUserTextEncoding
.DS_Store
…
.zshrc
Applications
Desktop
Documents
Downloads
…
山カッコ
.
..
.CFUserTextEncoding
.DS_Store
…
.zshrc
Applications
Desktop
Documents
Downloads
…
% ./sample3.pl
3.
ディレクトリ名を入力: .
glob関数
.
..
sample1.pl
sample2.pl
sample3.pl
temp
山カッコ
.
..
sample1.pl
sample2.pl
sample3.pl
temp
% ./sample3.pl
3.
ディレクトリ名を入力: tmp
No such file or directory at ./sample3.pl line 13, <STDIN> line 1.
% ./sample3.pl
3.
ディレクトリ名を入力: temp
glob関数
.
..
temp1.txt
temp2.txt
temp3.txt
temp4.txt
temp5.txt
山カッコ
.
..
temp1.txt
temp2.txt
temp3.txt
temp4.txt
temp5.txt
% 

0 コメント:

コメントを投稿