http://www.perlmonks.org?node_id=927952

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 { }