Again I found myself inspired by a blogs.perl.org post that demonstrate how to let a computer print stuff as it was reasoning on what it was printing, that is a movie-like message reader. In other words, a king of char-by-char cay(1) implementation. So, how difficult could it be to do it in Perl 5?
And here it is my solution:
#!/usr/bin/perl
use v5.10;
use Time::HiRes qw( usleep );
open my $fh, $ARGV[0] || die "\nCannot open input file\n$!";
$/ = \1; # set one char at time (input record separator)
$| = 1; # force autoflush
while ( <$fh> ){
print;
usleep( ( /[a-zA-Z]/ ? 1 : ( /[.!?\n]/ ? 10 : 2 ) ) * 75000 );
}
As you can imagine it is pretty simple: it does read an input file one char at a time, placing it into the topic variable ($_) and printing it on the screen (using autoflush, of course). Then I wait for a quite random time depending on the fact the char was a letter or something else. In this way I can simulate someone is really writing down the message, stopping after a dot or anew line.
I have to use millisecs sleep in order to make it usable at all, since perl func sleep accepts only integers, and printing a message one second at time is a little dummy!
1 commento:
https://metacpan.org/pod/String::KeyboardDistance can help getting the timings realistic
Posta un commento