如何以编程方式生成多个 Perl 文件句柄?
Perl 有没有办法以编程方式生成文件句柄?
我想同时打开十个文件并使用由(常量名称+数字)组成的文件句柄写入它们。例如:
print const_name4 "data.."; #Then print the datat to file #4
Is there any way in Perl to generate file handles programmatically?
I want to open ten files simultaneously and write to them by using file handle which consists of (CONST NAME + NUMBER). For example:
print const_name4 "data.."; #Then print the datat to file #4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将文件句柄直接粘贴到未初始化的数组槽中。
不要忘记打印到数组中的句柄的语法略有不同:
You can stick filehandles straight into an uninitialised array slot.
Don't forget that the syntax for printing to a handle in an array is slightly different:
带有一点
IO::File
< /a> 和 map 你也可以这样做:With a bit of
IO::File
and map you can also do this:如今,您可以将文件句柄分配给标量(而不是使用表达式(如您的示例所示)),因此您只需创建一个数组并用它们填充它即可。
当然,您可以使用 变量 代替,但它们是一种令人讨厌的方法,我'从来没有见过使用数组或哈希不是更好的选择的时候。
These days you can assign file handles to scalars (rather than using expressions (as your example does)), so you can just create an array and fill it with those.
You can, of course, use variable variables instead, but they are a nasty approach and I've never seen a time when using an array or hash wasn't a better bet.