Beautiful Code Blog

schwern on 2007-08-06T23:48:54

I've been invited to write in the new Beautiful Code Blog set up by O'Reilly to mirror their new book Beautiful Code. I'm intrigued and a little terrified at the idea. It will be fun to be the Perl programmer talking about beautiful code. :)

Suggestions welcome.

My first may well be this:

use LWP::Simple; my $html = get "http://www.example.com";


map and grep

claes on 2007-08-07T06:56:43

I've seen that many programmers coming to Perl from other languages aren't aware of or are afraid of using is 'map' and 'grep'.

I think these two are among the most beautiful operators that exists in Perl and deserve some nice examples.

Envy!

Ovid on 2007-08-07T08:03:20

Congrats. And that's a great first example. There's so much hidden away behind an interface that's intuitive (though I think the abstraction is marred by the lack of a thrown exception). I think 'use Fatal' would also be fantastic. It seems like a simple example, but it's really beautiful (to me) in what it does.

import vs explicit importing

markjugg on 2007-08-07T14:35:23

I might find it more beautiful if there was an explicit import:

use LWP::Simple 'get'

What happened to the mantra from Exporter.pod?

"Do not export anything else by default without a good reason! ... Exports pollute the namespace of the module user."

Re:import vs explicit importing

schwern on 2007-08-09T21:23:50

I hate that line.

Oh, its intentions are good. Its there to make authors think and to avoid modules like POSIX which export nearly everything. So often people only hear the "do not export anything by default" and miss the "without a good reason" part.

If your module doesn't do anything useful but provide functions, go ahead and export some useful defaults. The user's going to have to import stuff anyway, give them a leg up. Why? Choice. Specifically the user's.

Any module which exports by default can be turned into a module which does not. Observe: use LWP::Simple (). It can also be turned into one which explicitly exports: use LWP::Simple qw(get). However, you can't take a module which does not export by default and turn it into one that does. I prefer to let the user decide if they want my convenient defaults or not.

The worst offenders are those who don't allow exporting at all, I'm looking at you mro::compat and Class::ISA.

Re:import vs explicit importing

markjugg on 2007-08-10T16:31:26

I appreciate your line of thinking, schwern. Thanks for clarifying.

Personally, I have mixed feelings about it. Some default exports can be handy and look clean. However, I like to grep my code for a function name and be able to figure out where it came from.

OO code tends to be traceable to the object construction. But with with procedural code, if there's no explicit export, it is more difficult to track down where a function comes from, and that can slow me down. (Yes, I know there are modules and techniques to help with this, but I'm used to the simplicity of grep).

So, although I see both sides, I tend to favor explicit imports so I can see where things come from.

Re:import vs explicit importing

schwern on 2007-08-11T03:39:47

So, although I see both sides, I tend to favor explicit imports so I can see where things come from.


The wonderful thing is as a user of the module you always have that option no matter what the module author chooses to do.