tobbo has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have an array of objects and these objects have a method that returns a string. Now I'm looking for a one-liner to get an array of these strings for all objects. So I'm searching a Perl equivalent to the Python-esque

[ $_->my_method() for $_ in @my_list].

I already found List::Comprehensions, which does exactly what I need, but I can't install it due to a failed test.

Replies are listed 'Best First'.
Re: List Comprehensions
by eyepopslikeamosquito (Archbishop) on Oct 16, 2014 at 20:07 UTC

    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.

    See Also

Re: List Comprehensions
by LanX (Saint) on Oct 16, 2014 at 18:49 UTC
     [ map { $_->my_method() } @my_list ] should¹ do.

    > I already found List::Comprehensions

    ugh ...

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    update

    ¹) yep :)

    DB<8> [ map { 10-$_ } 1..10 ] => [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
      Works like a charm, thanks!
        You asked explicitly for an array reference, that's why I showed you this rather uncommon construction.

        NB: in Perl you don't need the brackets for a plain list or a "normal" array (i.e. in "list form" @array )!

        update

        in > 90% of all cases something like

        my @output = map { $_->meth } @input

        is preferred and as soon as you really need a reference you'll use \@output

        Cheers Rolf

        (addicted to the Perl Programming Language and ☆☆☆☆ :)