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

Wow! thanks for the quick replies! That looks like a very cool way around things, however I'm on 5.6 and can't upgrade, is there another method ?

Replies are listed 'Best First'.
Re^3: Virtual Filehandles..
by diotalevi (Canon) on Nov 18, 2004 at 21:06 UTC
    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.
      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; }
Re^3: Virtual Filehandles..
by ikegami (Patriarch) on Nov 19, 2004 at 00:18 UTC
    IO::Scalar should work with 5.6. It's implementation is not as complete as open(FILE, \$var), but it's often sufficient.