in reply to Re^2: Virtual Filehandles..
in thread Virtual Filehandles..

Yes though it isn't as nice. Tie your filehandle you'll write to and trap the writes. It looks like someone already wrote a module for this: Tie::Handle::Scalar. I've never used the module.

Replies are listed 'Best First'.
Re^4: Virtual Filehandles..
by Anonymous Monk on Nov 18, 2004 at 21:28 UTC
    Hmm, I don't think its possible with 5.6.0

    the module you referenced uses a temp file:

    my $tmpfile = $class->{tmpfile} = '.tmp.' . $$; $FILEHANDLE = new FileHandle "$tmpfile", O_RDWR|O_CREAT or croak "$tmp +file: $!"; $class->{FILENO} = $FILEHANDLE->fileno(); $class;
    PG's suggestion is also 5.8.0 specific.

      I didn't know it used a temp file. If I were to build the module to do this I'd use Tie::Handle and overload the writing functions.

      I didn't test this or even try to compile it.

      my $data = tie $fh, 'Tie::Handle::Scalar::WithoutTemp'; print $fh "Hello world"; print $$data; package Tie::Handle::Scalar::WithoutTemp; use base 'Tie::Handle'; sub TIEHANDLE { my $classname = shift; my $data; bless \ $data, $classname; } sub WRITE { my ( $self, $scalar, $length, $offset ) = @_; $$data .= substr $scalar, $offset, $length; 1; } sub PRINT { $$data .= join $,, @_[ 1 .. $#_ ]; 1; } sub PRINTF { $$data .= sprintf $_[1], @_[ 2 .. $#_ ]; 1; }