UNIXDAEMON Small Mosaic

Categories:

/books
/career
/cloud
/events
/firefox
/geekstuff
/linux
/meta
/misctech
/movies
/nottech
/perl
/programming
/security
/sites
/sysadmin
/tools
/tools/ansible
/tools/commandline
/tools/gui
/tools/network
/tools/online
/tools/puppet
/unixdaemon

Archives:

March 20146
February 20141
January 20145
December 20135
November 20132
October 20134
August 20134
July 20132
June 20131
May 20131
April 20131
March 20131
Full Archives


Tue, 27 Mar 2007

Simulating Typing in Perl

You'd think it would be easy - have a program type a previously written program at a human speed (minus the typos). Vim has record and reply functionality but it's done with typical vim efficiency: yes, instantly.

At EuroOSCON a couple of years ago Damian Conway handed out a presentation tidbit, he uses the hand_print function from IO::Prompt to make himself look like a master typist. Well, he could just have been saying that to make us feel better, maybe he can type that fast... Anyway, I tried a simple example using the module:

  
  #!/usr/bin/perl
  use strict;
  use warnings;
  use IO::Prompt qw/hand_print/;

  hand_print("I am not really typing this...");
  

It works but the typing speed is so uniform it makes it obvious over past a handful of lines. So I wrote my own that adds a little randomness to the typing speed, it's not pretty, it does what I want and its output is "Out on the big bad web."

  
  #!/usr/bin/perl
  use strict;
  use warnings;
  use Time::HiRes qw(usleep);
  $|++;

  my $input;

  {
    local $/ = undef;
    $input = <ARGV>;
  }

  $input =~ s/(.)/sleep_and_show($1)/esg;

  sub sleep_and_show {
    print $_[0];
    usleep int rand(200_000);
  }
  

It's a little more jittery, which is more like my typing, and has the nice side effect of a pretty looking invocation - ./seditor file_to_type - which could be a valid command.

Posted: 2007/03/27 19:11 | /perl | Permanent link to this entry


books career cloud events firefox geekstuff linux meta misctech movies nottech perl programming security sites sysadmin tools tools/ansible tools/commandline tools/gui tools/network tools/online tools/puppet unixdaemon

Copyright © 2000-2013 Dean Wilson :: RSS Feed