perl中文件的位置

时间:2011-07-26 16:09:38

标签: perl

正如本网站上的用户Chris所建议的那样。在第一个perl脚本中:值存储在字典中。第一个脚本很好。第一个脚本只运行一次并存储值。它正在发挥作用。

在第二个脚本中:

my $ processed = retrieve('processed_dirs.dat'); #$ processed是一个hashref

这里是读取第一个脚本中的“processed_durs.dat”。所以,我只是想知道第二个脚本如何知道Processed_dirs.dat在这里的位置?

#!/usr/bin/perl
use strict;
use warnings;
use Storable;

# This script to be run 1 time only. Sets up 'processed' directories hash.
# After this script is run, ready to run the daily script.

my $dir = '.'; # or what ever directory the date-directories are stored in

opendir my $dh, $dir or die "Opening failed for directory $dir $!";
my @dir = grep {-d && /^\d\d-\d\d-\d\d$/ && $_ le '11-04-21'} readdir $dh;
closedir $dh or die "Unable to close $dir $!";

my %processed = map {$_ => 1} @dir;

store \%processed, 'processed_dirs.dat';

第二个剧本:

#!/usr/bin/perl
use strict;
use warnings;
use File::Copy;
use Storable;

my $dir = shift or die "Provide path on command line. $!";

my $processed = retrieve('processed_dirs.dat'); # $processed is a hashref

opendir my $dh, $dir or die "Opening failed for directory $dir $!";
my @dir = grep {-d && /^\d\d-\d\d-\d\d$/ && !$processed->{$_} } readdir $dh;
closedir $dh or die "Unable to close $dir $!";
@dir or die "Found no unprocessed date directories";

my $fdir = '/some/example/path';

for my $date (@dir) {
    my $dday = "$dir/$date";
    my @gzfiles = glob("$dday/*tar.gz");

    foreach my $zf (@gzfiles) {  
        next if $zf =~ /BMP/ || $zf =~ /LG/ || $zf =~ /MAP/ || $zf =~ /STR/; 
        print "$zf\n";
        copy($zf, $fdir) or die "Unable to copy $zf to $fdir. $!";
    }
    $processed->{ $date } = 1;
}
store $processed, 'processed_dirs.dat';

1 个答案:

答案 0 :(得分:1)

除非我遗漏了某些内容,否则答案是:两个脚本都使用名为“processed_dirs.dat”的文件,无论它们在哪个目录中运行。因此,只要两个脚本都从同一目录运行,它们都将使用相同的文件。