zrzero has asked for the wisdom of the Perl Monks concerning the following question:

I have this code, and I need the output of 'it begins' and 'it ends' not to rely on any type of input. As it is written now, they are printed after something is entered instead of when the dertermining time passes by. Also, I need to have input still open, I can get the output to be displayed without the use of any input in the code, but that is not my point. Here's what the code looks like:
while (time) { $Seconds = substr(time,-3,1); if ($Seconds == 4&&$Factor != 1) { print "It begins."; $Factor = 1; } elsif ($Seconds == 5&&$Factor == 1) { print "It ends."; $Factor = 0; } $Input = <STDIN>; chomp($Input); if ($Input eq "time") { print time; print "\n"; } elsif ($Input eq "quit") { exit; } }
Some help or suggestions on cleaning this up would be nice... hehe.

Replies are listed 'Best First'.
Re: Input/Output Problem
by acid06 (Friar) on Sep 09, 2001 at 13:11 UTC
    I think there's only one *portable* way of performing a non-blocked read which is using Term::ReadKey. So, you might want to give it a shot.

    acid06
    perl -e "print pack('h*', 16369646), scalar reverse $="
      There's two different types of "portable" here. (Well, one's portability and the other's extensibility, but it's Monday and I like making words dance for me.)

      Term::ReadKey is portable across platforms, like Unix vs. Windows, if you know you will only want I/O from the keyboard.

      If you're on Unix and may want input from a socket as well as the keyboard, you can use select and bind STDIN and a socket. There are a few nodes that deal with this. (I'm lazy ... you can use SuperSearch. *grins*)

      If you want to be completely portable in both ways, then you can finagle the select on Windows if you're willing to telnet to yourself. (Again, there's a node on this somewhere on the monastery. *winks*)

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.