Perl:全球符号"%s"需要显式包名称

时间:2014-10-20 13:05:11

标签: perl variables global explicit

我对Perl很新,但我认为我已经记住了变量定义和范围等基础知识......但似乎没有。我的问题是我一直遇到错误:

Global symbol "$save_from_date" requires explicit package name at myMover.pl line 40

我有一个功能,我打算将文件日期与另一个日期(默认或参数)进行比较,但它失败了。

这是麻烦的代码,摘录:

# Process source directory
sub ParseSource {
    my $file = $_;
    my $filedate = localtime( ( stat $file )[9] )->ymd('');
    if ( $filedate >= $save_from_date ) {
        print "[To Archive] $file";
    }
}

#   Default Values
my $source_directory      = 'C:\Users\Public\Documents';
my $destination_directory = 'C:\Users\Public\Documents\Archive';
my $save_from_date        = strftime "%Y%m%d", localtime;
my ( $verboseornotverbose, $display_help ) = undef;

GetOptions(
    "verbose!"            => \$verboseornotverbose,
    "help|h!"             => \$display_help,
    "source_dir|s:s"      => \$source_directory,
    "destination_dir|d:s" => \$destination_directory,
    "date|t:i"            => \$save_from_date
);

usage() if $display_help;

# Basic checks
unless ( -d $source_directory ) { die "ERROR -- Source directory [$source_directory] does not exists" }
unless ( -d $destination_directory ) {
    unless ( mkdir $destination_directory ) {
        die "ERROR -- Destination directory $destination_directory does not exists and couldn't be created'";
    }
}
unless ( $save_from_date <= strftime "%Y%m%d", localtime ) {
    die "ERROR -- Wrong or unknown date format [$save_from_date] should be before or equal today\'s' date'";
}
print $save_from_date ;

# Here we parse the source directory
find( \&parseSource, $source_directory );

这是失败的比较: if ($filedate >= $save_from_date)

请你解释一下我在那个潜艇中错过了什么?

我已经阅读了很多其他帖子和一些变量定义教程,但我无法弄清楚我的问题...

谢谢!

2 个答案:

答案 0 :(得分:6)

解析sub时不声明变量。在sub之前移动变量声明,或者将变量作为参数传递给sub。

更新:您无法直接将参数传递给File::Find中的wanted子。但是,你可以将它包装在一个匿名子:

sub parseSource {
    my $save_from_date = shift;
    # ...
}

# ...

find( sub { parseSource($save_from_date) }, $source_directory );

答案 1 :(得分:0)

正如已经解释的那样,你必须在子程序中使用它们之前声明你的变量。

我还想证明您可以通过初始化代码并在同一步骤中传递引用来简化GetOptions代码:

GetOptions(
    "verbose!"            => \( my $verboseornotverbose ),
    "help|h!"             => \( my $display_help ),
    "source_dir|s:s"      => \( my $source_directory = 'C:\Users\Public\Documents' ),
    "destination_dir|d:s" => \( my $destination_directory = 'C:\Users\Public\Documents\Archive' ),
    "date|t:i"            => \( my $save_from_date = strftime "%Y%m%d", localtime ),
);

usage() if $display_help;

# Basic checks
die "ERROR -- Source directory [$source_directory] does not exists"
    unless -d $source_directory;

mkdir $destination_directory
    or die "ERROR -- Destination directory $destination_directory does not exists and couldn't be created'"
    unless -d $destination_directory;

die "ERROR -- Wrong or unknown date format [$save_from_date] should be before or equal today\'s' date'"
    unless $save_from_date <= strftime "%Y%m%d", localtime;

# Process source directory
sub ParseSource {
    my $file = $_;
    my $filedate = localtime( ( stat $file )[9] )->ymd('');
    if ( $filedate >= $save_from_date ) {
        print "[To Archive] $file";
    }
}