Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: In memory filehandles

by crouchingpenguin (Priest)
on Jan 27, 2006 at 01:45 UTC ( #525884=note: print w/replies, xml ) Need Help??

in reply to In memory filehandles

Use IO::Scalar for this.
#!/usr/bin/perl use strict; use warnings; use IO::Scalar; my $data = 'some sample data here'; my $fh = IO::Scalar->new(\$data); print {*STDERR} 'ref $fh yields ', ref $fh, "\n"; print_prop('$fh->can("opened")', $fh->can('opened')); print_prop('$fh->can("seek")', $fh->can('opened')); print_prop('$fh->can("seekable")', $fh->can('opened')); print_prop('$fh is IO::Scalar', $fh->isa('IO::Scalar')); print_prop('$fh->can("opened")', $fh->can("opened")); print_prop('$fh->opened()', $fh->opened()); print_prop('$fh->can("seek")', $fh->can("seek")); eval {print_prop('$fh->seek(0, 0)', $fh->seek(0, 0));} or print "$@"; print_prop('$fh is tied', tied $fh); close $fh; sub print_prop { my ($msg, $flag) = @_; print {*STDERR} $msg, '? ', ($flag ? 'yes' : 'no'), "\n"; return; }
which yields
ref $fh yields IO::Scalar $fh->can("opened")? yes $fh->can("seek")? yes $fh->can("seekable")? yes $fh is IO::Scalar? yes $fh->can("opened")? yes $fh->opened()? yes $fh->can("seek")? yes $fh->seek(0, 0)? yes $fh is tied? no

"Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."

Replies are listed 'Best First'.
Re^2: In memory filehandles
by polettix (Vicar) on Jan 27, 2006 at 09:26 UTC
    crouchingpenguin, thanks for the suggestion. I don't like the IO::Scalar solution very much, for the same reasons I don't like using IO::String in perl 5.8.x: in-memory filehandles should work out of the box (at least this is what I thought). I tried to code a quick solution here just to work around the problem without the need to install yet another module.

    There is also another reason why I don't like IO::Scalar very much: it does not work with Archive::Zip. This is where all really started: you can't pass an in-memory filehandle to Archive::Zip, simply because it contains a seekability test that will fail. And, sadly enough, it will explicitly fail for IO::Scalar as well.

    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://525884]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2017-05-30 02:48 GMT
Find Nodes?
    Voting Booth?