perl: printf indentation problem

hi all,
im having a problem with using perl printf. my requirement is to print a string (like [OK]) at the right most end of the screen.
i tried this perl script, but it fails with an error;

#!/usr/bin/perl 

use strict;
use warnings;

my $scrW = 0;

my $str = `stty size`;   # get the screen character width 
$str =~ m/(\d+)\s*$/;    # extract the char width
$scrW = eval($1);           # put the char width in a varable $scrW

$scrW -=10;                     # reduce the char width by 10
printf "%($scrW)s\n", "abcde";

tried to use printf with a variable as indentation length, but fails.
this type of method works in bash.
does anyone have an idea ?

thankx.

You don't need the eval, just assign $1 to $scrW. And take out the parentheses in the printf. You then need braces around the variable name to disambiguate the variable name from the traliing s, like ${scrW}s

If you subtract ten, you obviously don't use the entire screen width, but I guess you know that.

thankx era for the quick reply,
i subtracted ten thinking that the length of the string im gonna print is 10. :slight_smile:
what u said worked :b:, but i still had to use the eval.
anyway, thankx for the solution.

printf already does the subtraction for you; a positive number in %72s says pad with spaces as much as necessary to fill to the right up to, here, 72 characters width.

Believe us, the eval really is redundant

$ perl -e '($c)=qx(stty size)=~/\d+\s+(\d+)/;printf"%${c}s\n",$ARGV[0]' Hello\ World
                                                                          Hello World

sorry about my lack of understanding on how these functions work.
buffoonix`s method also works fine. thankx.