Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Not Using So Many 'if' Statements

by insensate (Hermit)
on Jan 09, 2003 at 19:15 UTC ( [id://225616]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

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

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

    No recent polls found