FAQ 5.11 How can I write() into a string?
FAQ 5.11 How can I write() into a string?
am 11.10.2007 09:03:02 von PerlFAQ Server
This is an excerpt from the latest version perlfaq5.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .
------------------------------------------------------------ --------
5.11: How can I write() into a string?
See "Accessing Formatting Internals" in perlform for an swrite()
function.
------------------------------------------------------------ --------
The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.
If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
Re: FAQ 5.11 How can I write() into a string?
am 27.10.2007 12:54:59 von hjp-usenet2
On 2007-10-11 07:03, PerlFAQ Server wrote:
> 5.11: How can I write() into a string?
>
>
> See "Accessing Formatting Internals" in perlform for an swrite()
> function.
>
I think a more useful companion question would be
How can I open a file handle to a string?
Use open(my $fh, '>', \$string); or open(my $fh, '<', \$string);
If this is in the FAQ I haven't found it.
hp
--
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Sysadmin WSR | with an emu on his shoulder.
| | | hjp@hjp.at |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"
Re: FAQ 5.11 How can I write() into a string?
am 27.10.2007 13:52:30 von Ben Morrow
Quoth "Peter J. Holzer" :
> On 2007-10-11 07:03, PerlFAQ Server wrote:
> > 5.11: How can I write() into a string?
> >
> > See "Accessing Formatting Internals" in perlform for an swrite()
> > function.
>
> I think a more useful companion question would be
>
> How can I open a file handle to a string?
>
> Use open(my $fh, '>', \$string); or open(my $fh, '<', \$string);
With reference to IO::String for pre-5.8 perls?
Ben
Re: FAQ 5.11 How can I write() into a string?
am 27.10.2007 13:53:44 von Michele Dondi
On Sat, 27 Oct 2007 12:54:59 +0200, "Peter J. Holzer"
wrote:
>I think a more useful companion question would be
>
> How can I open a file handle to a string?
>
> Use open(my $fh, '>', \$string); or open(my $fh, '<', \$string);
I second that.
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Re: FAQ 5.11 How can I write() into a string?
am 27.10.2007 17:15:58 von hjp-usenet2
On 2007-10-27 11:52, Ben Morrow wrote:
>
> Quoth "Peter J. Holzer" :
>> On 2007-10-11 07:03, PerlFAQ Server wrote:
>> > 5.11: How can I write() into a string?
>> >
>> > See "Accessing Formatting Internals" in perlform for an swrite()
>> > function.
>>
>> I think a more useful companion question would be
>>
>> How can I open a file handle to a string?
>>
>> Use open(my $fh, '>', \$string); or open(my $fh, '<', \$string);
>
> With reference to IO::String for pre-5.8 perls?
Yup. Something like this:
------------------------------------------------------------ ------------
How can I open a file handle to a string?
Since Perl 5.8.0, you can pass a reference to a scalar instead of
the filename to create a file handle which can be used to read of
write the string:
open(my $fh, '>', \$string) or die ...
print $fh "foo\n";
print $fh "bar\n"; # $string now contains "foo\nbar\n"
open(my $fh, '<', \$string) or die ...
my $x = <$fh>; # $x now contains "foo\n"
With older perls the IO::String module provides similar
functionality.
------------------------------------------------------------ ------------
hp
--
_ | Peter J. Holzer | It took a genius to create [TeX],
|_|_) | Sysadmin WSR | and it takes a genius to maintain it.
| | | hjp@hjp.at | That's not engineering, that's art.
__/ | http://www.hjp.at/ | -- David Kastrup in comp.text.tex
Re: FAQ 5.11 How can I write() into a string?
am 27.10.2007 21:17:42 von brian d foy
In article , Peter J. Holzer
wrote:
> On 2007-10-11 07:03, PerlFAQ Server wrote:
> > 5.11: How can I write() into a string?
> >
> >
> > See "Accessing Formatting Internals" in perlform for an swrite()
> > function.
> >
>
> I think a more useful companion question would be
>
> How can I open a file handle to a string?
That would be a good question, although you might want to phrase is
"How do I print to a string instead of a file?".
If someone writes out the full answer, I'll add it to the FAQ. Please
send the answer to perlfaq-workers as noted in perlfaq.pod.
Thanks, :)
Re: FAQ 5.11 How can I write() into a string?
am 27.10.2007 21:26:40 von brian d foy
In article <271020071417420054%brian.d.foy@gmail.com>, brian d foy
wrote:
> In article , Peter J. Holzer
> wrote:
> > How can I open a file handle to a string?
> If someone writes out the full answer, I'll add it to the FAQ. Please
> send the answer to perlfaq-workers as noted in perlfaq.pod.
Oh, never mind. Peter already posted an answer. I've added that to the
FAQ.
Thanks, :)
Re: FAQ 5.11 How can I write() into a string?
am 27.10.2007 21:48:39 von jurgenex
brian d foy wrote:
> In article , Peter J. Holzer
> wrote:
>
>> On 2007-10-11 07:03, PerlFAQ Server wrote:
>>> 5.11: How can I write() into a string?
>>>
>>>
>>> See "Accessing Formatting Internals" in perlform for an swrite()
>>> function.
>>>
>>
>> I think a more useful companion question would be
>>
>> How can I open a file handle to a string?
>
> That would be a good question, although you might want to phrase is
> "How do I print to a string instead of a file?".
Well, isn't the answer to that question a simple:
Use the sprintf() function?
jue
Re: FAQ 5.11 How can I write() into a string?
am 28.10.2007 02:24:08 von Michele Dondi
On Sat, 27 Oct 2007 19:48:39 GMT, "Jürgen Exner"
wrote:
>> That would be a good question, although you might want to phrase is
>> "How do I print to a string instead of a file?".
>
>Well, isn't the answer to that question a simple:
>
> Use the sprintf() function?
No, that's the answer to "How do I printf to a string instead of a
file?"
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Re: FAQ 5.11 How can I write() into a string?
am 28.10.2007 11:37:03 von hjp-usenet2
On 2007-10-27 19:17, brian d foy wrote:
> In article , Peter J. Holzer
> wrote:
>>
>> How can I open a file handle to a string?
>
> That would be a good question, although you might want to phrase is
> "How do I print to a string instead of a file?".
That was my first idea, too. I changed it because it works for
reading a string as well as writing.
> Thanks, :)
You're welcome.
hp
--
_ | Peter J. Holzer | It took a genius to create [TeX],
|_|_) | Sysadmin WSR | and it takes a genius to maintain it.
| | | hjp@hjp.at | That's not engineering, that's art.
__/ | http://www.hjp.at/ | -- David Kastrup in comp.text.tex