perl 文件句柄的问题
my $fh='';
open ($fh,$dnafile) or die "cannot open $dnafile($!)\n";
会报错, can't use string ("")as a symbol ref while "strict refs" in use at XXXXXXX。
my $fh;
open ($fh,$dnafile) or die "cannot open $dnafile($!)\n";
就正常了,这是怎么回事呢?哪位高手能解释下呢,谢谢了。
[解决办法]
初始化成undef就行了,这个就是Perl中经常提到的上下文(context).
A filehandle is just a name you give to a file,device, socket, or pipe to help you remember which one you’re talking about,and to hide some of the complexities of buffering and such. (Programming Perl(4th))
句柄是WONDOWS用来标识被应用程序所建立或使用的对象的唯一整数。所以,它应该是一个整数。
[解决办法]
If FILEHANDLE is an undefined scalar variable (or array or hash
element), a new filehandle is autovivified, meaning that the
variable is assigned a reference to a newly allocated anonymous
filehandle. Otherwise if FILEHANDLE is an expression, its value
is the real filehandle. (This is considered a symbolic
reference, so "use strict "refs"" should *not* be in effect.)
多看帮助,如果$fh不为undef,它的值会被解释为一个符号引用。