perl - 你能 tie() 一个没有裸字 glob 的文件句柄吗?

标签 perl serial-port file-handling tie

我正在尝试使用 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?

问题:

  1. 是否有可能以不创建裸字 glob 的方式进行绑定(bind)?
  2. 为什么大括号不作用于 *fh?

最佳答案

您可以使用local

我希望这也能奏效:

use Symbol qw( gensym );

my $fh = gensym;
tie *$fh, ...

关于perl - 你能 tie() 一个没有裸字 glob 的文件句柄吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72525241/

相关文章:

java - 尝试使用资源如何调用刷新方法

linux - 从数据库创建用户给我随机结果

c# - 我可以在 C# 中通过 serialPort 发送一个 int 吗?

c - 在c中更新/覆盖文件而不创建新文件或临时文件

file-handling - 在 Web 安装程序上,如何排除所有 .pdb 文件?

java - 无法下载 javax.comm

linux - perl-mechanize 遇到限制 - 开始进行多次调试尝试

perl - 用 ssh 替换 telnet

perl - perl中如何将每个数组的值相加?

sockets - 转发蓝牙套接字到串口