views:

77

answers:

1

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?

+3  A: 

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:

use Term::Size::Any;
my $cols = chars *STDOUT{IO};

With that, you can then print whatever you want, padded with the right amount of whitespace, e.g.:

printf "% ${cols}s", "Done\n";

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.

rafl