Magic <> makes STDIN unusable - How to fix?

V

Vorxion

If someone could please help me? I'm having issues with the <> operator.
I've read the docs on it, tried using close(ARGV), re-opening STDIN to "-",
and a slew of other things.

The upshot of this is that if I'm using <> and I supply @ARGV filenames,
Curses' wgetch() works fine. However, if I give input via STDIN instead,
I continuously get -1 on that function forever, which simply makes my
larger program unusable and entirely unresponsive.

Can anyone help, please? I've read up on eof(), perlop's section on <>,
and nothing is helping so far. I suspect it's possible, but it's gotta be
tricky, whatever the fix.

Sample code:

#!/usr/local/bin/perl

use strict;
use warnings;

$| = 1;

my @file_lines = <>; # Use filename in @ARGV, it's fine.
# Use a pipe or redirect, and getch()
# never works correctly.
use Curses;
initscr;
my $win = new Curses;
$win->clear;
$win->refresh;
$win->keypad(1);
noecho;
$win->addstr(0,0,"Enter keystroke: ");
$win->move(0,19);
$win->refresh;
while (my $keypress = $win->getch) {
$win->addstr(2,0,"Got key |${keypress}|.\n") if defined(${keypress});
}
endwin;
exit;
 
S

Steven Kuo

If someone could please help me? I'm having issues with the <> operator.
I've read the docs on it, tried using close(ARGV), re-opening STDIN to "-",
and a slew of other things.

The upshot of this is that if I'm using <> and I supply @ARGV filenames,
Curses' wgetch() works fine. However, if I give input via STDIN instead,
I continuously get -1 on that function forever, which simply makes my
larger program unusable and entirely unresponsive.

Can anyone help, please? I've read up on eof(), perlop's section on <>,
and nothing is helping so far. I suspect it's possible, but it's gotta be
tricky, whatever the fix.




STDIN got redirected and now you need access back to
the controlling terminal?


Sample code:

#!/usr/local/bin/perl

use strict;
use warnings;

$| = 1;

my @file_lines = <>; # Use filename in @ARGV, it's fine.
# Use a pipe or redirect, and getch()
# never works correctly.



# Assuming you have the requisite OS, try adding this:

open (STDIN, "+</dev/tty")
or die $!;

# before the rest of your code using Curses.pm
 
V

Vorxion

STDIN got redirected and now you need access back to
the controlling terminal?
Yup.

# Assuming you have the requisite OS, try adding this:

open (STDIN, "+</dev/tty")
or die $!;

# before the rest of your code using Curses.pm

Worked like a charm, but why +< instead of just < ? You only read from
STDIN...

Thanks for the fix!
Hope this helps,

Indeed it did, thanks!

--
Vorxion - Founder of the knocking-shop of the mind.

"You have it, you sell it, you've still got it--what's the difference?"
--Diana Trent, "Waiting for God", on why a modelling agency is really a
knocking-shop. Applied by me to the field of consulting. :)

The Sci-Fi fan's solution to debt: Reverse the polarity on your charge card.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top