e evenly obvious, although there is still no comment?

First, almost a side-issue here, but an important one. My personal take on comments is expressed at length here and here; but can be summarised by saying that I think that for the most part comments in source code are a counter-productive waste of time, energy, resources and therefore, money.

That said, in the isolated form, I find your self-documenting code a highly readable, fine attempt at the art.

But ... it only works because it is in isolation of a context.

For example, prepend_yearmonth() requires to close over %months. In a short script, the hash might be a package global and declared above the declaration of the subroutines; the actual sort is part of the package level code and everything's hunky-dory.

But now put the sort inside a subroutine -- say, part of the logic of an object's method call. And the hash being referenced by the sort is a part of that object's instance data. Now there is simply no way to define those utility subs to close over the required reference hash.

And that's the crux of the problem that anonymous lambda's address. They allow the programmer to define callback code in the context of its use, thus availing it of access to everything that is in scope at that point. Named-subroutines cannot do this -- in Perl at least -- because any closures used by subs declared in a nested context will result in the infamous:

sub x{ my $y; sub z{ say $y; } };; Variable "$y" will not stay shared at ...

If you feel it is necessary to (self)document the use pattern of the GRT -- and I can make a strong case that it is better for programmers to learn the pattern than to have it hidden from them -- then I would perhaps suggest something along the lines of:

#! perl -slw use strict; use GRT; # contains: #sub GRT::prefix(&@) { my $code = shift; map &$code, @_ } #sub GRT::discard(&@) { my $code = shift; map &$code, @_ } my %months = ( FY => 0, Jan => 1, Feb => 2, Mar => 3, Apr => 4, May => 5, Jun => 6, Jul => 7, Aug => 8, Sep => 9, Oct => 10, Nov => 11, Dec => 12, ); print for GRT::discard{ unpack 'x[nn]A*' } sort{ $b cmp $a } GRT::prefix{ my( $alpha, $num ) = m[^(\S+?)\s*(\d+)$]; $num += 2000 if $num <= 49; $num += 1900 if $num <= 99; pack 'nnA*', $num, $months{ $alpha }, $_; } <DATA>; __DATA__ Apr 2006 FY05 FY98 FY04 Dec 2007 Jan 1997 Jan 1998 Dec 1998

The key points of this are:

  1. The GRTsubs can be imported from a module.
  2. That module can document the GRT and the use of the helper functions.
  3. The purely implementation elements, the maps, are hidden.
  4. The sort comparator block can be specified to invert the order of the sort.
  5. It can be constructed at any scope and make full use of the context.

Of course, it is also substantially slower and little more than a pair of 'active comments'; which leaves me believing that the simple addition of a couple of comments achieves the same thing:

print for map{ ## remove them once sorted unpack 'x[nn]A*', $_ } sort map { ## prefix year & month in binary form my( $alpha, $num ) = m[^(\S+?)\s*(\d+)$]; $num += 2000 if $num <= 49; $num += 1900 if $num <= 99; pack 'nnA*', $num, $months{ $alpha }, $_; } <DATA>;

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.

In reply to Re^9: I want you to convince me to learn Perl by BrowserUk
in thread I want you to convince me to learn Perl by kasxperanto

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.