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

As you know, Expect module is very useful. I use it to start/stop a prcess. Sometimes, startup processing takes too long. I tried to send Ctrl^C to abort this processing. I don't know how to send Ctrl characters. thanks,
PerlIsFun
  • Comment on How to send Ctrl^C to a program via Expect module

Replies are listed 'Best First'.
Re: How to send Ctrl^C to a program via Expect module
by Fletch (Bishop) on Jul 22, 2004 at 19:36 UTC

    kill INT => $expect_object->pid();

      Thank you very much. It works very well.
      PerlIsFun
Re: How to send Ctrl^C to a program via Expect module
by beable (Friar) on Jul 23, 2004 at 06:47 UTC

    You should use the soft_close() or hard_close() methods. Please take a look at the documentation for the Expect module. Here's a simple example:

    #!/usr/bin/perl use strict; use warnings; use Expect; my $command = "cat > tmp.txt"; my @params; my $exp = Expect->spawn($command, @params) or die "Cannot spawn $command: $!\n"; print("sending...\n"); $exp->send("Hello world\n"); sleep 1; $exp->hard_close; __END__

    Or, if you really want to send Ctrl-C to your process, try this: $exp->send("\cC");. Or you can just send a signal to the process like Fletch recommends.