3

I'm trying to test a function that reads from standard input by redirecting the standard input to read from a file, more or less as advised here

Here's the script:

use strict;
use warnings;

use Term::EditLine qw(CC_EOF);

use v5.14;

my $el = Term::EditLine->new('progname');
$el->set_prompt ('# ');

$el->add_fun ('bye','desc',sub { say "\nbye"; return CC_EOF; });
$el->parse('bind','-e');
$el->parse('bind','exit','bye');

*STDIN = *DATA;

while (defined($_ = $el->gets())) {
  say $_;
}

__DATA__;
goo
gar
exit

It's using Term::Editline which you would have to install first. In this case, instead of reading from the (faked) standard input, it drops me into the prompt. The input is designed so that the last one of them, exit, would signal exiting (as indicated in the command above). However, it simply does not take its input from the redirected handle. It might be the case that gets is actually a function defined in an XS, but it might be something completely different. Any help will be appreciated.

jjmerelo
  • 22,578
  • 8
  • 40
  • 86
  • 2
    I think this _might_ be possible by actually reopening STDIN to the DATA file handle (`open(STDIN, '<&', \*DATA)`) but I can't test r/n. That should have effects on the POSIX level, not just on the PerlIO level. This might still fail if the Term::EditLine module has more complex interactions with the terminal than just reading from STDIN. In that case, testing your software as a separate process with something like Expect could be the easiest approach. – amon Dec 02 '18 at 12:32
  • 1
    Try adding the `DATA` handle as input handle to the constructor: `my $el = Term::EditLine->new('progname', \*DATA, \*STDOUT, \*STDERR)` – Håkon Hægland Dec 02 '18 at 15:58

0 Answers0