mifflin has asked for the wisdom of the Perl Monks concerning the following question:

I was just reading the perldoc on the open (perl 5.8.0) command and noticed the anonymous temp file.
...
File handles can be opened to "in memory" files held
in Perl scalars via:

     open($fh, '<', \$variable) || ..
...
I was wondering if it was possible to do the same with IO::File. I've tried
use IO::File; $var = <<EOT; line 1 line 2 line 3 EOT ###open($fh, '<', \$var) || die $!; # this works # $fh = IO::File->new('<', \$var) || die $!; # but this does not # while ($line = $fh->getline()) { print $line; } $fh->close();
But it does not work.
The error I get is

IO::Handle: bad open mode: SCALAR(0x11eabc) at test.pl line 10

I've read through the perldoc on IO::File and IO::Handle but can find nothing that indicates that this feature is supported.
Is it possble?

Replies are listed 'Best First'.
Re: opening anonymous temp files with IO::File?
by Juerd (Abbot) on May 12, 2004 at 18:37 UTC

           new ( FILENAME [,MODE [,PERMS]] )
               Creates an "IO::File".  If it receives any parameters, they are passed to
               the method "open"; if the open fails, the object is destroyed.  Otherwise,
               it is returned to the caller.
    
    That's not new ( MODE, THING ).

    But no, IO::File does not support opening references. This makes sense, because the module is IO::File and a reference is not a file. See IO::Scalar.

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Re: opening anonymous temp files with IO::File?
by Matts (Deacon) on May 12, 2004 at 19:47 UTC
    You want IO::File->new_tmpfile().
Re: opening anonymous temp files with IO::File?
by Steve_p (Priest) on May 12, 2004 at 18:47 UTC
Re: opening anonymous temp files with IO::File?
by rob_au (Abbot) on May 12, 2004 at 21:02 UTC
    There is a fairly comprehensive overview, albeit, a little dated now, of mechanisms for the employ of temporary files in perl which I wrote at Using Temporary Files in Perl.

     

    perl -le "print unpack'N', pack'B32', '00000000000000000000001011010101'"