in reply to the greedy diamond, or leggo my STDIN

You're redirecting STDIN so you can't read the user's input from there.

If you're on a POSIXy system you can read user input from /dev/tty:

open T,"<","/dev/tty" or die "Can't read from /dev/tty: $!"; print "What do you want to do with this message?"; my $response = <T>; print $response;

Replies are listed 'Best First'.
Re^2: the greedy diamond, or leggo my STDIN
by headybrew (Beadle) on Jul 03, 2007 at 20:20 UTC
    Ah hah! I just did this.

    close(STDIN); open(STDIN,"<","/dev/tty") or die "Can't reopen STDIN from /dev/tty: $ +!";

    I don't know if it's any better or worse than using a new filehandle, but it keeps me happy.

    BTW: What would I do on a non-posix system?

      I don't know if it's any better or worse than using a new filehandle, but it keeps me happy.

      It's exactly as the same IMHO and in both ways it keeps me unhappy: I find your way to do what you want to do clumsy and error prone, which is probably the reason why I couldn't understand your question at all, first.

      BTW: What would I do on a non-posix system?

      But seriously, why don't you use <>'s magic instead?

      q:~/tmp [22:37:14]$ cat headybrew.pl #!/usr/bin/perl chomp(my @msg =<>); print @msg, "\n"; print "What do you want to do with this message?"; $response = <STDIN>; print $response; q:~/tmp [22:37:17]$ ./headybrew.pl 'ls|' headybrew.plheadybrew.pl~ What do you want to do with this message?foo foo
Re^2: the greedy diamond, or leggo my STDIN
by headybrew (Beadle) on Jul 03, 2007 at 20:06 UTC
    Thanks.

    Sounds like that would work, but isn't there a way to just switch my STDIN back to the keyboard?

      Sounds like that would work, but isn't there a way to just switch my STDIN back to the keyboard?

      Nope, because it's not been switched forth. It has been switched away once and for all out of your program. Of course you can reopen STDIN to whatever you want, if you like:

      q:~/tmp [22:27:50]$ cat headybrew.pl #!/usr/bin/perl chomp(my @msg =<>); print @msg, "\n"; print "What do you want to do with this message?"; close STDIN; open STDIN, '<', '/dev/tty' or die "Can't read from /dev/tty: $!\n"; my $response = <STDIN>; print $response; q:~/tmp [22:27:58]$ ls | ./headybrew.pl headybrew.plheadybrew.pl~ What do you want to do with this message?foo foo q:~/tmp [22:28:08]$

      But then you rely on something hardcoded that you know or want to be the keyboard. Because from within your program you have no notion of "what" the keyboard would have been, had STDIN not been redirected by the shell.