perl.org wrote:
On Thu, 29 Jul 2004 12:08:20 -0400 (EDT), Jeff 'japhy' Pinyan wrote

That's why he broke it down. Is the map() really the problem, or is it the regex, the ?: operator, and the two array references?


All of the above ;), but now that I think about it the map looks easiest, then
> ?: (which I also prefer not to use, along with unless). At least map is a
> word instead of a (cryptic) token.


map() is easier to understand if you rearange it a bit. Take the expression you've been discussing:

map { m/\.([^.]+)$/ ? [$_, $1] : [$_, ''] } @input;

Well, let's make it a complete expression

my @results = map { m/\.([^.]+)$/ ? [$_, $1] : [$_, ''] } @input;

Let's add a little formatting:

my @results = map {
  m/\.([^.]+)$/
    ? [$_, $1]
    : [$_, '']
} @input;

Change the spelling of map and rearrange a bit:

my @results;
foreach (@input) {
  push( @results, m/\.([^.]+)$/
                    ? [$_, $1]
                    : [$_, ''] );
}

We can change the spelling of that ?: thingy too:

my @results;
foreach (@input) {
  if ( m/\.([^.]+)$/ ) {
    push( @results, [$_, $1];
  } else {
    push( @results, [$_, ''];
  }
}

Let's be a little more specific about what we're working with:

my @results;
foreach my $file (@input) {
  if ( $file =~ m/\.([^.]+)$/ ) {
    push( @results, [$file, $1];
  } else {
    push( @results, [$file, ''];
  }
}

That is easier to read. It's more obvious what is going on. I hardly ever write code like this. ;-)

For better or worse, I've gotten use to the terseness that perl is capable of. The original map expression reads just like good prose to me. I guess a person can get used to about anything.

Actually, there does seem to be an advantage to the terseness: it seems to help me comprehend larger chucks of code when reading unfamiliar code. The above is not the greatest example, but you can see that in the rearranged version you have to read about a half dozen lines to figure out what is going on whereas in the original, it's one line.

I can't say which is better, terseness or verbosity. I can say that I had very similar opions to yours when I first encountered perl. I come from a C/C++ background, and I like minimalist languages. The fewer constructs in a language, the fewer the complexities, the fewer the problems. I remember reading the camel book and hating all the ways of saying if/else/unless, amoung other things. Too many ways to do it (TMWTDI). I would never use some of those extraneous constructs...

Oh well, so much for first impressions. I'm now a sinner with the worst of them. There's not a construct I won't abuse. I glory in terseness. I spend untold amounts of time trying to sqeeze two expressions into one. I spend precious cycles pouring over sections of code not because it doesn't work, but because it doesn't look right... esthetically. I want to find ways to restructure my code... poetically.

Confused?

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to