According to PerlDoc you can direct a file open to a scalar variable https://perldoc.perl.org/functions/open#Opening-a-filehandle-into-an-in-memory-scalar
I am trying to establish if I can use this approach for a temp file that is persistent within the lexical domain. I am doing the following as a test:
my $memfile;
my $filename=\$memfile;
open (OUT, ">", $filename);
print OUT "some data\n";
close (OUT);
I want to be able to read from the scalar later; eg:
open (IN, "<", $filename);
while (my $in=<IN>)
{
print "$in\n";
}
close (IN);
This doesn't work, so am I barking mad or is there a right way to do it so it does work?
perl -wE'open my $fh, ">", \my $mem or die $!; say $fh "hi"; say "var: $mem"'
works fine – Petrieperl -wE'my $mem; my $f = \$mem; open my $fh, ">", $f or die $!; say $fh "hi"; say "var: $mem"'
works as well – PetrieFile::Temp
? – Petrie