Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

our + redo = ???

by jptxs (Curate)
on Apr 01, 2001 at 08:35 UTC ( [id://68786]=perlquestion: print w/replies, xml ) Need Help??

jptxs has asked for the wisdom of the Perl Monks concerning the following question:

OK. I was really proud about my "no question" streak. Me and Camel 3 have been snuggling well lately but.... I run this and get:
Use of uninitialized value in foreach loop entry at ./tryMoreKids line + 33, <> line 2. Use of uninitialized value in foreach loop entry at ./tryMoreKids line + 33, <> line 2. Use of uninitialized value in string ge at ./tryMoreKids line 43, <> l +ine 2.
All refer to a var declared with our outside the scope of the loop where the action is and are not myed or localed in any way. AFAICT, it should be fine. The whole point of this code is to figure out how to control the number of children one parent has dynamically - so if you've got a better way to do that by all means let me know. Otherwise, here's my attempt (a prototype) thus far:
use strict; use warnings; our $children; our $new_kids; FORK: { if ( !$children ) { print 'How many children to start with? [5]: '; $children = <>; chomp $children; unless ($children) { $children = '5'; } die "only numbers for kids, please\n\n" if $children =~ /\D/; for ( 1..$children ) { my $child; my $signal = $_; die "Can't fork: $!" unless defined ($child = fork()); if ($child == 0) { # i'm the child! &dummy(); } else { # i'm the parent while ( $signal ge $children ) { &interact(); redo FORK; } } } } else { for ( 0..$new_kids ) { my $child; my $signal = $_; die "Can't fork: $!" unless defined ($child = fork()); if ($child == 0) { # i'm the child! &dummy(); } else { # i'm the parent while ( $signal ge $new_kids ) { &interact(); redo FORK; } } } } } sub dummy() { sleep 90; exit; } sub interact() { print 'Current number of children is: ', "$children", '. i.e. thes +e:', "\n\n"; system('ps -ef | grep tryMore | grep -v grep'); print "\n\n"; print 'Minus one for the parent, of course', "\n\n"; print 'How many children do you want to add? [1]: '; my $new_kids = <>; chomp $new_kids; $new_kids = '0' unless $new_kids; unless($new_kids) { $children += 1; } else { $children += $new_kid +s; } return; }
"A man's maturity -- consists in having found again the seriousness one had as a child, at play." --Nietzsche

Replies are listed 'Best First'.
Re (tilly) 1: our + redo = ???
by tilly (Archbishop) on Apr 01, 2001 at 08:43 UTC
    On line 33 your uninitialized variable was $new_kids. So I looked for that, and I had to wonder if you really meant to write:
    sub interact() { print 'Current number of children is: ', "$children", '. i.e. thes +e:', "\n\n"; system('ps -ef | grep tryMore | grep -v grep'); print "\n\n"; print 'Minus one for the parent, of course', "\n\n"; print 'How many children do you want to add? [1]: '; my $new_kids = <>; # ^^ (Your mistake?) chomp $new_kids; $new_kids = '0' unless $new_kids; unless($new_kids) { $children += 1; } else { $children += $new_kid +s; } return; }

    UPDATE
    In response to the note below. Experience consists of recognizing a mistake the second time we make it. *Learning* is when you can recognize that someone else is making the mistake. When I can answer questions fast, it usually is a good sign that I have spent an hour or more making that very mistake in my own code...

      I am SUCH a dumb-ass. thank you =)

      UPDATE someone asked in the CB why I was using redo to exit a for loop. Just to clarify, it's because this is just a prototype for a larger program and that will not do this. I know it's bad form and figured I should note that here so not to set a bad example =) (as if I already didn't do that with such a dumb-ass mistake...)

      oh, and thanks for the kind words tilly; though, i must say that it's hard to picture you making this mistake in particular =)

      "A man's maturity -- consists in having found again the seriousness one had as a child, at play." --Nietzsche
      /me fumbles to make a painful "New Kids on the Block" pun out of this thread:
      • Is this an instance of "New Kids on the Enclosing Block"? (hmm... no)
      • Out with it, tilly, we know you're a closet New Kids on the Block fan (ad hominem, inciteful hate-speech... next)
      • Duh, they're called "New Kids on the Block", not "New Kids in the Block"! (might be a keeper)
      • Now that's my New Kids on the Block. (being an Abbot was nice while it lasted)
      • Like I've always said, the New Kids on the Block have no value. (ding!)
      • Ah, screw it...
         MeowChow                                   
                     s aamecha.s a..a\u$&owag.print
        Were I a fan of new kids on the block, would I have zeroed in on that as the error so fast?

        Well you said you wanted a painful pun...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-23 09:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found