Here's an approach that achieves what you ask for. As mentioned earlier, it is pretty insane to do this - doing it in two separate steps makes far more sense - but it is possible.
% cat t0 use strict; use warnings; my $max_width = shift @ARGV; my $prefix = "foo"; my $postfix = "0123456789"; my $stored_width; tie my $limit, 'LazyScalar', sub { $max_width - $stored_width }; printf "%s%n%.*s\n", $prefix, $stored_width, $limit, $postfix; # A simple tied class for deferred evaluation of a scalar's value package LazyScalar { sub TIESCALAR { my($class, $cb) = @_; return bless \$cb, $class; } sub FETCH { my($self) = @_; return $$self->(); } }; % perl t0 6 foo012 % perl t0 7 foo0123 % perl t0 8 foo01234 %
In reply to Re: How to use sprintf %n formatting pattern
by hv
in thread How to use sprintf %n formatting pattern
by ibm1620
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |