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.