The Pain of a Beautiful Solution

Ovid on 2008-02-08T15:50:03

Iterating over a list, five elements at a time:

for ( my $num = 20; $num <= 100; $num += 5 ) {
    print $num, $/;
}

That's ugly!

sub increment {
    my ( $start, $end, $inc ) = @_;
    $start -= $inc;
    return sub {
        return if $start >= $end;
        $start += $inc;
    };
}

my $inc = increment( 20, 100, 5 );
while ( my $num = $inc->() ) {
    print $num, $/;
}

A vaguely interesting abstraction, but I hate that you have to declare $inc before hand (you can work around this with grep, but I like the lazy list).

use List::Maker;

for my $num (<20..100 x 5>) {
    print $num, $/;
}

Elegant and clear. I love what Damian has done, but ...

package List::Maker;

# several lines later

no warnings 'redefine';
*CORE::GLOBAL::glob = sub
{

Oops. No file globbing any more. Plus, internally this uses grep to calculate all the elements at once. I'd prefer a lazy list.

Damian has stated that the file globbing issue is fixed in the next release, but we're still waiting on that release.

The moral of this story: no matter how tempting, don't reach for *CORE::GLOBAL::.... It's caused us too much pain at work with other modules. Encouraging the problem doesn't help.


Fixed!

Smylers on 2008-02-21T21:39:09

Damian has stated that the file globbing issue is fixed in the next release, but we're still waiting on that release.

And it was released, with that bug fixed, just a few days after this blog entry linking to it.

So thank you for any part you played in bringing that about!