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 {
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.