in reply to In memory filehandles

The method-calls-on-filehandles work because perl makes any filehandle an IO::Handle (or a FileHandle if you've loaded FileHandle, but that's just a wrapper around IO::Handle now I think). But IO::Handle doesn't "handle" everything; in particular, it's not an IO::Seekable. Because it perl does this across the board for every filehandle, it can't assume that it is seekable.

Replies are listed 'Best First'.
Re^2: In memory filehandles
by polettix (Vicar) on Jan 26, 2006 at 13:15 UTC
    So, this basically means that there aren't any hooks to plug IO::File instead of IO::Handle where needed. As a workaround, I'm using something like this:
    sub open_memory { my ($mode, $scalar_ref) = @_; open my $fh, $mode, $scalar_ref or die "open(): $!"; my $retval = IO::File->new_from_fd($fh, $mode) or die "IO::File->new_from_fd() failed"; return $retval; } ## end sub open_memory
    This makes clear that in-memory filehandles aren't a real substitute for IO::String-s as I used to believe.

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

    Don't fool yourself.
      Or you could just use FileHandle;. Or you could push @IO::Handle::ISA, "IO::File". Or call $fh->IO::File::seek(...).

      Except for giving easy access to the per-filehandle punctuation variables, I think the whole "let's pretend filehandles are objects" thing is pretty silly.

        I generalised an actual problem I had: using in-memory filehandles with Archive::Zip. Thus, I'm stuck to passing something that a third party library likes, which basically eliminates the third solution. The second solution seems a bit overkill and not scalable, but it could come handy with proper localisation I think. The first... I'll give it a try.

        I'm also thinking about blessing the filehandle directly to IO::File, and see what happens.

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

        Don't fool yourself.