我正在维护旧的Perl代码,需要在所有模块中启用严格的pragma.我在传递文件句柄作为模块和subs之间的引用时遇到问题.我们有一个公共模块负责打开日志文件,该文件作为typeglob引用传递.在其他模块中,run函数首先从公共模块调用open_log(),然后将此文件句柄传递给其他subs.
在这里,我写了一个简单的测试来模拟这种情况.
#!/usr/bin/perl -w
use strict;
$::STATUS_OK = 0;
$::STATUS_NOT_OK = 1;
sub print_header {
our $file_handle = @_;
print { $$file_handle } "#### HEADER ####"; # reference passing fails
}
sub print_text {
my ($file_handle, $text)= @_;
print_header(\$file_handle);
print { $$file_handle } $text;
}
sub open_file_handle {
my ($file_handle, $path, $name) = @_;
my $filename = $path."\\".$name;
unless ( open ($$file_handle, ">".$filename)) {
print STDERR "Failed to open file_handle $filename for writing.\n";
return $::STATUS_NOT_OK;
}
print STDERR "File $filename …Run Code Online (Sandbox Code Playgroud)