Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

( #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? 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

Comment on Answer: How do I print formatted text to a scalar instead of a filehandle?
Select or Download Code
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 avoiding work at the Monastery: (13)
As of 2014-09-17 17:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (92 votes), past polls