How to write to a Perl variable as if it's a filehandleSun, Apr 12, 2009 in Open Source Programming
This is really cool. I just learned this: you can treat a Perl variable just like a filehandle (read, write, etc).
my $buffer = ""; open my $fh, ">", \$buffer or die $OS_ERROR; print $fh "hello, world\n";
$buffer contains “hello, world\n”. You can do the same kinds of things when reading from a variable.
I knew you could do it with IO::Scalar, but while refreshing my memory on that, I stumbled upon this – who needs IO::Scalar anymore?
This is going to make a lot of tests in Maatkit easier to write.
I'm Baron Schwartz, the founder and CEO of VividCortex. I am the author of High Performance MySQL and many open-source tools for performance analysis, monitoring, and system administration. I contribute to various database communities such as Oracle, PostgreSQL, Redis and MongoDB.