Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Answer: How do I print formatted text to a scalar instead of a filehandle?

by kwaping (Priest)
on Jan 05, 2007 at 00:19 UTC ( #593049=categorized answer: print w/replies, xml ) Need Help??

Q&A > input and output > How do I print formatted text to a scalar instead of a filehandle? - Answer contributed by kwaping

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

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2016-09-29 20:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:







    Results (560 votes). Check out past polls.