Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Problems? Is your data what you think it is?
 
PerlMonks  

unless statements

by root (Scribe)
on Nov 05, 1999 at 04:10 UTC ( #947=perltutorial: print w/ replies, xml ) Need Help??

Unless statements allow you to say unless something is true do this. For example:
$value=20;
unless($divisor==0){      #so long as the $divisor isn't equal to 0
  $value=$value/$divisor; #go ahead and divide $value by $divisor
} else {
  $divisor=1;             #otherwise set $divisor to 1
}
If you've got that I suggest you take a look at while loops

Comment on unless statements
what is benefit of unless ?
by Anonymous Monk on Feb 05, 2005 at 11:34 UTC
    I think unless statements are similar to if statements.But if-else stmt gives us more flexibility then unless.So is there any specific use of this . please reply soon. regards, barun parichha
        is this the equivalent code for " unless(condition)" ? if !(condition) { ... ... } else { ... ... } reply soon, barun parichha
        You can use the if/else statement intead of "unless" but it requires more commands of if/else than "unless" and the loop continues as long we required

      It is not similar statement.

      $a = 10; print "if stmt true" if ($a == 10); print "unless" unless ($a == 10);

      Prasad

      $ cat input foo bar $ perl -ne 'print unless /^$/../^$/' input foo bar $ perl -ne 'print if not /^$/../^$/' input foo bar $ perl -ne 'print if ! /^$/../^$/' input foo bar
      The unless statement is equivalent to if not, but is different from if ! due to the associativity and precedence rules covered in perlop. A benefit of this behavior allows the reduction of runs of blank lines to a single blank line.

      taken from here
Re: unless statements
by htoug (Deacon) on Feb 08, 2005 at 11:09 UTC
    It is a matter of taste (and style), but I only use unless without the else-clause. If you have the else, then you could just as well use a normal if and get better readability - it's more like waht everyone is used to.

    But in a standalone if (!(some_complicated_expression)) the use of unless lets you remove one pair of parenthesis and lets the poor smuck reading the code have one less thing to worry about.

    I would rewrite your example as

    $value=20; if ($divisor!=0){ #so long as the $divisor isn't equal to 0 $value=$value/$divisor; #go ahead and divide $value by $divisor } else { $divisor=1; #otherwise set $divisor to 1 }
    Now the statement says the same as the comment (in your code it should have been #unless the $divisor isn't equal to 0 - 'so long' indicates a repetition IMHO).
      Hello , well i think unless is a loop and things like : if ($divisor!=0){ doesn't work rather than using unless if will procedes the action for one time but unless will keep procedes the same things unless the statment goes wrong that's what i think

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-04-21 12:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (495 votes), past polls