我正在尝试使用 Device::SerialPort没有裸词 glob,请参阅底部的问题。
这是他们的例子:
$PortObj = tie (*FH, 'Device::SerialPort', $Configuration_File_Name)
print FH "text";
...但是用 *FH 污染命名空间感觉很脏...
我尝试了 tie(my $fh, ...)
就像使用 open (my $fh, ...)
一样,但是 Device::SerialPort
未实现 TIESCALAR
,因此它会出错。
这是我的肮脏 hack,但它仍然使用裸词,我什至无法让它限定裸词 glob 的范围:
# This does _not_ scope *FH, but shouldn't it?
{
$port = tie(*FH, 'Device::SerialPort', $ARGV[0]) or die "$ARGV[0]: $!";
$fh = \*FH; # as a GLOB
$fh = bless(\*FH, 'IO::Handle'); # or as an IO::Handle
}
print $fh "text"; # this works
print FH "text"; # so does this, but shouldn't *FH be scoped?
问题:
- 是否有可能以不创建裸字 glob 的方式进行绑定(bind)?
- 为什么大括号不作用于 *fh?
最佳答案
您可以使用local
。
我希望这也能奏效:
use Symbol qw( gensym );
my $fh = gensym;
tie *$fh, ...
关于perl - 你能 tie() 一个没有裸字 glob 的文件句柄吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72525241/