開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Strawberry Perl (WindowsのPerlの言語処理系)
- Visual Studio Code (Text Editor)
- Perl 5.28 (プログラミング言語)
初めてのPerl 第7版 (Randal L. Schwartz(著)、brian d foy(著)、Tom Phoenix(著)、近藤 嘉雪(翻訳)、嶋田 健志(翻訳)、オライリージャパン)の13章(ディレクトリ操作)、13.4(練習問題)3の解答を求めてみる。
コード
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use v5.18;
use Cwd;
say '3.';
while (1) {
print "ディレクトリ名を入力: ";
chomp(my $dir = <STDIN>);
last if $dir eq 'q';
$dir = $ENV{HOME} if $dir =~/^\s*$/;
say 'The current working directory is ', getcwd();
opendir my $dh, $dir or die "Cannot open $dir: $!";
for (sort readdir $dh) {
say $_;
}
}
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal)
$ ./sample3.pl
3.
Wide character in print at ./sample3.pl line 11.
ディレクトリ名を入力:
The current working directory is /Users/{username}/.../perl/初めてのPerl/ch13
.
..
.CFUserTextEncoding
.DS_Store
...
.vscode
Applications
Desktop
Documents
Downloads
Dropbox
IdeaProjects
Library
Movies
Music
...
Wide character in print at ./sample3.pl line 11, <STDIN> line 1.
ディレクトリ名を入力: tmp
The current working directory is /Users/{username}/.../perl/初めてのPerl/ch13
.
..
a.txt
z.txt
Wide character in print at ./sample3.pl line 11, <STDIN> line 2.
ディレクトリ名を入力: ..
The current working directory is /Users/{username}/.../perl/初めてのPerl/ch13
.
..
ch1
ch10
ch11
ch12
ch13
ch2
ch3
ch4
ch5
ch6
ch7
ch8
ch9
Wide character in print at ./sample3.pl line 11, <STDIN> line 3.
ディレクトリ名を入力: .
The current working directory is /Users/{username}/.../perl/初めてのPerl/ch13
.
..
output.txt
sample1.pl
sample2.pl
sample3.pl
sample3.pl~
tmp
tmp.txt
Wide character in print at ./sample3.pl line 11, <STDIN> line 4.
ディレクトリ名を入力: /opt
The current working directory is /Users/{username}/.../perl/初めてのPerl/ch13
.
..
local
Wide character in print at ./sample3.pl line 11, <STDIN> line 5.
ディレクトリ名を入力: /opt/local
The current working directory is /Users/{username}/.../perl/初めてのPerl/ch13
.
..
Library
bin
etc
include
lib
libexec
man
sbin
share
var
www
Wide character in print at ./sample3.pl line 11, <STDIN> line 6.
ディレクトリ名を入力: abcde
The current working directory is /Users/{username}/.../perl/初めてのPerl/ch13
Cannot open abcde: No such file or directory at ./sample3.pl line 18, <STDIN> line 7.
$ ./sample3.pl
3.
Wide character in print at ./sample3.pl line 11.
ディレクトリ名を入力: q
$
0 コメント:
コメントを投稿