2012年4月23日月曜日

開発環境

『続・初めてのPerl 改訂版』(Randal L. Schwartz, brian d foy, Tom Phoenix 著、伊藤 直也田中 慎司吉川 英興 監訳、株式会社ロングテール/長尾 高弘 訳、オライリー・ジャパン、2006年、ISBN4-87311-305-9) の7章(サブルーチンへのリファレンス), 7.8(練習問題)1を解いてみる。

1.

やり方の1つ。(「やり方は何通りもある」(TIMTOWTDI(There Is More Than One Way To Do It.)))

コード(TextWrangler)

#!/usr/bin/env perl
use strict;
use warnings;
use File::Find;
use Time::Local;

sub gather_mtime_between{
  my($start, $stop) = @_;
  my @files;
  my $gather = sub{
    my $timestamp = (stat $_)[9];
    push @files, $File::Find::name if 
      $timestamp >= $start and 
      $timestamp < $stop;
  };
  my $yield = sub {
    @files;
  };
  ($gather, $yield);
}

my $target_dow = 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);
while ($dow != $target_dow){
  $start -= $seconds_per_day;
  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];
  my $when = localtime $mtime;
  print "$when: $file\n";
}

入出力結果(Terminal)

$ ./sample.pl
Mon Apr 23 15:54:08 2012: .
Mon Apr 23 16:06:42 2012: ./perl_kamimura_blog
Mon Apr 23 16:12:58 2012: ./sample.pl
$

0 コメント:

コメントを投稿