名前付き引数を使用して字句ファイルハンドルをサブルーチンに渡したいのですが、以下はコンパイルされません。
#!/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;
苦情は次のとおりです。
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
引数を渡すという基本的な手法を使用すると問題なく機能します。名前付き引数のハッシュ手法は、ファイルハンドル部分ではなく、メッセージ部分に対して機能します。新しいバージョンの印刷物が必要ですか?