如何修复变量“file_size_kb”不会在...保持共享?使用 Perl 语言

问题描述 投票:0回答:1

问题出现在打印行。请帮助,因为我是编程新手。我的问题是变量不会在子例程语句中保持共享,并且 PDF 文件行的 #Path 处存在未知的正则表达式修饰符“/v”和“/e”。

# Path to the PDF file
my $pdf_file = 'path/to/your/file.pdf';

# Size threshold in kilobytes
my $size_threshold_kb = 1_000_000;

# Get the file size in bytes
my $file_size_bytes = -s $pdf_file;

# Convert bytes to kilobytes
my $file_size_kb = $file_size_bytes / 1024;

# Check if the file size is less than the threshold
if ($file_size_kb < $size_threshold_kb) {
    # Perform PDF impression reconciliation
    reconcile_pdf_impression($pdf_file);
} else {
    # Log that no reconciliation happened
    log_no_reconciliation($pdf_file);
}

sub reconcile_pdf_impression {
    my ($file) = @_;
    # Replace with actual reconciliation logic
    print "Performing PDF impression reconciliation for $file.\n";
}

sub log_no_reconciliation {
    my ($file) = @_;
    open my $log_fh, '>>', 'reconciliation.log' or die "Could not open log file: $!";
    print $log_fh "No reconciliation performed for $file because its size is $file_size_kb KB.\n";
    close $log_fh;
}

我试图打印文件大小,在 pdf 中添加一个条件,如果大小低于 1,000,000,那么它将进行印象核对,并在没有印象核对的情况下创建日志。

perl
1个回答
0
投票

要与子程序共享变量,您需要:

  • 将其传递到子例程中
my $a = 1;
foo($a);

sub foo {
  // $a is not visible here
  my ($b) = $@;
  print "$b\n";
}
  • 或将其定义为全局。
our $a = 1;
foo;

sub foo {
  print "$a\n";
}
© www.soinside.com 2019 - 2024. All rights reserved.