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

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

Replies are listed 'Best First'.
Re^7: Reassign STDOUT/STDERR contents to a variable
by Anonymous Monk on Sep 27, 2009 at 02:19 UTC
    It is unrelated to eval, and truly is a WTF moment , and it prints PerlIO.pm.

    I've confirmed on win32 with perl v5.8.9, v5.10.0, v5.10.1

    #!/usr/bin/perl -- use strict; use warnings; close(STDIN); open( STDIN, '<', \my $stdin ) or die "$!\n$^E"; my $script_input = 'blah'; $stdin .= $script_input; while (<>) { print; }
Re^7: Reassign STDOUT/STDERR contents to a variable
by Anonymous Monk on Sep 27, 2009 at 02:24 UTC
    The bug goes away if you load "use PerlIO::scalar;"
      Confirmed. Thank you.
Re^7: Reassign STDOUT/STDERR contents to a variable
by ikegami (Patriarch) on Sep 27, 2009 at 03:23 UTC
    Apparently, an internal call to open (by an internal call to require) reused fd 0 (since you closed it), and something is referencing fd 0 rather than *STDIN.