in reply to Not Using So Many 'if' Statements

Why not:
for(@host){ $stout = run($_); exit unless($stout == 256); print "$_ not responding, trying next host\n"; } print "all hosts not responding";

Replies are listed 'Best First'.
Re: Re: Not Using So Many 'if' Statements
by CountZero (Bishop) on Jan 09, 2003 at 20:34 UTC

    exit is not really the to be preferred way of leaving a loop, as it completely exits the program and not only the loop.

    One should indeed rather use last.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      Right... last will break me out of my loop... but why not just exit as soon as I've accomplished the purpose of the script instead of adding an extra command?

      Update: Here's what I mean:
      Adding the last statement would require an additional conditional:
      my $success; for(@host){ $stout = run($_); unless($stout == 256) { $success++; last; } print "$_ not responding, trying next host\n"; } print "all hosts not responding" unless $success;

        Indeed, no reason why you should not use exit to kill the program if you're done. I was just pointing to the side-effects of exit: you may get more more than you bargained for.

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Re: Not Using So Many 'if' Statements
by Dru (Hermit) on Jan 09, 2003 at 20:29 UTC
    Thank you, I knew there was MTOWTDI