Perl unless

Perl if the control structure, the conditional expression is true if and only if a piece of code is executed. If you want the block if the condition is false when implemented, if at this time can be changed unless

 

E.g:

unless ($fred =~ /^([A-Z_]\w*$/i) {
  print "The value of \$fred doesn't look like a Perl identifier name. \n";
}

 

Use unless means that either condition is true, or execute a piece of code. It seems that the use control structures to determine if the opposite conditions. Another argument is that it is similar to an independent else clause. That is, when not read unless a statement can be used if the following statement instead:

if ($fred =~ /^([A-Z_]\w*$/i) {
  //什么都不做
} else {
   print "The value of \$fred doesn't look like a Perl identifier name. \n";
}

 

Doing so has nothing to do with the level of operating efficiency, two way system should be translated into the same internal byte code. Also a method of rewriting, is inverted to the condition operator to deny!:

if ( ! ($fred =~ /^([A-Z_]\w*$/i) ) {
  print "The value of \$fred doesn't look like a Perl identifier name. \n";
}

 

unless incidental else clause

In fact, even if it is also possible to use else statement unless the structure, while supporting the syntax, but could lead to confusion:

#!/usr/bin/perl -w
unless ($mon =~ /^Feb/) {
  print "This month has at least thirty days.\n";
} lese {
  print "Do you see what's going on here?\n";
}
#如果用if语句我们可以写成这样:
if ($mon =~ /^Feb/) {
  print "Do you see what's going on here?\n";
} else {
  print "This month has at least thirty days.\n";
}

 

Guess you like

Origin www.cnblogs.com/lelin/p/11622093.html