Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: List Comprehensions

by eyepopslikeamosquito (Archbishop)
on Oct 16, 2014 at 20:07 UTC ( [id://1104107]=note: print w/replies, xml ) Need Help??


in reply to List Comprehensions

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:

  • Use foreach to iterate read-only over each element of a list
  • Use map to create a list based on the contents of another list
  • Use foreach to modify elements of a list
  • Use grep to select elements in a list

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:

  • No reviews. That is, nobody has bothered to review or rate it.
  • No change log. No license. No META.
  • The module has not been updated since 2003. Probably abandoned.
  • This is the only module written by this author.
  • Test results ok, but test suite not comprehensive (just one tiny test.t file).
Contrast with, for example, List::MoreUtils:
  • Seven reviews. Good rating score.
  • Comprehensive change log.
  • The module has been updated many times.
  • This author has written over 100 CPAN modules.
  • Test results ok, test suite has six .t files and a Test.pm.

References Added Later

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1104107]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-29 08:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found