% 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 %