Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm having trouble saving and restoring STDIN in a script I'm working on. For part of the script I redirect STDIN, so before I do that I save it's location:
local *REAL_STDIN; open(REAL_STDIN, ">&STDIN"); $this->{'_stdin'} = *REAL_STDIN;
Then later I try to restore the stream using:
if (defined($this->{'_stdin'})){ local *REAL_STDIN = $this->{'_stdin'}; if(!(open STDIN, "&REAL_STDIN")){ return -3; } }
And when I try to run it, I get -3. The same approach works fine with STDERR and STDOUT (though in the restore code they need something like:
#<snip> if(!(open STDOUT, ">>&STDOUT")){ #<snip>

Anyone know what I'm doing wrong here?

Replies are listed 'Best First'.
Re: Save/restore STDIN
by Zaxo (Archbishop) on Sep 06, 2003 at 00:22 UTC

    It may be simpler to just localize *STDIN,

    { open local(*STDIN), '<', "whatever" or die $!; # go on with processing, # calls to any sub will think STDIN is # the one you just set up. # ... close STDIN or die $!; } # old STDIN is restored, never been touched.
    Explicit duplication is sometimes necessary, but not usually.

    After Compline,
    Zaxo

Re: Save/restore STDIN
by CountZero (Bishop) on Sep 06, 2003 at 09:53 UTC

    Other than having switched the meaning of ">" and "<", you seem to have trouble with properly using local. Zaxo gave you the right info, but was perhaps somewhat short in explaining.

    The beauty of local is that you do not have to restore the variable (or filehandle in this case) after having changed it.

    Everytime you use local variable, a "new" copy of this variable is made which hides the original variable for the duration of the innermost enclosing scope (this can be a block ({...}), a loop, a subroutine, ...). The "original" variable is still there, only you cannot access it. Once you leave the innermost enclosing scope, the localized value of your variable disappears and the original value is automagically restored.

    So , the easiest way to solve your problem is to enclose that part of your program which needs to have STDIN redirected in a {   } block as shown by Zaxo's code.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      I agree with count zero's suggestion.

      local uses run time scoping, it saves the current content of your variable onto the run-time stack, and then automatically restores it when the thread of executions leaves the scope.

Re: Save/restore STDIN
by Limbic~Region (Chancellor) on Sep 05, 2003 at 19:05 UTC
    Anonymous Monk,
    Simply that STDOUT and STDERR are output >, and STDIN is input <.
    Notice the direction of the arrows.

    Cheers - L~R

Re: Save/restore STDIN
by gmpassos (Priest) on Sep 09, 2003 at 03:41 UTC
    Take a look on that, without local():
    ## save: my $stdin_org ; open($stdin_org,"<&main::STDIN") ; ## redirect: open(main::STDIN,"<$0") ; ##restore: *main::STDIN = $stdin_org ; ## use main:: to really restore the main + STDIN.
    This is just a guess, I haven't testd it yet!

    Graciliano M. P.
    "Creativity is the expression of the liberty".