2020年7月21日火曜日

開発環境

続・初めてのPerl 改訂第2版 (Randal L. Schwartz(著)brian d foy(著)Tom Phoenix(著)伊藤 直也(監修)長尾 高弘(翻訳)、オライリージャパン)の7章(サブルーチンへのリファレンス)、7.9(練習問題)1の解答を求めてみる。

コード

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

use File::Find;
use Time::Local;

my $dir = shift @ARGV;
chdir $dir or die "can't chdir '$dir': $!";

sub gather_mtime_between {
 my ($start, $stop) = @_;
 my @files = ();
 return (sub {
  if (-f $_) {
   my $timestamp = (stat $_)[9];
   if ($start <= $timestamp && $timestamp <= $stop) {
    push @files, $File::Find::name;
   }
  }
 },
 sub { return @files });
}
my $target_dow = 1;        # Sunday is 0, Monday is 1, ...
my @starting_directories = (".");

my $seconds_per_day = 24 * 60 * 60;
my($sec, $min, $hour, $day, $mon, $yr, $dow) = localtime;
my $start = timelocal(0, 0, 0, $day, $mon, $yr);        # midnight today

while ($dow != $target_dow) {
  # Back up one day
  $start -= $seconds_per_day;        # hope no DST! :-)
  if (--$dow < 0) {
    $dow += 7;
  }
}
my $stop = $start + $seconds_per_day;

my($gather, $yield)  = gather_mtime_between($start, $stop);
find($gather, @starting_directories);
my @files = $yield->(  );

for my $file (@files) {
  my $mtime = (stat $file)[9];        # mtime via slice
  my $when = localtime $mtime;
  print "$when: $file\n";
}

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

% ./sample1.pl ~/Documents/
Mon Jul 20 17:07:08 2020: ./…
…
%

0 コメント:

コメントを投稿