http://qs321.pair.com?node_id=728581


in reply to Little annoying mistakes ... of others

my $line = <STDIN>; chomp $line;

Can be written chomp(my $line = <STDIN>); ;D

Anyway, a pet peeve of mine would be people writing a program in Perl as if they were writing a program in C (or C++, etc). I.e., using C-style for loops when a foreach would work much better. Or, something even more annoying, putting the curlies in the same vertical column~

for (my $i = 0; $i < $#names; $i++) { print $names[$i]; }

I'm so adjective, I verb nouns!

chomp; # nom nom nom

Replies are listed 'Best First'.
Re^2: Little annoying mistakes ... of others
by Jenda (Abbot) on Dec 07, 2008 at 11:40 UTC

      Worse? That's the One True Way. However, for one-liners, horizontal alignment is preferable, i.e.:

      for (my $i = 0; $ <= @names; $i++) { print $names[$i] }

      Given the C code. Hey, sometimes Perl is useful as a C prototyper.

      sas
      The funniest part is that all three for-loops (parent, this, and child) are wrong (which only is a great defense of the whole point). The correct versions are
      for (my $i = 0; $i <= $#names; $i++) { ... } # or for (my $i = 0; $i < @names; $i++) { ... }
      You guys had $i < $#names and $i <= @names. :-)

      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re^2: Little annoying mistakes ... of others
by Porculus (Hermit) on Dec 07, 2008 at 14:25 UTC
    Can be written chomp(my $line = <STDIN>); ;D

    But that doesn't generalise for all rvalue uses. For example, I frequently want to write something like

    frobnicate(chomp) while <$file>;     # doesn't work

    and am of course instead forced to write the loop out longhand, since

    chomp, frobnicate($_) while <$file>; # yuck

    is starting to get hard to read.

    Personally I consider this a failure in Perl's Huffman coding. I can't think of a single case where I've cared in the slightest how many characters chomp removed, but I do want to be able to chomp something and then immediately pass the chomped value to some other routine.

      I think the point is efficiency (avoid copying the argument) rather than counting characters.
Re^2: Little annoying mistakes ... of others
by SuicideJunkie (Vicar) on Dec 07, 2008 at 15:36 UTC
    To contrast, what annoys me, is people putting curlies at the end of a line where they are hard to find, and even harder to match up with their close curly.

    I have no idea why saving one line could be worth reducing readability like that, even on an 800x600 screen like my laptop. :)

      Why do you need to find them? There's a while, for, eval, if, sub or something like that at the start of the line and the following ones are indented.

        Many times there is. But blocks do not always have control structures. And there are often times when you need to break the control up into multiple lines for readability.
        If you ever want to comment out those lines or even the whole block, it is much nicer to have the open curly on its own line.
Re^2: Little annoying mistakes ... of others
by kubrat (Scribe) on Dec 08, 2008 at 16:45 UTC
    And where is the mistake?
      So what are your annoyances or rather those bugs that you see others make while you think they are simple?

      I answered that part. I hope you are not too mad :(

      Although, it can be considered a mistake according to the rules. Sort of.

      I'm so adjective, I verb nouns!

      chomp; # nom nom nom