PBP: 065 Other Postfix Modifiers

The PBP suggests one simple thing for using other postfix control structures, such as unless, for, while, and until.  It says: Don’t.

All the comments Mr. Conway makes about block forms being easier to read in most cases, and about avoiding $_ are strong, as far as I’m concerned.  Not being able to name the iterator means that if you’re doing something more than trivial, you wind up using $_ or the even stranger defaults.

print for grep {defined $_} @generated_lines;

This makes my teeth squeak, especially when it’s so much clearer in the block form, for the cost of two extra lines with { and } on them.  Oh wait, that’s a benefit here.

In general, avoid most of the postfix operators, except when it isn’t clearer.

One Response to “PBP: 065 Other Postfix Modifiers”

  1. Gustavo Chaves says:

    You really don’t need the topicalizer in that line, which can be simplified like so:

    print for grep {defined} @generated_lines;

    In fact, you don’t need the “for” either, since “print” can deal with a list:

    print grep {defined} @generated_lines;

    A good example would involve some code treating each list element by itself. Perhaps something like this:

    foo($_) for @list;

    I happen to like this construct due to its terseness. I tend to use “for” whenever the code dealing with the list elements is really small, like a routine call.

    BTW, I’m enjoying your series of posts about PBP. Keep on!

Leave a Reply