in reply to Re^3: How to use sprintf %n formatting pattern
in thread How to use sprintf %n formatting pattern

By using a sprintf parameter to determine the width:

my $str = '1234567890' x 6; my $prefixLen; my $magicLimit = tie_me_a_scalar(sub { $max_width - $prefixLen }); printf "Prefix stuff %n%.*s", $prefixLen, $magicLimit, $str;

Update: corrected %*s to %.*s

Replies are listed 'Best First'.
Re^5: How to use sprintf %n formatting pattern
by GrandFather (Saint) on Jun 09, 2022 at 12:44 UTC

    It's not clear to me how a tied scalar gets around the evaluation order issue with printf's arguments. Maybe you could provide a fully worked example?

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond