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

Re: Is it possible to write to STDIN ?

by DrHyde (Prior)
on Jul 21, 2014 at 14:54 UTC ( [id://1094513]=note: print w/replies, xml ) Need Help??


in reply to Is it possible to write to STDIN ?

$ foo | bar

'bar's STDIN is whatever 'foo' spits to STDOUT. So if you can control how your code is executed from the shell, you can control its STDIN. For example ...

for i in `seq 1 10`; do echo $i|./myscript.pl; done
will execute your script ten times, each time with a different number on its STDIN.

Replies are listed 'Best First'.
Re^2: Is it possible to write to STDIN ?
by exilepanda (Friar) on Jul 21, 2014 at 15:18 UTC
    Hey, this one is interesting and seem very likely what I am looking for... but please have a look for what I am planning to do, and could you advise can I do this inside the code rather than pipe it from the shell?
    $ENV{REQUEST_METHOD} = "POST"; open F, "testplan.txt" or die $!; while ( chomp ( my $data = <F> ) ) { ### DO ANYTHING TO MAKE $data BECOME STDIN ### # the main() script that reads and handle the form # print final result # next test plan data } close F;
      You could close STDIN and re-open it as a normal filehandle:
      close(STDIN); open(STDIN, 'foo'); print $_ foreach(<STDIN>);
      The documentation for 'open' shows how to save and restore filehandles, and some other dirty tricks you can play.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1094513]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-25 07:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found