Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • … only I’d write the conditional the other way around:

    my $country = $card->country;
    $country = ( $country and $country ne 'gbr' ) ? uc "[$country]" : '';
    • Oops, that’s not actually the same. Well, it might be OK; if zeroes and undefs are not valid values that you need to care about, then that is how I’d write it.

      If you do need to distinguish, though, then I’d put it this way:

      my $country = $card->country;
      $country
          = not $country          ? $country
          :     $country eq 'gbr' ? ''
          :                         uc "[$country]";
      • As for speedbumps... nested ternaries really slow /me/ down! (Except for simple 1-on-1 mappings.)
        • Did you miss the part where I said this complex solution is only necessary if undef and zero need to be preserved? Sure this one is complex – because a three-way condition is always complex.

          • It is never necessary. Every such nested ternary can be written more verbosely, although you may have to use temporary variables.

            my $country = $card->country;
            if ($country) {
                $country = uc "[$country"];
                $country = "" if $country eq "[GBR]";
            }

            (I swapped the uc and gbr check around, because it makes the code prettier IMO.)

            Though what's really needed in this code, regardless of the Perl syntax used, is a comment stating its intention. Perhaps:

            # Don't mention the country if it's u

            • Every such nested ternary can be written more verbosely, although you may have to use temporary variables.

              Didn’t you just exclaim in another comment that you refuse to have this as a default style? Personally I find nested ternaries, if they’re laid out like a table, much easier to read than imperative code with a pile of temporary variables doing the same thing. Much easier to maintain, too.

              Though what’s really needed in this code, regardless of the Perl syntax used, is a comment st

              • Yes, I did. But I would pick such a verbose style over short ?: tables.

                As for comments: they provide redundancy. When the comment and the code are no longer in sync, that's a good sign something's wrong, and that helps to write good code.