in reply to Re^2: Reassign STDOUT/STDERR contents to a variable
in thread Reassign STDOUT/STDERR contents to a variable

Should work, too, in principle, e.g.

my $stdin = "Some input text here\n"; close STDIN; open(STDIN,'<', \$stdin) or die $!; while (my $line = <STDIN>) { print "got line: $line"; } __END__ got line: Some input text here

What are those 10 pages complaining about? :)

Replies are listed 'Best First'.
Re^4: Reassign STDOUT/STDERR contents to a variable
by lothos (Novice) on Sep 25, 2009 at 18:38 UTC
    Yep. Your test code works, but in my script I get this. Ignore the Host not found: on the lines. That's output from my script. Looks like a perlio man page.

    I also failed to mention that the input I'm speaking of might, or might not be binmode. This works as expected:

    open(FILE,'>' . $stdin_file); binmode(FILE); print FILE $script_input; close(FILE); close(STDIN); open(STDIN, '<',$stdin_file);
    This:
    #open(FILE,'>' . $stdin_file); #binmode(FILE); #print FILE $script_input; #close(FILE); close(STDIN); open(STDIN, '<',\$script_input);
    Results in my output being:

      Sorry, I haven't got the foggiest what's going on in your case :)  Honestly. I can't replicate anything similar here.

      binmode should not matter, AFAICT.  Do you get any errors when you add ... or die $! to the various open statements?

        Figured it out. The difference was that in the script that is to be executed via the eval, you can't rely on the special <> anymore it would seem.

        Executing the following code results in printing the PerlIO man page.

        my $script = 'while(<>) { print; }'; close(STDIN); open (STDIN,'<',\my $stdin); my $script_input = 'blah'; $stdin .= $script_input; eval $script;

        Wheras executing this code works fine:

        my $script = 'while(<STDIN>) { print; }'; close(STDIN); open (STDIN,'<',\my $stdin); my $script_input = 'blah'; $stdin .= $script_input; eval $script;

        Any thoughts on how to workaround this? I like <>.