问题描述
类似于可以从文件中找到文件名吗?在Perl中使用文件句柄?,但在Tcl中.
Similar to Can I find a filename from a filehandle in Perl? but in Tcl.
无论如何,我计划缓存文件名-文件句柄的关联,所以我纯粹是出于好奇-尤其是链接中提到的操作系统向导".在Tcl中可以吗?
I plan to cache filename-filehandle associations anyway, so I'm asking purely out of curiosity--particularly of the "operating system wizardry" mentioned in the link. Is it possible in Tcl?
如果有关系,我(被迫)在SunOS 5.10上使用Tcl 8.0.5.
If it matters, I'm (forced into) using Tcl 8.0.5 on SunOS 5.10.
推荐答案
使用Tcl这么旧的版本,您实际上没有太多选择.最简单的方法是对 open
和 close
进行一些包装:
With a version of Tcl that old, you really don't have all that many options. The simplest is to do a little bit of wrapping of open
and close
:
rename open _original_open
rename close _original_close
proc open {filename args} {
global fileForChannel
set channel [eval [list _original_open $filename] $args]
set fileForChannel($channel) $filename
return $channel
}
proc close {channel} {
global fileForChannel
catch {unset fileForChannel($channel)}
_original_close $channel
}
然后,只需读取 $ fileForChannel($ ch)
,即可获取开放频道 $ ch
的文件名.
Then, you can get the filename for an open channel $ch
by just reading $fileForChannel($ch)
.
这篇关于我可以从Tcl的文件句柄中找到文件名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!