http://www.perlmonks.org?node_id=593049


in reply to How do I print formatted text to a scalar instead of a filehandle?

tie won't work, unfortunately. Here's a little trick that'll let you fudge it, with thanks to runrig's snippet for the inspiration/pointer and perldoc for the details.

### see perldocs perlvar, perlform, and formline for more details my $output; my %hash = (one => 1, two => 2, three => 3); # create a format picture for use with formline my $picture = '@<<<<<<<<< @##' . $/; foreach my $key (keys %hash) { # clear out the special variable $^A ("$ACCUMULATOR") $^A = ''; # again, see perldoc formline for details formline($picture,$key,$hash{$key}); # add the contents of $^A to $output $output .= $^A; } print $output;
the output:
one 1 two 2 three 3