加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Exercise for Chapter 7 of "Intermediate Perl&#34

发布时间:2020-12-15 20:59:33 所属栏目:大数据 来源:网络整理
导读:######################################################################### # Brief: Exercise for Chapter 7 of "Intermediate Perl" # File: ch7_exam.pl # Creator: thinkhy # Date: 12/23/2012 use File:: Find ; use Time:: Local ; use File::Spec:
 
 
   
   
     
     
#########################################################################
     
     
# Brief: Exercise for Chapter 7 of "Intermediate Perl"
     
     
# File: ch7_exam.pl
     
     
# Creator: thinkhy
     
     
# Date: 12/23/2012
     
     
use File:: Find ;
     
     
use Time:: Local ;
     
     
use File::Spec:: Functions qw(canonpath no_upwards) ;
     
     
     
     
my $target_dow = 2 ; # Sunday is 0,Monday is 1,...
     
     
my @starting_directories = ( "." , "e:/tmp" );
     
     
my $seconds_per_day = 24 * 60 * 60 ;
     
     
my ( $sec , $min ,128)">$hour ,128)">$day ,128)">$mon ,128)">$yr ,128)">$dow ) = localtime ;
     
     
my $start = timelocal ( 0 , $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 ,128)">$yield ) = gather_mtime_between ( $start ,128)">$stop );
     
     
find ( @starting_directories );
     
     
     
     
my @files = $yield -> ( );
     
     
for my $file ( @files ) {
     
     
my $mtime = ( stat $file )[ 9 ]; # mtime via slice
     
     
my $when = localtime $mtime ;
     
     
print "$when: $filen" ;
     
     
}
     
     
     
     
sub gather_mtime_between {
     
     
my $start_time = shift ;
     
     
my $end_time =
     
     
my @files = ();
     
     
     
     
# Here use canonpath,details explained as below
     
     
# canonpath:
     
     
# No physical check on the filesystem,but a logical cleanup of a path.
     
     
# no_upwards:
     
     
# Given a list of file names,strip out those that refer to a
     
     
# parent directory. (Does not strip symlinks,
     
     
# only '.','..',and equivalents.)
     
     
# Refer to http://perldoc.perl.org/File/Spec.html
     
     
#
     
     
# Also,notice that when using stat inside the callback,the filename is $_
     
     
# but when the filename reperting to the user,the name is $File::Find::name
     
     
# because different current working directory for callback and user function
     
     
sub {
     
     
my $when = ( $_ )[ 9 ];
     
     
push @files , canonpath ( $ File::Find:: name )
     
     
if ( $when >= $start_time and $when <= $end_time ) },255)">
sub { @files = no_upwards ( @files );
     
     
wantarray ? @files : [ @files ] }
     
     
}
     
     
     
     
# Here is the 'official' answer for Chapter 7
     
     
sub
my ( $begin ,128)">$end ) = @_ ;
     
     
my @files ;
     
     
     
     
my $gatherer = sub {
     
     
my $timestamp = (
unless ( defined $timestamp ) {
     
     
warn "Can't stat $File::Find::name: $!,skippingn" ;
     
     
return ;
     
     
}
     
     
name
     
     
if $timestamp >= $begin and $timestamp <= $end ;
     
     
};
     
     
     
     
my $fetcher = sub { @files };
     
     
( $gatherer ,128)">$fetcher );
     
     
}
     
     
?
     
     
############################################################################
     
     
# Compared with answer,I omitted a special condition that the timestamp
     
     
# will be undef if the stat fails for some reason. That can happen,for example,255)">
# if it finds a dangling symbolic link(the target file doesn't exist).
     
     
# In that case,the callback warns the user and returns early.
     
     
# With the code of answer,if we omit that check,we can get warnings of?
# an undefined value during the comparison with begin time and end time.
     
     
############################################################################

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读