...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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.