in reply to Virtual Filehandles..

If you are using perl 5.8 you can create a filehandle to a plain scalar.

open my $fh, ">:scalar:raw", \ my $stored_output or die "Couldn't open memory handle: $!"; print $fh "Hello world!"; print $stored_output;

Replies are listed 'Best First'.
Re^2: Virtual Filehandles..
by NetWallah (Canon) on Nov 18, 2004 at 20:45 UTC
    Cool !

    On Win32, I had to remove the ":raw" to make it work on perl, v5.8.3 built for MSWin32-x86-multi-thread.

    With ":raw", I got no output (More accurately, NULL output), and warnings produced:
    Use of uninitialized value in concatenation (.) or string at -e line 1.

        Earth first! (We'll rob the other planets later)

Re^2: Virtual Filehandles..
by Anonymous Monk on Nov 18, 2004 at 21:02 UTC
    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 ?
      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.
      IO::Scalar should work with 5.6. It's implementation is not as complete as open(FILE, \$var), but it's often sufficient.