Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Re: while (1==1) results in subroutine not called

by zakzebrowski (Curate)
on Sep 30, 2003 at 20:26 UTC ( [id://295436]=note: print w/replies, xml ) Need Help??


in reply to Re: while (1==1) results in subroutine not called
in thread while (1==1) results in subroutine not called

So ya'll know, sometimes (like when I wrote car talk the other day) when I just breifly described a problem, someone imedatly came back with that the problem was or similar circumstances... Anyways, I attempted an approximate code snipet below...

for my $x (0..4){ # pesudo calling function system("(sleep 5); touch done_" . $x); } &wait(4); sub wait{ my $c = shift; my $i=0; while ($c != $i){ # If I make this $c==$i, returns no problem for my $j (0..$c){ if (-e "done_" . $j){ unlink "done_" . $j; $i++; } } } }

Now there's nothing above which is screaming to me that the sub routine wait won't be called, but apparently it never is...



----
Zak
undef$/;$mmm="J\nutsu\nutss\nuts\nutst\nuts A\nutsn\nutso\nutst\nutsh\ +nutse\nutsr\nuts P\nutse\nutsr\nutsl\nuts H\nutsa\nutsc\nutsk\nutse\n +utsr\nuts";open($DOH,"<",\$mmm);$_=$forbbiden=<$DOH>;s/\nuts//g;print +;

Replies are listed 'Best First'.
Re: while (1==1) results in subroutine not called
by Abigail-II (Bishop) on Sep 30, 2003 at 20:52 UTC
    Of course the while loop never terminates. You make five files, then call &wait (a name I would never have picked since wait is a keyword) with argument 4. In the loop, you delete 5 files, increasing $i with 1 for each file deleted. So, $i equals 5, $c equals 4. And 4 will never be 5.

    Abigail

Re: Re: Re: while (1==1) results in subroutine not called
by antirice (Priest) on Sep 30, 2003 at 20:42 UTC

    Note that all the system calls don't return until what it runs has exited (thus why system returns the exit status of what was called). Also, you have a small counting problem. The array 0..4 contains 5 elements. All of this combines to create a situation where on the first iteration of the while, 4!=0 is tested and, since all the files were created earlier with the system calls, 4!=5 is tested each time thereafter. Perhaps testing for $c >= $i? Perhaps something like:

    for my $x (0..4){ # pesudo calling function system("( sleep 5 ; touch done_$x ; ) &"); } &wait(4); sub wait{ my $c = shift; my $i=0; while ($c >= $i){ for my $j (0..$c){ if (-e "done_" . $j){ unlink "done_" . $j; $i++; } } } }

    Hope this helps.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

Re: Re: Re: while (1==1) results in subroutine not called
by hardburn (Abbot) on Sep 30, 2003 at 20:42 UTC

    Step back from your code and figure out what the real problem you're trying to solve is. It appears that you just want your program to wait a while. Which is what sleep is for. Not the shell program, mind you--Perl has this functionality built in.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      The "sleep" program was just an example... What I want to do (and I forgot to put in the & ) was for a background programs to "do something". I want a notification to know when all of the programs have exited, so I can scp any output files back to the orignal host... (Which I have working, just not when I launch a bunch of background programs...) I'll figure it out..


      ----
      Zak
      undef$/;$mmm="J\nutsu\nutss\nuts\nutst\nuts A\nutsn\nutso\nutst\nutsh\ +nutse\nutsr\nuts P\nutse\nutsr\nutsl\nuts H\nutsa\nutsc\nutsk\nutse\n +utsr\nuts";open($DOH,"<",\$mmm);$_=$forbbiden=<$DOH>;s/\nuts//g;print +;
Re: Re: Re: while (1==1) results in subroutine not called
by eric256 (Parson) on Sep 30, 2003 at 20:42 UTC

    I'm just shooting in the dark here but wouldn't...

    for my $x (0..4){ # pesudo calling function system("(sleep 5); touch done_" . $x); } &wait(4); sub wait{ my $c = shift; for my $i (0..$c) { for my $j (0..$c){ if (-e "done_" . $j){ unlink "done_" . $j; } } } }

    Work just the same? I'm assuming that you have some use for deleteing the same files 4 times, but its unlcear from this.

    ___________
    Eric Hodges

Log In?
Username:
Password:

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

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

    No recent polls found