AHA! I just ran into the same problem in some code I'm writing: I had assigned to the "in memory" variable after opening it. Turns out you can only concatenate to it, otherwise you have to seek to the beginning. Some examples:

# Save old STDIN and STDOUT open my $oldin, '<&', \*STDIN or die "Can't dup STDIN:$!"; open my $oldout, '>&', \*STDOUT or die "Can't dup STDOUT:$!"; # set up new STDIN my $invar = "foo\n"; close STDIN; open STDIN, '<', \$invar or die "Can't open scalar:$!"; # set up new STDOUT my $outvar; close STDOUT; open STDOUT, '>', \$outvar or die "Can't open scalar:$!"; # Try it out! print <>; print STDERR $out; # prints "foo" # But beware: # $invar = "bar\n"; #BAD! try this instead: $invar .= "bar\n"; print STDERR <>; #Would have given your manpage. Now works! # This might work, too, but I'm not sure: $invar = "baz\n"; seek STDIN, 0, 0; print STDERR <>; #Not tested, should print "baz\n"; # Another caveat about STDOUT: $outvar = ""; #This does not do what you think. print "ding\n"; print STDERR $outvar; #prints same as "foo\nding\n" # instead, do this: seek STDOUT, 0, 0; print "dong\n"; print STDERR $outvar; # this does print "dong\n" only. # Now restore old filehandles: close STDIN; open STDIN, '<&', $oldin or die "Can't redup STDIN\n"; close $oldin; close STDOUT; open STDOUT, '<&', $oldout or die "Can't redup STDOUT\n"; close $oldout;

UPDATE: the seek on STDIN works just fine. I decided to go that route because it scales better (always concatenating to my input scalar would keep increasing my memory usage.

print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))

In reply to Re^3: Reassign STDOUT/STDERR contents to a variable by bv
in thread Reassign STDOUT/STDERR contents to a variable by lothos

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.