...and then use open on that scalar to reopen the STDxxx filehandles.
Irrespective of whether your actual issue with open and the tied scalar can be solved (which I believe isn't possible), there's another problem you'd most likely run into, if the idea behind this is to capture stdout/stderr of external programs run via system().
There are two types of file handles in Perl: internal ones, and those associated with a system file descriptor (you can tell them apart with fileno(), which returns -1 for internal file handles). The thing is that handles you've opened to a scalar ref are always internal, i.e. without a system file descriptor. But when system() fork/execs, the child will only inherit those system file descriptors, not Perl internal file handles... In other words, the child's output to stdout/stderr (typically file descriptor 1/2) would never go to the Perl scalar you've opened your handle to.
# this would try to re-use STDERR's file descriptor 2, # which dies with a "Bad file descriptor" error open STDERR, ">", \$variable or die $!; # closing STDERR first 'detaches' STDERR from file descriptor 2 # so it is then being re-opened to an internal file handle printf "fd=%d\n", fileno(STDERR); # 2 close STDERR; open STDERR, ">", \$variable or die $!; printf "fd=%d\n", fileno(STDERR); # -1 (not accessible from system( +)'s child process!)
Update: also see capturing stderr of a command, invoked via backticks.
In reply to Re: Using a tied scalar as an in memory file
by almut
in thread Using a tied scalar as an in memory file
by duncs
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |