in reply to This is why Perl is so frustrating

I would replace that rather clunky

if (($file ne ".") or ($file ne "..")) {

with the more elegant

unless ( $file eq "." || $file eq ".." ) {

because it reads more naturally. In fact, since parsing the file is the only thing that's happening inside the loop, I would just skip the file entirely with

next if ( $file eq "." || $file eq ".." );

There might be responses to my post for calls to make a regular expression out of this, but it's not really necessary.

Alex / talexb / Toronto

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

Replies are listed 'Best First'.
Re^2: This is why Perl is so frustrating
by wfsp (Abbot) on Jul 30, 2009 at 07:08 UTC
    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? :-)

      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