Click here to get back home

FAQ 5.12 How can I open a filehandle to a string?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
FAQ 5.12 How can I open a filehandle to a string? PerlFAQ Server 05-26-2008
Posted by PerlFAQ Server on May 26, 2008, 3:03 pm
Please log in for more thread options
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.12: How can I open a filehandle to a string?

, , ,
(contributed by Peter J. Holzer, hjp-usenet2@hjp.at)

Since Perl 5.8.0, you can pass a reference to a scalar instead of the
filename to create a file handle which you can used to read from or
write to a string:

open(my $fh, '>', $string) or die "Could not open string for
writing";
print $fh "foo\n";
print $fh "bar\n"; # $string now contains "foo\nbar\n"

open(my $fh, '<', $string) or die "Could not open string for
reading";
my $x = <$fh>; # $x now contains "foo\n"

With older versions of Perl, the "IO::String" module provides similar
functionality.



--------------------------------------------------------------------

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.

Posted by Bill H on May 26, 2008, 3:19 pm
Please log in for more thread options
[snip]
> 5.12: How can I open a filehandle to a string?
>
> , , ,
> =A0 =A0 (contributed by Peter J. Holzer, hjp-usen...@hjp.at)
>
> =A0 =A0 Since Perl 5.8.0, you can pass a reference to a scalar instead of =
the
> =A0 =A0 filename to create a file handle which you can used to read from o=
r
> =A0 =A0 write to a string:
>
> =A0 =A0 =A0 =A0 =A0 =A0 open(my $fh, '>', $string) or die "Could not open=
string for writing";
> =A0 =A0 =A0 =A0 =A0 =A0 print $fh "foo\n";
> =A0 =A0 =A0 =A0 =A0 =A0 print $fh "bar\n"; =A0 =A0 =A0# $string now contai=
ns "foo\nbar\n"
>
> =A0 =A0 =A0 =A0 =A0 =A0 open(my $fh, '<', $string) or die "Could not open=
string for reading";
> =A0 =A0 =A0 =A0 =A0 =A0 my $x =3D <$fh>; =A0# $x now contains "foo\n"
>
> =A0 =A0 With older versions of Perl, the "IO::String" module provides simi=
lar
> =A0 =A0 functionality.
>
[snip]

This is interesting, but what real world examples would there be to
doing this instead of just using string functions? The only one I can
think of is to redirect file output to a string for debugging
purposes, or maybe for duplication purposes, direct to a string, write
to a file, write to another etc.

Bill H

Posted by Uri Guttman on May 26, 2008, 3:40 pm
Please log in for more thread options

BH> [snip]
>> 5.12: How can I open a filehandle to a string?
>>
>> , , ,
>>     (contributed by Peter J. Holzer, hjp-usen...@hjp.at)
>>
>>     Since Perl 5.8.0, you can pass a reference to a scalar instead of the
>>     filename to create a file handle which you can used to read from or
>>     write to a string:
>>
>>             open(my $fh, '>', $string) or die "Could not open string for
writing";
>>             print $fh "foo\n";
>>             print $fh "bar\n";      # $string now contains "foo\nbar\n"
>>
>>             open(my $fh, '<', $string) or die "Could not open string for
reading";
>>             my $x = <$fh>;  # $x now contains "foo\n"
>>
>>     With older versions of Perl, the "IO::String" module provides similar
>>     functionality.
>>
BH> [snip]

BH> This is interesting, but what real world examples would there be to
BH> doing this instead of just using string functions? The only one I can
BH> think of is to redirect file output to a string for debugging
BH> purposes, or maybe for duplication purposes, direct to a string, write
BH> to a file, write to another etc.

there are many uses. sometimes you have data and some parsing modules
expect a file handle. or some module expects to print to a file and you
want to put the data elsewhere (or use it in two places). even the list
you provided are all real world uses. sure you don't do this everyday
but it will come up once in a while and it is good that it is easy to
use. the previous way was with a tied handle module which is much
slower.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

Posted by brian d foy on May 26, 2008, 4:46 pm
Please log in for more thread options
In article

> [snip]
> > 5.12: How can I open a filehandle to a string?

> This is interesting, but what real world examples would there be to
> doing this instead of just using string functions?

I usually need this where some module insists on having a filehandle to
send its data to, but I want it in a string without all the extra work
and left-over files. Now I can print to a string directly.

Posted by Peter J. Holzer on May 29, 2008, 6:15 pm
Please log in for more thread options
> In article
>
>> [snip]
>> > 5.12: How can I open a filehandle to a string?
>
>> This is interesting, but what real world examples would there be to
>> doing this instead of just using string functions?
>
> I usually need this where some module insists on having a filehandle to
> send its data to, but I want it in a string without all the extra work
> and left-over files. Now I can print to a string directly.

I find this especially handy in test scripts. For example, here's an
excerpt from a test script for a project I'm currently working on:

my $s;
close(STDOUT);

my $id = $dataset->id;

open(STDOUT, '>', $s);
$dataset->print('xml');
ok(index($s, "<d:id>$id</d:id>") != -1, "generated XML contains
<d:id>$id</d:id>");
ok(index($s, "<d:description xml:lang='de'") != -1, "generated XML contains
german description");

(in this case $dataset also has a method as_xml, which returns the XML
representation as a string. So this test mainly serves to ensure that
print really calls as_xml and prints the result to STDOUT. But it might
actually be useful to turn them around: Put all the logic into print and
then implement as_xml as a wrapper around print - this safes memory when
you are only printing and allows pipeling the produces and the consumer
of the XML file)

        hp


Similar ThreadsPosted
FAQ 5.12 How can I open a filehandle to a string? March 9, 2008, 9:03 am
FAQ 5.12 How can I open a filehandle to a string? August 4, 2008, 9:03 pm
How do I determine whether a filehandle is open? April 11, 2008, 3:10 pm
How to convert from a filehandle to a string? September 5, 2007, 6:12 pm
FAQ: Why doesn't open() return an error when a pipe open fails? October 22, 2004, 5:10 pm
FAQ 8.26 Why doesn't open() return an error when a pipe open fails? March 9, 2005, 6:03 am
FAQ 8.26 Why doesn't open() return an error when a pipe open fails? May 22, 2005, 5:03 pm
FAQ 8.26 Why doesn't open() return an error when a pipe open fails? August 7, 2005, 10:03 am
FAQ 8.26 Why doesn't open() return an error when a pipe open fails? October 4, 2005, 10:03 pm
FAQ 8.26 Why doesn't open() return an error when a pipe open fails? December 5, 2005, 5:03 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap