nysus has asked for the wisdom of the Perl Monks concerning the following question:
I've written a test that inputs keystrokes into the terminal. To do this, I create a fork of my test code. The input from the terminal modifies the object. When my fork returns, however, the object is not modified. How do I return the object from the fork to my original test process?
# CODE: my $obj = ''; lives_ok { $obj = Commander->new(valid_chars => [ qw ( a b c d ) ] ) } + 'Can pass it an array of valid chars'; lives_ok { issue_cmd("abcd\n") } 'Can get a command terminated with ne +w line'; is ($obj->last_cmd, 'abcd', 'got expected input'); # this fails, retur +ns empty string # support methods sub jam { $TIOCSTI = 0x80017472; for (split(//, $_[0])) { ioctl(STDIN, $TIOCSTI, $_) || die "bad TIOCSTI: $!"; } } sub issue_cmd { my $cmd = shift; my $pid = fork(); if (!defined $pid) { die "Cannot fork: $!"; } elsif ($pid == 0) { $obj->get_cmd; exit 0; } else { jam($cmd); waitpid $pid, 0; } }
$PM = "Perl Monk's";
$MCF = "Most CluelessFriarAbbotBishopPontiffDeaconCuratePriest";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Modifying an object in a fork
by 1nickt (Canon) on Mar 11, 2018 at 23:45 UTC | |
by marioroy (Prior) on Apr 29, 2018 at 11:03 UTC | |
by nysus (Parson) on Mar 14, 2018 at 18:05 UTC | |
|
Re: Modifying an object in a fork
by karlgoethebier (Abbot) on Mar 12, 2018 at 11:15 UTC | |
|
Re: Modifying an object in a fork
by Anonymous Monk on Mar 11, 2018 at 22:34 UTC | |
by nysus (Parson) on Mar 11, 2018 at 23:36 UTC | |
by Anonymous Monk on Mar 12, 2018 at 06:09 UTC |