Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Filehandle Adapter for String?

0 views
Skip to first unread message

bugbear

unread,
Oct 15, 2008, 12:02:20 PM10/15/08
to
It is quite common for modules to be able to handle
files, or filehandles, but not strings.

In order to ease some of the uses I have for
such modules, in an age where a 1 Mb scalar
is reasonable, I would quite like to have a class/module
that Implements FileHandle, but is (really)
a scalar(string).

A readonly version of this would present
the string as a readable FileHandle, and a writeonly
version would allow writing/printing to a string,
which could be picked up later.

This is by close analogy with ByteArrayInputStream
http://java.sun.com/j2se/1.4.2/docs/api/java/io/ByteArrayInputStream.html
and ByteArrayOutputStream
http://java.sun.com/j2se/1.4.2/docs/api/java/io/ByteArrayOutputStream.html

in Java.

Does such a Module exist? I tried to search CPAN
but didn't really know what search terms to use.

BugBear

smallpond

unread,
Oct 15, 2008, 12:29:45 PM10/15/08
to
On Oct 15, 12:02 pm, bugbear <bugbear@trim_papermule.co.uk_trim>
wrote:

> It is quite common for modules to be able to handle
> files, or filehandles, but not strings.
>
> In order to ease some of the uses I have for
> such modules, in an age where a 1 Mb scalar
> is reasonable, I would quite like to have a class/module
> that Implements FileHandle, but is (really)
> a scalar(string).
>
> A readonly version of this would present
> the string as a readable FileHandle, and a writeonly
> version would allow writing/printing to a string,
> which could be picked up later.
>
> This is by close analogy with ByteArrayInputStreamhttp://java.sun.com/j2se/1.4.2/docs/api/java/io/ByteArrayInputStream....
> and ByteArrayOutputStreamhttp://java.sun.com/j2se/1.4.2/docs/api/java/io/ByteArrayOutputStream...

>
> in Java.
>
> Does such a Module exist? I tried to search CPAN
> but didn't really know what search terms to use.
>
> BugBear


Looks like IO::String is close to what you want.

John Bokma

unread,
Oct 15, 2008, 12:51:08 PM10/15/08
to
bugbear <bugbear@trim_papermule.co.uk_trim> wrote:

> It is quite common for modules to be able to handle
> files, or filehandles, but not strings.
>
> In order to ease some of the uses I have for
> such modules, in an age where a 1 Mb scalar
> is reasonable, I would quite like to have a class/module
> that Implements FileHandle, but is (really)
> a scalar(string).
>
> A readonly version of this would present
> the string as a readable FileHandle,

my $yourstring = "hello, world";
open my $fh, '<', \$yourstring or ...


> and a writeonly
> version would allow writing/printing to a string,
> which could be picked up later.

open my $fh, '>', \$yourstring or ...

"
Since v5.8.0, perl has built using PerlIO by default. Unless
you've changed this (i.e. Configure -Uuseperlio), you can open
file handles to "in memory" files held in Perl scalars via:
"

perldoc -f open


--
John http://johnbokma.com/ - Hacking & Hiking in Mexico

Perl help in exchange for a gift:
http://johnbokma.com/perl/help-in-exchange-for-a-gift.html

bugbear

unread,
Oct 16, 2008, 5:19:34 AM10/16/08
to
John Bokma wrote:
> bugbear <bugbear@trim_papermule.co.uk_trim> wrote:
>
>> It is quite common for modules to be able to handle
>> files, or filehandles, but not strings.
>>
>> In order to ease some of the uses I have for
>> such modules, in an age where a 1 Mb scalar
>> is reasonable, I would quite like to have a class/module
>> that Implements FileHandle, but is (really)
>> a scalar(string).
>>
>> A readonly version of this would present
>> the string as a readable FileHandle,
>
> my $yourstring = "hello, world";
> open my $fh, '<', \$yourstring or ...
>
>
>> and a writeonly
>> version would allow writing/printing to a string,
>> which could be picked up later.
>
> open my $fh, '>', \$yourstring or ...
>
> "
> Since v5.8.0, perl has built using PerlIO by default. Unless
> you've changed this (i.e. Configure -Uuseperlio), you can open
> file handles to "in memory" files held in Perl scalars via:
> "
>
> perldoc -f open

Well, that couldn't be much easier to use, or harder to find!

It *never* occurred to me that it would be that easy, and (thus)
I never looked in perldoc -f open.

Thanks for the (perfect) reply.

BugBear

0 new messages