開発環境
- 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(練習問題)1の解答を求めてみる。
コード
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use v5.18;
use Cwd;
say '1.';
print "ディレクトリ名を入力: ";
chomp(my $dir = <STDIN>);
if ($dir =~/^\s*$/) {
chdir or die "cannot chdir to home directory: $!";
} else {
chdir $dir or die "cannot chdir to $dir: $!";
}
say 'The current working directory is ', getcwd();
for (sort <*>) {
say $_;
}
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal)
$ ./sample1.pl
1.
Wide character in print at ./sample1.pl line 10.
ディレクトリ名を入力:
The current working directory is /Users/{username}
Applications
Desktop
Documents
Downloads
Dropbox
IdeaProjects
Library
...
$ ./sample1.pl
1.
Wide character in print at ./sample1.pl line 10.
ディレクトリ名を入力: tmp
The current working directory is /Users/{username}/.../perl/初めてのPerl/ch13/tmp
$ touch tmp/{a,z}.txt
$ ./sample1.pl
1.
Wide character in print at ./sample1.pl line 10.
ディレクトリ名を入力: tmp
The current working directory is /Users/{username}/.../perl/初めてのPerl/ch13/tmp
a.txt
z.txt
$ ./sample1.pl
1.
Wide character in print at ./sample1.pl line 10.
ディレクトリ名を入力: ..
The current working directory is /Users/{username}/.../perl/初めてのPerl
ch1
ch10
ch11
ch12
ch13
ch2
ch3
ch4
ch5
ch6
ch7
ch8
ch9
$ ./sample1.pl
1.
Wide character in print at ./sample1.pl line 10.
ディレクトリ名を入力: .
The current working directory is /Users/{username}/.../perl/初めてのPerl/ch13
sample1.pl
sample1.pl~
tmp
$ ./sample1.pl
1.
Wide character in print at ./sample1.pl line 10.
ディレクトリ名を入力: /opt
The current working directory is /opt
local
$ ./sample1.pl
1.
Wide character in print at ./sample1.pl line 10.
ディレクトリ名を入力: /opt/local
The current working directory is /opt/local
Library
bin
etc
include
lib
libexec
man
sbin
share
var
www
$ ./sample1.pl
1.
Wide character in print at ./sample1.pl line 10.
ディレクトリ名を入力: abcde
cannot chdir to abcde: No such file or directory at ./sample1.pl line 16, <STDIN> line 1.
$
0 コメント:
コメントを投稿