With particular reference to Re^3: How would you do it? Re ARGV, fundamentally!, which is your favourite perfectly legal and well documented (but possibly not terribly known) Perl feature that in your opinion would deserve a comment in "production" code, whatever that is for you?

I'm not fully convinced about the one pointed out by merlyn himself in the above mentioned node, but one possibility I was thinking aobut is the .. in scalar context (see perlop).

Update: s/cmt/comment/ in the text of the node. (Guess it's far too late for the title.)

Replies are listed 'Best First'.
Re: Which is your favourite cmt-deserving Perl feature?
by tlm (Prior) on Jul 21, 2005 at 22:21 UTC

    I figure that if an idiom is so mystifying that it needs a comment, I'd rather not use it. For the most part I use idioms to save myself typing; it defeats this purpose to have to type out comments to explain them.

    the lowliest monk

Re: Which is your favourite cmt-deserving Perl feature?
by siracusa (Friar) on Jul 21, 2005 at 20:38 UTC

    You said it in your original post, but here's the canonical example:

    while(<>) { next if 1..1; # skip first line ... }
      Eeew. Classical example of how not to program. About the first thing I learned when learning how to program efficiently is to take anything out of the loop that doesn't need to be in the loop. If you want to skip the first line, skip it outside the loop, don't test for every line whether it'll be the first line or not.
      <>; # Skip first line. while (<>) { ... }
        Except that this is not an equivalent loop if there is only one line to be read. If you read that one line, @ARGV is now empty, and when you enter the loop, the ARGV loop will now be reading STDIN! Oops! Doh!

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

      Ouch! Not only this is the .. operator in scalar context, which is somewhat esoteric as of itself, but it is a special case of it which is even more esoteric, and used with both limits equal to 1 which further adds to its esoteric nature! Thus: nice example indeed! And nice golfing technique - I'll keep it in mind. But I guess that I would rather write
      next if $. == 1;
      in most other situations...
Re: Which is your favourite cmt-deserving Perl feature?
by diotalevi (Canon) on Jul 21, 2005 at 17:50 UTC
    Comment on what "CMT" is.
      Hmmm "CMT" ne "cmt".

      Leaving aside jokes, I must say that I can't stand too people who spells everything like a dude, a' la "can u plz tell me if u can do this or that b4..." But I thought that "cmt", especially used only once, would have been an acceptable and commonly used abbreviation.

        But I thought that "cmt", especially used only once, would have been an acceptable and commonly used abbreviation.

        Yes, the meaning is clear, but what does this thread have to do with the Country Music Television channel?

Re: Which is your favourite cmt-deserving Perl feature?
by jdhedden (Deacon) on Jul 21, 2005 at 19:07 UTC
    I would comment the following idiom:
    # If not already done, initialize $something using whatever() $something ||= whatever();

    Remember: There's always one more bug.
      jdhedden,
      This idiom can bite viciously. This will assign the value of whatever() to $something if it has a false value. A false value doesn't necessarily mean undefined.
      • 0
      • ''
      • '0'
      • undef
      Future Perl's will have a //= operator which means "defined or" but without a patch, if a defined but false value is valid - you need to write this more like $something = whatever() if ! defined $something

      This is likely a good case to comment since when debugging having a reminder that defined false values may be overwritten.

      Cheers - L~R

      See What is truth? (Curiosity corner) for more details
        This is likely a good case to comment since when debugging having a reminder that defined false values may be overwritten.
        You're perfectly right. But, if one is aware of what he is doing ||= does its job most of the time. Of course we all will welcome //= to cover the remaining cases.

        Occasionally I use ||= on purpose to overwrite a false value (with another "standardized" false value), e.g.:

        $u ||= ''; # or $v ||= 0;
        (Most often this is not necessary either, and -as usual- there are other ways to do it...)
      If used (I do it often), that's something that would be spread widely in the application.

      So if it's used, it would be considered a known idiom and shouldn't be commented everywhere in the code.

      If in a team environment, this would be considered team lore and any newbie could easily just ask someone the first time it's encountered.

      /J

      # If not already done, initialize $something using whatever() $something ||= whatever();

      I have to admit that this is the sort of comment I hate :-) It doesn't tell me anything that the code doesn't.

Re: Which is your favourite cmt-deserving Perl feature?
by Anonymous Monk on Jul 22, 2005 at 15:58 UTC
    I'd comment what I meant whenever I'd use the word cmt.