Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

small text game with fork problem

by Anonymous Monk
on Sep 26, 2011 at 21:14 UTC ( #927952=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all.

I don't know why this "game" came to me but I had some time to waste and thought it'd be funny.

Basically have two scripts, 1.pl and 2.pl. Both are identical in nature except they both call each other. I begin the script by calling perl 1.pl.

The idea is the script counts, then passes that number to the other script and then it ++s it and counts, then passes it back and forth. Each one counts then passes. However you can type during it.

The problem is I can't get $cmd_cnt to add. Each time you type something and hit enter I want it to ++ so it displays the next @funny. This is the only part I can't get to work, each time I enter something it goes back to 0.

Can you help?
use warnings; use strict; my $pid = fork(); my $cnt = shift; my $cmd_cnt = shift; defined($cnt) or $cnt = 0; defined($cmd_cnt) or $cmd_cnt = -1; my @funny = ("shut the hell up, I'm counting here!", "you talk too muc +h, human!", "if you speak again I'll hurt you!", "die die die die die + diedie die die die die diedie die die die die diedie die die die die + diedie die die die die diedie die die die die diedie die die die die + diedie die die die die diedie die die die die diedie die die die die + diedie die die die die diedie die die die die diedie die die die die + diedie die die die die diedie die die die die diedie die die die die + diedie die die die die diedie die die die die die", "there you said +something, happy now?"); if ($pid) { #parent process while(1) { $cnt++; print "PC1: $cnt\n"; sleep 1; system("perl 2.pl $cnt $cmd_cnt") or die "Error $!"; exit; } } elsif (defined($pid)) { #child process while (my $command = <STDIN>) { chomp($command); $cmd_cnt++; print "you said: $command\n"; print "PC1: $funny[$cmd_cnt] $cmd_cnt\n"; sleep 1; system("perl 2.pl $cnt $cmd_cnt") or die "Error $!"; exit; } } else { }

Comment on small text game with fork problem
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://927952]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2014-07-31 09:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (248 votes), past polls