Stories
Slash Boxes
Comments

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Friday February 08, 2008
11:50 AM

The Pain of a Beautiful Solution

[ #35613 ]

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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • 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 [cpan.org], 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!