This bit of code was inspired by a conversation in #macosx on efnet between several new (or soon-to-be) dads, all of which are CS majors. This was written to let them get the basic idea of what to expect :) (note: this is humorous and only mostly realistic). Code compiles.
#!/usr/bin/perl if (!%baby) { sleep $with_mate; } else { if ($baby{'age'} <= 1) { @actions = qw/Crying Sleeping Eating Pooping/; } if ($baby{'age'} == 2) { @actions = qw/Screaming Crying/; } if ( ($baby{'age'} >= 3) && ($baby{'age'} <= 12) ) { @actions = qw/Running Playing "Getting into Trouble"/; } if ( ($baby{'age'} >= 13) && ($baby{'age'} <= 15) ) { if ($baby{'gender'} eq "male") { @actions = qw/Eat Eat Eat Eat Eat Sleep/; } else { @actions = qw/Talk Talk Talk Shop Shop Sleep/; } } if ( ($baby{'age'} >= 16) && ($baby{'age'} <= 20) ) { @actions = qw/Flirt Flirt Flirt Flirt Flirt/; } if ( ($baby{'age'} >= 21) && ($baby{'age'} <= 95) ) { select MATE; fork; } if ($baby{'age'} > 95) { write WILL; bless %children; die; } }

Replies are listed 'Best First'.
Re: Life, in Perl.
by Mr_Person (Hermit) on Jul 21, 2003 at 20:56 UTC

    Okay, here's my go at it. Uses POE so there can be many people "living" at once and creating new children. Reality has been sped up a bit so that each year of a person's life only takes 1/10 of a second. Actually runs too, though you may not want to leave it going for too long as the "population" increases rapidly. :-)

    While this one may be more functional, the OP is a lot more fun to read, partially because I wanted to use strict and warnings which leaves less room for stuff that reads well, but doesn't actually do anything.

    This also has a big friggin comet feature, just press Ctrl+C after it's been running for a while :-)

    Enjoy!

    #!/usr/bin/perl use strict; use warnings; use POE; *MATE = *STDOUT; POE::Session->create( inline_states => { _start => \&_start, make_baby => \&make_baby, }, ); $poe_kernel->run; sub _start { my $kernel = $_[ KERNEL ]; $kernel->alias_set('God'); $kernel->post('God', 'make_baby'); } sub make_baby { my $session = $_[ SESSION ]; POE::Session->create( inline_states => { _start => \&born, _stop => \&die, live => \&live }, args => [ $session ], ); } sub born { my ($heap, $person, $kernel, $init) = @_[ HEAP, SESSION, KERNEL, A +RG0 ]; $heap->{age} = 0; $heap->{gender} = (int(rand(2)) ? 'male' : 'female'); $heap->{person} = $person->ID - 2; $heap->{kids} = int(rand(5)); @{$heap->{actions}} = qw/Crying Sleeping Eating Pooping/; print "Child $heap->{person} born, age $heap->{age}, gender $heap- +>{gender}. Likely to have $heap->{kids} kids. Actions: " . join(', +', @{$heap->{actions}}) . "\n"; $kernel->delay('live', .1, $init); } sub live { my ($heap, $kernel, $init) = @_[ HEAP, KERNEL, ARG0 ]; $heap->{age}++; if ($heap->{'age'} == 2) { @{$heap->{actions}} = qw/Screaming Crying/; } elsif (($heap->{'age'} >= 3) && ($heap->{'age'} <= 12)) { @{$heap->{actions}} = qw/Running Playing "Getting into Trouble +"/; } elsif (($heap->{'age'} >= 13) && ($heap->{'age'} <= 15)) { if ($heap->{'gender'} eq "male") { @{$heap->{actions}} = qw/Eat Eat Eat Eat Eat Sleep/; } else { @{$heap->{actions}} = qw/Talk Talk Talk Shop Shop Sleep/; } } elsif ( ($heap->{'age'} >= 16) && ($heap->{'age'} <= 20) ) { @{$heap->{actions}} = qw/Flirt Flirt Flirt Flirt Flirt/; } elsif ($heap->{'age'} >= 21 && $heap->{'age'} <= ($heap->{'gende +r'} eq 'male' ? 74.1 : 79.5)) { select MATE; @{$heap->{actions}} = 'Making and raising kids'; if(int(rand(5)) == 1 && $heap->{kids}) { print $init->ID . "\n"; $kernel->post('God', 'make_baby') or die $!; $heap->{kids}--; } } print "Person $heap->{person} turned $heap->{age}. Their activiti +es are now: " . join(', ', @{$heap->{actions}}) . "\n"; $kernel->delay('live', .1, $init); if ($heap->{'age'} >= ($heap->{'gender'} eq 'male' ? 74.1 : 79.5)) + { print my $will = "I, Person $heap->{person}, do make and decla +re my last will and testament as follows: ...\n"; $kernel->delay('live'); } } sub die { my $heap = $_[ HEAP ]; print "Person $heap->{person} died.\n"; }

    Update: Slightly restructed with "God" session because the children were keeping the parents alive after they were supposed to be dead :-)

      Very nice, ran effortlessly for me. The only comment I have is that you have "Person 1 turned 79. Their activities are now: Making and raising kids"... I hope that I can still 'make kids' at that age 8^)
      djbiv
Re: Life, in Perl.
by Mr_Person (Hermit) on Jul 21, 2003 at 19:23 UTC

    Hmm, shouldn't that be enclosed in some sort of loop so that when it forks the process starts over? Of course if the code worked and you actually ran it then you'd have a fork bomb :-) Also, according to the CDC, the Life Expectancy of a male is 74.1 and a female is 79.5 (in America anyway) so perhaps the last part of the script should reflect that.

    I know it's just for fun, but I couldn't resist! :-)

      The loop was left out on purpose because that's the way the human race works. There is never a stop to the forking of children. (ok, that just sounds wrong). A child is born (created), grows, and produces children of its own, and so on and so forth until a big friggin comet smashes into the earth (a kill -9, as it were).

      The death age.. uhm.. I'm just being optimistic :)

Re: Life, in Perl.
by ajdelore (Pilgrim) on Jul 21, 2003 at 19:44 UTC

    Maybe a recursive function call would make more sense than I loop, I think. Or, actually a forked process that is called for each new $baby, so that when it is complete it can die (or be killed). :)

    </ajdelore>