開発環境
- 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(練習問題)2の解答を求めてみる。
コード
#!/usr/bin/env perl use strict; use warnings; use utf8; use v5.18; use Cwd; say '2.'; 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)
$ ./sample2.pl 2. Wide character in print at ./sample2.pl line 10. ディレクトリ名を入力: The current working directory is /Users/{username} . .. .CFUserTextEncoding .DS_Store .FontForge ... .vscode Applications Desktop Documents Downloads Dropbox IdeaProjects Library Movies Music ... $ ./sample2.pl 2. Wide character in print at ./sample2.pl line 10. ディレクトリ名を入力: tmp The current working directory is /Users/{username}/.../perl/初めてのPerl/ch13/tmp . .. a.txt z.txt $ ./sample2.pl 2. Wide character in print at ./sample2.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 $ ./sample2.pl 2. Wide character in print at ./sample2.pl line 10. ディレクトリ名を入力: . The current working directory is /Users/{username}/.../perl/初めてのPerl/ch13 . .. sample1.pl sample2.pl sample2.pl~ tmp tmp.txt $ ./sample2.pl 2. Wide character in print at ./sample2.pl line 10. ディレクトリ名を入力: /opt The current working directory is /opt . .. local $ ./sample2.pl 2. Wide character in print at ./sample2.pl line 10. ディレクトリ名を入力: /opt/local The current working directory is /opt/local . .. Library bin etc include lib libexec man sbin share var www $ ./sample2.pl 2. Wide character in print at ./sample2.pl line 10. ディレクトリ名を入力: abcde cannot chdir to abcde: No such file or directory at ./sample2.pl line 16, <STDIN> line 1. $
0 コメント:
コメントを投稿