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
Get Chitika Premium
Posted by Bill H on May 29, 2008, 6:42 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 =3D $dataset->id;
>
> open(STDOUT, '>', $s);
> $dataset->print('xml');
> ok(index($s, "<d:id>$id</d:id>") !=3D -1, =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
"generated XML contains <d:id>$id</d:id>");
> ok(index($s, "<d:description xml:lang=3D'de'") !=3D -1, =A0 =A0 "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)
>
> =A0 =A0 =A0 =A0 hp

I like this idea Peter. I am starting to do a lot of xml processing
via perl and this may come in handy.

Thanks, Bill H

Posted by Peter J. Holzer on May 29, 2008, 5:57 pm
Please log in for more thread options
> --------------------------------------------------------------------
>
> 5.12: How can I open a filehandle to a string?
>
> , , ,

I think the commas in

| X<string>, X<open>, X<IO::Scalar>, X<filehandle>

should be omitted.

> (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:

Yikes! Did I really create that monster of a sentence (not to mention
the typo)? I guess it shows that German is my native language. How about
this?

| 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:

        hp


Posted by Ben Morrow on May 29, 2008, 10:02 pm
Please log in for more thread options

>
> Yikes! Did I really create that monster of a sentence (not to mention
> the typo)? I guess it shows that German is my native language. How about
> this?
>
> | 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:

| Since Perl 5.8.0 a file handle can be used to read and write the
| contents of a scalar instead of a file on disk. This is done by
| passing C<open> a reference in place of a filename:

The important part is 'this handle doesn't actually point to a file'.
The detail of passing a reference should come later.

Ben

--
Razors pain you / Rivers are damp
Acids stain you / And drugs cause cramp. [Dorothy Parker]
Guns aren't lawful / Nooses give
Gas smells awful / You might as well live. ben@morrow.me.uk

Posted by Peter J. Holzer on May 30, 2008, 9:47 am
Please log in for more thread options
>> Yikes! Did I really create that monster of a sentence (not to mention
>> the typo)? I guess it shows that German is my native language. How about
>> this?
>>
>> | 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:
>
>| Since Perl 5.8.0 a file handle can be used to read and write the
>| contents of a scalar instead of a file on disk. This is done by
>| passing C<open> a reference in place of a filename:
>
> The important part is 'this handle doesn't actually point to a file'.
> The detail of passing a reference should come later.

Yes, that's better.

        hp

Posted by David Combs on June 24, 2008, 10:52 am
Please log in for more thread options

The suggested examples and uses all concern writing *to* a string.

How about for reading *from* a string?

What are some of the (possible) uses for doing that?


And -- what about "peek" on such a thing?

And a "putback", as for when you have to look-ahead?


-----


Re reading *and writing* to/from a string (pretending to be
an open file), what are some of the useful perl-operations
already existing for *files*, for which there's no corresponding
perl-op for *strings*, which you can get at via the kludge
of pretending it's a file?


I think such a discussion here (in the faq) could lead to
lots of interesting (and useful!) programming-tricks!

David







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
FAQ 5.12 How can I open a filehandle to a string? November 2, 2008, 9:03 am
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
Filehandle Adapter for String? October 15, 2008, 12:02 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

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap