Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Writing formatted text to a string?

by loris (Hermit)
on May 16, 2011 at 14:53 UTC ( #905093=note: print w/ replies, xml ) Need Help??


in reply to Re: Writing formatted text to a string?
in thread Writing formatted text to a string?

Just to remind myself: The backslash in open($fh, '>', \$variable) allows the filehandle to be opened to an "in memory" file held in a scalar. See open.

Update:

Here is my working example:

#!/usr/bin/perl use strict; use warnings; use English qw( -no_match_vars ); use Getopt::Long; my $in_memory; GetOptions('in_memory|i' => \$in_memory, ); my $greeting = "Hello World!"; format BLAH = Greeting: @<<<<<<<<<<<<<<<< $greeting . my $fh; my $message; if (defined($in_memory)) { open $fh, '>', \$message or die "Couldn't open for writing: $!"; } else { $fh = *STDOUT; } select($fh); $FORMAT_NAME = 'BLAH'; write(); select(STDOUT); if (defined($in_memory)) { print "Message written to in memory variable:\n"; print "$message"; close($fh); }

loris

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://905093]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2016-06-26 05:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...











    Results (327 votes). Check out past polls.