Perl:如何将子例程的词法文件句柄作为命名参数传递和使用?

Tho*_*day 7 perl subroutine

我想使用命名参数将词法文件句柄传递给子例程,但以下内容不能编译:

#!/usr/bin/perl -w
use strict;

my $log_fh;
my $logname = "my.log";

sub primitive {
   my ($fh, $m) = @_;
   print $fh $m;
}

sub sophisticated {
   my ($args) = @_;
   print $args->{m};
   print $args->{fh} $args->{m} ;
}

open $log_fh, ">", $logname;

print $log_fh "Today I learned ...\n";

primitive($log_fh,"... the old way works ...\n");

sophisticated({
   fh=>$log_fh, 
   m=>"... and the new way requires an intervention by SO.",
   });
close $log_fh;
Run Code Online (Sandbox Code Playgroud)

投诉是:

Scalar found where operator expected at ./lexical.file.handle.pl line 15, near
} $args"
(Missing operator before  $args?)

$ perl --version

This is perl, v5.10.1
Run Code Online (Sandbox Code Playgroud)

当我使用传递参数的原始技术时,它工作正常,而命名参数哈希技术适用于消息部分,而不适用于文件句柄部分.我需要新版本的打印吗?

fri*_*edo 17

如果你有一个返回文件句柄的复杂表达式(比如$args->{fh}),你需要通过添加一些额外的curlies来消除语法歧义:

print { $args->{fh} } $args->{m};
Run Code Online (Sandbox Code Playgroud)

这是由于print操作符设计的奇怪方式,文件句柄和要打印的东西列表之间没有逗号.

或者,您可以先从参数hashref中获取文件句柄,例如

my $fh = $args->{fh};
print $fh $args->{m};
Run Code Online (Sandbox Code Playgroud)