Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Keep It Simple, Stupid
 
PerlMonks  

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

( #593050=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 shmem

Since Perl 5.8.0 you can open a filehandle for writing to a scalar. Then, if you make that filehandle the default, writes will go there.

my %hash = (one => 1, two => 2, three => 3); my $output; open FH, '>', \$output; my $old_fh = select(FH); my $key; foreach $key (keys %hash) { write; } select ($old_fh); print $output; format FH = @<<<<<<<<< @## $key, $hash{$key} .
Content of $output:
three 3 one 1 two 2

Unfortunately, this doesn't seem to work with localized filehandles.

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 surveying the Monastery: (5)
As of 2014-04-19 02:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (475 votes), past polls