Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re (tilly) 1: How to preserve the value of STDIN

by tilly (Archbishop)
on Sep 22, 2000 at 03:22 UTC ( #33587=note: print w/replies, xml ) Need Help??


in reply to How to preserve the value of STDIN

Ick.

Untested:

use IO::Scalar; my $data; tie *HACK, 'IO::Scalar', \$data; print HACK <STDIN>; tied(*HACK)->setpos(0); *STDIN = *HACK;
Now if you don't destroy anything, reading from STDIN gives you what is already in $data which is what was in STDIN.

UPDATE
The idea was good but apparently copying tied stuff like that does not maintain the tie. (I shoulda known that, sorry.) Try this code instead:

use IO::Scalar; $data = join '', <STDIN>; my $s; tie *STDIN, 'IO::Scalar', \$s; print STDIN $data; tied(*STDIN)->setpos(0);
I tested this by adding
print $data; print <STDIN>;
after it, running it, and typing into it.

My apologies for the initial mistake.

Replies are listed 'Best First'.
(Ovid) RE: Re (tilly) 1: How to preserve the value of STDIN
by Ovid (Cardinal) on Sep 22, 2000 at 03:58 UTC
      I don't understand why it works, this is pure magic

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://33587]
help
Chatterbox?
[ikegami]: That should be ${ $hash{$key} }[$_] because it's $a[$_] when you don't use a ref.
[oiskuu]: James, that's an array slice you have there (over some hash value)
[ikegami]: Mini-Tutorial: Dereferencing Syntax

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2017-04-27 19:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (512 votes). Check out past polls.