in reply to How to use sprintf %n formatting pattern
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 %
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to use sprintf %n formatting pattern
by GrandFather (Saint) on Jun 09, 2022 at 12:50 UTC | |
by hv (Prior) on Jun 09, 2022 at 13:54 UTC | |
by GrandFather (Saint) on Jun 09, 2022 at 21:10 UTC | |
by hv (Prior) on Jun 09, 2022 at 13:41 UTC | |
by GrandFather (Saint) on Jun 09, 2022 at 21:05 UTC | |
by hippo (Archbishop) on Jun 09, 2022 at 13:24 UTC |