Page 1 of 1

Perl: "Linux::Input" and problem with IO redirecti

Posted: Tue Feb 21, 2006 3:42 pm
by ^rooker
[PROBLEM]
When polling events using the Perl module "Linux::Input", the default output gets shifted to something else. This results in problems when redirecting the output of this script.

Example:

Code: Select all

#!/usr/bin/perl



use Linux::Input::Joystick;
my $js = Linux::Input::Joystick->new("/dev/js0");

while (1) {
  my @event = $js->poll(0.01);
  printf("ding dong\n");
}

Code: Select all

$> ./myscript.pl
outputs "dingdong"

Code: Select all

$> ./myscript.pl > test.txt
...leaves us with an empty (0 byte size) test.txt.



[SOLUTION]
According to a friend of mine, the "selector" in "Linux::Input" shifts the default output to something else during polling, but doesn't set it back.

Code: Select all

#!/usr/bin/perl



use Linux::Input::Joystick;
my $js = Linux::Input::Joystick->new("/dev/js0");

while (1) {
  my @event = $js->poll(0.01);
  printf STDOUT "ding dong\n";
}
redirecting the output of this version to "test.txt" works fine.


NOTE: Actually, I'm not really sure if that's a bug in Linux::Input or not.

I was wrong - Problem still NOT fixed

Posted: Tue Feb 21, 2006 4:21 pm
by ^rooker
I'm sorry, but simply using print STDOUT "blabla..\n" didn't solve this problem.

Even more confusing is that filehandles seem to be broken at all when calling "$js->poll()":

Code: Select all

#!/usr/bin/perl

use Linux::Input::Joystick;
my $js = Linux::Input::Joystick->new("/dev/js0");

print "hello\n";
open $fh, ">test.txt" or die "broken: $!\n";



while (1) {
  print $fh ".\n";
  my @event = $js->poll(0.01);
  select STDOUT;
  print STDOUT ":\n";
}
This outputs a lot of ":" and should write "." to test.txt, but it doesn't.

Hahaaa! I think I've solved it!

Posted: Tue Feb 21, 2006 5:15 pm
by ^rooker
[MAYBE the PROBLEM]
I think that the problem is not really with Linux::Input or IOSelect - but it's my infinite loop that's causing a problem!

Because my main intention was to read input from a joystick device, I did "while (1)" and killed it with Ctrl+C. Unfortunately stdout was not flushed before, so my "print" outputs stayed in the buffer which never got flushed.



[MAYBE the SOLUTION]
If my perl would be better, I'd flush the buffer to STDOUT manually after each incoming joystick event, but that's the only thing I found for flushing:

Code: Select all

autoflush STDOUT 1;
I've added this to my code and now it seems to work. whooopee!!