Perl day 3: unless statement

Going off our if statement yesterday, I found that there’s something called the unless clause. According to the ifelseifelse website, the unless clause is the opposite of an if statement. I like their example of the following two statements being equivalent:
if (!$flag) { do_something();}
unless ($flag) { do_something();}

I’m used to using the ! for doing the opposite of a boolean statement, and probably will be doing that instead of using the unless statement. It is possible that some situations are better to use the unless statement that would provide better readability, like:
#!/usr/bin/perl
$a = 'taco';

unless ('$a' eq 'burrito') {
print "unless $a is a burrito, don't print this \n";
} else {
print "else it's a $a \n";
}

This entry was posted in software and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *