"Can't call method "size" 未定义值

"Can't call method "size" on an undefined value

我编写了一个 perl 脚本,其中出现错误“无法在 userAccessLogMon.pl 第 49 行对未定义的值调用方法“大小”。”以下是我要执行的脚本。我是 Perl 的新手,运行 脚本作为测试的一部分。有关此错误的任何指导都会有所帮助。

这些脚本当前用作迁移到 AWS 的一部分。

#!/usr/bin/perl
use File::stat;
use File::Basename;
use Time::localtime;


my $dirname = dirname(__FILE__);
my $name = basename([=11=]);
my $json_file = "$dirname/$name.json";

#open (FILE, "> $json_file") || die "problem opening $json_file\n";
#close (FILE);

my $config_file = "$dirname/userAccessLogMon.conf";
my ($enabled,$run_counter, $source_log, $byte_offset, $line, $log_line, $log_type)="";


open (my $fh_config, "<",$config_file) or die("Could not open Config File");
my @all_lines;
my $criticals;
@all_lines = <$fh_config>;
for my $index (0..$#all_lines)
{
if ($all_lines[$index]  =~ m/GENGREP/)
{
            chomp $all_lines[$index];
            ($enabled, $log_type) = split('#',substr($all_lines[$index],8));
    }
    elsif ($all_lines[$index]  =~ m/CRITGREP/)
    {
            chomp $all_lines[$index];
            $criticals .= substr($all_lines[$index],9);
    }
}
$criticals= substr($criticals,0,-1);
if ( $enabled eq 'Y')
{
my $config_file_seek = "$dirname/seek_userAccessLogMon.conf";
open (my $fh_config_seek,"<",$config_file_seek) or die("Could not open Config Seek File");
foreach $line (<$fh_config_seek>)
{
        chomp($line);
        ($source_log, $byte_offset) =split('#',$line);
}
close $fh_config_seek;
my $out_file_crit = "$dirname/userAccessLogMon.out";
my $fh_log, $fh_crit;
open ($fh_crit, ">", $out_file_crit);
my $file_size = stat($source_log)->size;
if ($file_size < $byte_offset)
{
        $byte_offset = 0;
}
open ($fh_log,"<",$source_log);
seek $fh_log, $byte_offset, SEEK_SET;
while ($log_line = <$fh_log>)
{
    if ($log_line =~ m/$criticals/)
    {      # print "$log_line";
            print $fh_crit $log_line;
    }
}
close $fh_log, $fh_warn, $fh_crit, $fh_config, $fh_config_seek;
`find $out_file_crit -size 0 -exec rm -f {} +`;

$byte_offset = stat($source_log)->size;
open $fh_config_seek, ">", $config_file_seek;
       print $fh_config_seek "$source_log#$byte_offset";
close $fh_config_seek;
}
my $fName = "/datalocal/monitor/userAccessLogMon/userAccessLogMon.out";
my $TfName = "/datalocal/monitor/userAccessLogMon/userAccessLogMon.tmp";
if (-e $fName)
{
if (-e $TfName)
{
        unlink($TfName) or die "Can't unlink $TfName : $!";
}
`cut -d '=' -f2 $fName | sed 's/>//g' | sort | uniq -c | awk '{$1=$1};1' | sed 's/\s/,/g'>> 
$dirname/userAccessLogMon.tmp`;
open CONFIGFILE, "<", $TfName;
            my ($confLine, $userID, $count, $metrics_data)="";
            $metrics_data="[";
            while ($confLine = <CONFIGFILE>)
            {
                    chomp($confLine);
                    ($count, $userID) = split(",",$confLine);
                    $metrics_data .= "{\"eventType\":\"DBS_Prod_User_LogOn_Dtl\",";
                    $metrics_data .= "\"UserID\": \"$userID\",";
                    $metrics_data .= "\"Server\":\"USNENCVL069\",";
        $metrics_data .= "\"LogonCnt\":$count\},\n";
            }
    close CONFIGFILE;
    $metrics_data =~ s/,$/]/g;
    open (FILE, "> $json_file") || die "problem opening $json_file\n";
    print FILE $metrics_data;
    close(FILE);
    print $metrics_data;
    unlink($TfName) or die "Can't unlink $TfName : $!";
    unlink($fName) or die "Can't unlink $fName : $!";
}

来自 stat documentation:Returns 如果统计失败则为空列表。

如果将 stat($source_log) 的结果分配给一个变量,则可以检查大小是否已填充:

my $stat_result = stat($source_log);
if (!$stat_result || !$stat_result->size){
    ...handle lack of result
}
else{
    $byte_offset = $stat_result->size;
}