http://www.perlmonks.org?node_id=1055649


in reply to Re^4: I want you to convince me to learn Perl
in thread I want you to convince me to learn Perl

I had seldom need for anonymous subs, so didn't miss multiline ones :-)

You've never spread a sort block or map block or grep block over two or three lines?


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
  • Comment on Re^5: I want you to convince me to learn Perl

Replies are listed 'Best First'.
Re^6: I want you to convince me to learn Perl
by Tux (Canon) on Sep 25, 2013 at 11:09 UTC

    something like this? (excerpt from App::tkiv)

    my @img = map { $_->[0] } sort { $tsort{$Option{thumbsorting}}->() } map { my $seq = m/(\d+)/ ? $1 : 0; [ $_, $seq, (stat "$Idir/$_")[7,9], lc $_, rand 1 ] } grep { my $if = "$Idir/$_"; # Sanity check. Minimal image size 100 my $s = -s $if; # Skip MacOS working copies $if =~ s{/\._([^/]+)$}{/$1} && -s $if and $s = 0; $s and $s > 100; } # convert can't deal with .ico files (yet) # Tk can deal with Tiff/NEF as of 804.027_501 with Tk::TIFF grep m/\.(jpe?g|gif|x[pb]m|png|bmp|tiff?|nef)$/i => readdir +IDIR;

    Enjoy, Have FUN! H.Merijn
Re^6: I want you to convince me to learn Perl
by soonix (Canon) on Sep 25, 2013 at 14:35 UTC
    See, those little helpers are our friends. Don't good friends deserve a name? :-)
      Don't good friends deserve a name?

      Sorry. I see your smiley face, but I'm going to answer the question seriously: Absolutely frikkin' not :)

      Why contrive a name for something that will never be called anywhere else?

      Why contrive a name for a block of code that only makes sense in one place in the source?

      That will often will only work in one place in the source because it needs access to lexical variables in scope at that position (closures). If you make the blocks named subroutines, then they have to be declared out-of-line of where they are used, which means you now need to pass (what would be) closures via arguments; and that means making up more names for the formals.

      Take a look at this, and tell me, what would you name the two map blocks?

      And if you're going to go that route, how about naming the blocks for your while loops; and for loops; and if & elsif & else bodies; and ...


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        I see your arguments and they are true. But - there are no comments in the sample code. Although it is more or less obvious what it does (even without the surrounding node), wouldn't
        sub discard_sortkey { unpack 'x[nn]A*' }; sub prepend_yearmonth { my( $alpha, $num ) = m[^(\S+?)\s*(\d+)$]; $num += 2000 if $num <= 49; $num += 1900 if $num <= 99; pack 'nnA*', $num, $months{ $alpha }, $_; } print for map{ discard_sortkey } sort map { prepend_yearmonth } <DATA> +;
        be evenly obvious, although there is still no comment?

        My field of operations was mainly CRM/ERP systems. At my first job, we used AWK to generate COBOL program source for the different target systems. At my previous job I still generated (and sometimes programmed) COBOL - in this very century. Of course, when Perl became known, I liked it at first sight, especially because I knew the patchwork tools it aimed to replace.

        OTOH, most of the languages I had worked with before, had variable declarations and no "contexts", hence no need for sigils. Thus, when I later saw Python, I liked it, too. I think this is what the OP called "cleaner".

        I see virtues in both approaches.

        (Update: Originally posted under a different node in this thread)

        "Why contrive a name for something that will never be called anywhere else? Why contrive a name for a block of code that only makes sense in one place in the source?"

        This is one of the compelling arguments for favoring C++11 over C++98(03): In older flavors of C++ the standard algorithms often required that a functor class be passed in, for example, as a comparator. Believe it or not (I'm sure you already know this, though), people had to go to all the trouble of creating a named class with an overloaded operator(), and then pass that named class to a standard algorithm just to do simple things like calling a sort routine, or a binary search routine on non-POD types. And even then it might be necessary to overload operator< as well. C++11 finally provided lambda functions, which allowed users to stop going through all the silly work just to create a comparator that would be used in only one place. In other words, in this regard, C++11 finally gained the ability to make something that should be simple, relatively easy as well, and this is one of the things that has made C++ fun, finally. Perl has had anonymous functions for...well, I don't know how long, but at least dating back to when sort and map made it into the language.

        So what I don't get is, after all these years, and after so much work has gone into making older languages like C++ adopt the approach, why people still say, "now why would you want that?"


        Dave

      NO!

      Wasting time inventing a name for something I use once, ensuring the name is unique within its namespace, moving the three lines somewhere far away from the place they are used and risking someone calls them differently than originally intended, just because the author of the language could not think of a nice enough syntax? No thanks!

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.