Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
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 { }

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?
[stevieb]: davido thanks for the link ;) I was being the typical lazy
[davido]: date parsing is hard. The more examples you can provide of the input (within reason) and expected output, the better.
[stevieb]: agreed. That's why I said at least a half-dozen. If enough of the different formats are present, the date/time folk may not have to request more. If they do, then at least there was a decent base to start with
[stevieb]: I do date and time transformations in both Perl and Python, but not frequently enough to not have to search for the format params etc ;)

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2017-04-29 02:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (531 votes). Check out past polls.