#!/usr/bin/perl use strict; use warnings; use IPC::Open2; use Term::ReadKey; use threads; use threads::shared; my $pid = open2(\*Reader, \*Writer, "driver.exe") or die "Failed to open pipe! $!"; ReadMode('cbreak'); my $state : shared = 0; my $i : shared = 0; my $thr = threads->new(\&read_in)->detach; while (1) { # Continue reading the spam from the driver if ($state == 0) { my $got = ; if (defined $got) { print $got; } } #User paused spam reading, changed value of $i, and #needs to pipe it to the driver, spam output will be updated elsif ($state == 1) { $state = 0; #reset state print Writer "$i\n"; } #User quit! elsif ($state == 2) { print Writer "quit\n"; # Before the driver exits, it does output a message, which # for some reason does not get grabbed here my $goodbye = ; print $goodbye; last; } } ReadMode('normal'); sub read_in { while (1) { my $char = 0; # A key was pressed to change state if (defined ($char = ReadKey(0))) { # 'p' was pressed to signal paramter change, stop # outputting from driver output, get new value from console, # and update it if ($char eq 'p') { $state = 1; # How to make this work? $i = ; chomp $i; } # User quit elsif ($char eq 'q') { $state = 2; } } } } waitpid($pid, 0); __END__