Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Writing formatted text to a string?

by loris (Hermit)
on May 16, 2011 at 14:53 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://905093]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-18 05:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found