如何打印“完成”或“失败”在 Perl 中标准输出的行尾?
我刚刚开始使用 Perl,我想编写自己的脚本来扫描文档并将生成的 TIFF 文件转换为 PDF 文件。如果转换成功(使用 tiff2pdf
),我想在行尾打印“完成”,但我似乎无法在网络上找到执行此操作的提示。
我的猜测是,我必须获取终端的几何形状并计算我已经打印的字母,但这似乎太复杂了。你有什么建议吗?
I just have begun with Perl and I want to write my own script to scan a document and convert the resulting TIFF file to a PDF file. If the conversion succeeds (using tiff2pdf
), I want to print "Done" at the end of the line, but I can't seem to find a hint to do this on the Web.
My guess is that I have to get the geometry of the terminal and count the letters I already printed but that seems to be to complicated. Do you have any advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须检查要打印到的终端的大小,这是正确的。有很多方法可以做到这一点,但我知道的最便携、最可靠的方法是
术语::大小::任意
。这样,您就可以获得正在运行的终端的宽度:
这样,您就可以打印您想要的任何内容,并用适量的空格填充,例如:
还要注意程序并不总是输出到终端。例如,输出可以重定向到文件,因此如果确定终端大小失败,您可能希望有适当的后备。
You're right about having to inspect the size of the terminal you're printing to. There's many ways to do that, but the most portable and reliable way I'm aware of is
Term::Size::Any
.With that, you can get the width of the terminal you're running in:
With that, you can then print whatever you want, padded with the right amount of whitespace, e.g.:
Also be aware that programs don't always output to terminals. Output could, for example, be redirected to a file, so you might want to have an appropriate fallback if determining the terminal size fails.