Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: No interactivity (prompt) after reading from pipe

by fizbin (Chaplain)
on Nov 18, 2005 at 15:08 UTC ( [id://509819]=note: print w/replies, xml ) Need Help??


in reply to No interactivity (prompt) after reading from pipe

What everyone else has said so far is right - if you are reading from a pipe, then you cannot simply read from standard input again to read from the terminal.

However, this does not mean that all is lost. On many POSIX systems, you can find the name of the controlling terminal with POSIX::ctermid(); and then open and read from that.

#!/usr/bin/perl -w use strict; use Getopt::Std; use POSIX qw//; my $what; $| = 1; my @hosts; my %opts; while (<STDIN>) { chomp; push(@hosts, $_); } getopts(':dw', \%opts); $what .= ' bla' if $opts{'d'}; $what .= ' blabla' if $opts{'w'}; my $cterm = POSIX::ctermid(); if (! $cterm) {die "No controlling terminal to read prompt from!";} if (! open(CTERM, '<', $cterm)) { die "Couldn't open controlling terminal $cterm: $!"; } print "OK? [Y|n]>\n"; my $yesno = <CTERM>; chomp $yesno; exit 1 if $yesno =~ /^[nN]$/; print "Did not stop, sorry!\n";

Yes, I could have used STDIN there the second time, but I didn't so as to keep it clear where you were reading what from.

--
@/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-24 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found