開発環境
- 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(著)、近藤 嘉雪(翻訳)、嶋田 健志(翻訳)、オライリージャパン)の12章(ファイルテスト)、12.5(練習問題)2の解答を求めてみる。
コード
#!/usr/bin/env perl
## Copyright (C) 2019 by kamimura
use strict;
use warnings;
use utf8;
use v5.18;
# Wide character in print at ... line ...が出力されないようにに指定
# 付録C(Unicode入門)、C.4(ファンシーな文字)、C.4.2(さらにファンシーな文字)より
# WindowsとmacOSのどちらでも標準入力、標準出力、標準エラーについてちゃんと動くように設定
use Encode::Locale;
binmode STDIN, ':encoding(console_in)';
binmode STDOUT, ':encoding(console_out)';
binmode STDERR, ':encoding(console_out)';
say '1.';
my $filename;
my $days = 0;
for (@ARGV) {
unless (-e) {
say $_ . "は存在しない。";
next;
}
if (-M > $days) {
$filename = $_;
$days = -M;
}
}
unless ($days) {
say "Usage: <filename> [filename...]";
exit;
}
say "最後に変更されてからの日数が最も古いファイル名と日数";
say "$filename, ${days}日";
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal)
$ tree . ├── sample1.pl ├── sample2.pl ├── some_file1.txt └── some_file2.txt 0 directories, 4 files $ ./sample2.pl 1. Usage: <filename> [filename...] $ ./sample2.pl sample2.pl 1. 最後に変更されてからの日数が最も古いファイル名と日数 sample2.pl, 0.000659722222222222日 $ ./sample2.pl * 1. 最後に変更されてからの日数が最も古いファイル名と日数 some_file1.txt, 0.742916666666667日 $ ./sample2.pl a.txt 1. a.txtは存在しない。 Usage: <filename> [filename...] $ ./sample2.pl a.txt * 1. a.txtは存在しない。 最後に変更されてからの日数が最も古いファイル名と日数 some_file1.txt, 0.743055555555556日 $ ./sample2.pl * a.txt 1. a.txtは存在しない。 最後に変更されてからの日数が最も古いファイル名と日数 some_file1.txt, 0.743136574074074日 $ ./sample2.pl some_file1.txt a.txt some_file2.txt 1. a.txtは存在しない。 最後に変更されてからの日数が最も古いファイル名と日数 some_file1.txt, 0.743298611111111日 $
0 コメント:
コメントを投稿