Perl questions about re-opening standard input during program operation

Source: Internet
Author: User
Tags perl script

The question is, how does the program run with the standard input using the "<" symbol or the "|" Conforms to a file that is directed at the command line.

However, in the process of running the program to get input from the keyboard.

Because/dev/tty is the console for the current process, it STDIN is entered for the current standard. If redirected, for example:

Perl script.pl <myfile.txt

STDINBe pointed tomyfile.txtBut/dev/ttyStillfrom the control terminal. All UNIX is like this, not just referring to this perl.

Then the solution:

#!/usr/bin/perl-wuse strict;$| = 1;my $stream; while (<>) {$stream. = $_;} Open STDIN, "/dev/tty" or die;print "does you want to process the stream?"; My $ans = <stdin>;chomp $ans;p rint "Got" $ans ' \ n ';p rint "stream = $stream"; #...exit;__end__

Test:

$ echo Foo |./stdin does want to process the stream? Yesgot ' yes ' stream = Foo

Find the standard input to re-point to the keyboard. Problem solving. Good luck, June.

This article is from the "EIT Tramp" blog, please be sure to keep this source http://zicowarn.blog.51cto.com/3815716/1708173

Perl questions about re-opening standard input during program operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.