你能在没有裸字 glob 的情况下 tie() 一个文件句柄吗?

KJ7*_*LNW 3 perl serial-port file-handling tie

我正在尝试使用Device::SerialPort而不使用裸字 glob,请参阅底部的问题。

这是他们的例子:

$PortObj = tie (*FH, 'Device::SerialPort', $Configuration_File_Name)
print FH "text";
Run Code Online (Sandbox Code Playgroud)

...但是用 *FH 污染命名空间感觉很脏...

tie(my $fh, ...)像你一样尝试过open (my $fh, ...),但Device::SerialPort没有实现TIESCALAR,所以它给出了一个错误。

这是我的肮脏黑客,但它仍然使用裸字,我什至无法让它限制裸字 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?
Run Code Online (Sandbox Code Playgroud)

问题:

  1. 是否可以以不创建裸字全局的方式进行绑定?
  2. 为什么大括号的范围不是 *fh?

ike*_*ami 6

您可以使用local

我希望这也能起作用:

use Symbol qw( gensym );

my $fh = gensym;
tie *$fh, ...
Run Code Online (Sandbox Code Playgroud)