From: PerlFAQ Server on 27 Jul 2010 12:00 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.13: How can I open a filehandle to a string? (contributed by Peter J. Holzer, hjp-usenet2(a)hjp.at) Since Perl 5.8.0 a file handle referring to a string can be created by calling open with a reference to that string instead of the filename. This file handle can then be used to read from or write to the 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.
From: Justin C on 28 Jul 2010 06:04 On 2010-07-27, PerlFAQ Server <brian(a)theperlreview.com> wrote: > 5.13: How can I open a filehandle to a string? > > (contributed by Peter J. Holzer, hjp-usenet2(a)hjp.at) > > Since Perl 5.8.0 a file handle referring to a string can be created by > calling open with a reference to that string instead of the filename. > This file handle can then be used to read from or write to the 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" I can see how it works, but I wonder in what circumstances one might want to do this? Justin. -- Justin C, by the sea.
From: Peter J. Holzer on 28 Jul 2010 07:53 On 2010-07-28 10:04, Justin C <justin.1007(a)purestblue.com> wrote: > On 2010-07-27, PerlFAQ Server <brian(a)theperlreview.com> wrote: >> 5.13: How can I open a filehandle to a string? >> >> (contributed by Peter J. Holzer, hjp-usenet2(a)hjp.at) >> >> Since Perl 5.8.0 a file handle referring to a string can be created by >> calling open with a reference to that string instead of the filename. >> This file handle can then be used to read from or write to the 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" > > I can see how it works, but I wonder in what circumstances one might > want to do this? Sometimes you have a module which is intended to read from a file. But you have the data already in memory. Of course you could write it to a temporary file and then invoke the module to read it back. But that's not very elegant - much better if you can tell it to read from a scalar. Same thing the other way: You have a module which can only write to a file but you need the output in a variable. I regularly use this in test scripts. hp
From: Justin C on 28 Jul 2010 09:23 On 2010-07-28, Peter J. Holzer <hjp-usenet2(a)hjp.at> wrote: > On 2010-07-28 10:04, Justin C <justin.1007(a)purestblue.com> wrote: >> On 2010-07-27, PerlFAQ Server <brian(a)theperlreview.com> wrote: >>> 5.13: How can I open a filehandle to a string? >>> >>> (contributed by Peter J. Holzer, hjp-usenet2(a)hjp.at) >>> >>> Since Perl 5.8.0 a file handle referring to a string can be created by >>> calling open with a reference to that string instead of the filename. >>> This file handle can then be used to read from or write to the 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" >> >> I can see how it works, but I wonder in what circumstances one might >> want to do this? > > Sometimes you have a module which is intended to read from a file. But > you have the data already in memory. Of course you could write it to a > temporary file and then invoke the module to read it back. But that's > not very elegant - much better if you can tell it to read from a scalar. > Same thing the other way: You have a module which can only write to a > file but you need the output in a variable. > > I regularly use this in test scripts. Thank you for the explanation. I suppose I've either never used a module like that or I've always gone the long way round. I will try to keep this in mind should I encounter such a problem. Justin. -- Justin C, by the sea.
|
Pages: 1 Prev: FAQ 4.9 How can I output Roman numerals? Next: XML::Simple - Processing Query |