From your question, I am assuming you are an experienced Python programmer, but new to Perl.

If you're looking for something similar to Python list comprehensions in Perl, start by mastering the basic Perl built-ins: foreach, map and grep. As LanX has pointed out, map is the right fit for your specific example. Effective Perl Programming has a nice item "Use foreach, map and grep as appropriate", which provides a handy summary of when to use foreach, map and grep:

If you are interested in functional style programming in Perl, once you've mastered these basics, move on to the Perl core List::Util module. After that, CPAN List::MoreUtils.

Choosing "good" modules from CPAN is a bit of a dark art. Remember, CPAN is a "commons" with a very low barrier of entry ... so it is full both of gems, and utter rubbish. Before adding a dependency to some random third-party CPAN module, I suggest you check some basic things, to get a feel for how good the module is, like the module rating, CPAN Testers results, change log, test suite, reputable author responsive to bug reports, reported bug list, META.yml/json, pre-reqs, license, kwalitee, and so on, then perhaps google for the module to get a feel for how widely used it is and what folks think of it.

For List::Comprehensions, we see:

Contrast with, for example, List::MoreUtils:

See Also


In reply to Re: List Comprehensions by eyepopslikeamosquito
in thread List Comprehensions by tobbo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.