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.
  • Multiline mappings is something I do a lot. The Perl::Critic advice is:

    "The best solutions to a complex mapping are: 1) write a subroutine that performs the manipulation and call that from map; 2) rewrite the map as a for loop."

    I guess the advantage of a for loop over a map is that the array over which you are iterating is upfront.

    I always have a problem with the indentation of my multiline maps anyway. How should the brackets around the block line up? How should I line up the final array?
    • As usual in Perl:Critic, this policy is optional. I flagged it as a "medium" severity (a 3 on a 1 to 5 scale). Multi-statement maps do legitimate uses, but generally should not be used when you can find a better way to say the same thing (which is most of the time). You can always annotate your code such that Perl::Critic will ignore the violation:

        my @out = map {  ##no critic(ProhibitComplexMapping)
            foo($_);
            bar($_);
        } @list;

      Note that the "no c

      • Oooh, argh! Your choice of indentation is painful! If the large chunks are the target, source, map, and map contents, it is semi-sensible to indent based on that. Often, I'd assume this would mean a line per chunk or inlining two or three if they were really simple.

        The thing that gets me is you close your map guts and then cuddle the source into that. It's what's driving the entire loop! I'd think it rates pretty highly in importance and should be visually distinct. How about another line for it?

        tgt =

        • Yes, well, multi-statement map() is painful. :-) I used that indentation just to emphasize where the "##no critic" goes, if someone were to want it. In real code, I try to make my maps fit readably on one line, or else I choose not to use map.