in reply to Re: This is why Perl is so frustrating
in thread This is why Perl is so frustrating

For me
unless ($expression){ # do something }
is never elegant. I'll always plump for
if (not $expression){ # do something }
especially if, as in this case $expression includes some boolean arithmetic. I do use it for early exits from loops though. I can read
while (<$fh>){ next unless /\S/; # }
without too much trouble (as long as it's simple).

As soon as an expression has a not an and and an or in it I'm almost sure to get it wrong and it often points to a poor algorithm which some refactoring can fix. Either that or put it into sub so I can say

if (work_to_do($args)){ # do some work }
and test the living daylights out of it.

Did I mention I've had awful trouble with boolean arithmetic? :-)

Replies are listed 'Best First'.
Re^3: This is why Perl is so frustrating
by talexb (Chancellor) on Jul 30, 2009 at 13:58 UTC

    We'll have to agree to diagree on this one. I find

    if ( !$boolean1 and !$boolean2 ) {
    harder to read, and therefore more difficult to comprehend, than
    unless ( $boolean1 or $boolean2 ) {
    I make this distinction because the original code was checking *two* conditions, not just one. I will agree that it's a saw-off when comparing
    if ( !$boolean ) {
    and
    unless ( $boolean ) {
    But for me, if I miss the tiny '!' character, then I have the logic backwards, which is a huge problem. I'm not going to miss the 'unless'.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds